icodelab equilateral triangle

description

Xiao Ming in the hands of a bunch of different lengths of small stick, Xiao Ming randomly selected from a pile of three to see if this makes up a triangle, obviously the probability of success is very high, now Xiao Ming would like to increase the difficulty,

His randomly selected from the stack stick 4, and looking forward to spell an equilateral triangle, obviously this possibility is relatively low. You are given n wooden stick, asked how many kinds of selection methods.

Because the method may be chosen very large, the need for the final answer modulo 1e9 + 7.

Entry

Enter two lines

The first line n, the stick was a total of n;

The second line of a total number of n, the length of each stick was

Export

It outputs a number representing the total number of successful selection method

Sample input 1

4
2 2 4 4

Sample Output 1

1

prompt

n<=100,000;

Stick length <= 5000

Thinking

Arrangement A (n, m) = n × (n-1). (Nm + 1) = n! / (Nm)! (N is a subscript, m is superscript, the same applies hereinafter)
composition C (n, m) = P (n, m ) / P (m, m) = n! / m! (nm)!

When the problem when i = j,

  The length of the sticks and when 1/2 of the length of the stick were ≥2, ans + = a [i] * (a [i] -1) / 2 * (a [i << 1] * (a [i << 1 ] -1) / 2 ans + = C (i, 2) + C (j, 2);

  A stick length, B sticks are ≥1, A + B when the stick ≥2, ans + = a [i] * a [j] * (a [i + j] * (a [i + j] -1 ) / 2 ans + = C (i + j, 2) + i + j;

Code:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=5010;
const int Mod=1e9+7;

int n,x;
long long ans,a[N];

int main () {
	scanf("%d",&n);
	while(n--) {
		scanf("%d",&x);
		a[x]++;
	}
	for(int i=1; i<=5000; i++)
		for(int j=i; j<=5000; j++) {
			if(i+j>5000)
				break;
			if(i==j) {
				if(a[i]>=2&&a[i<<1]>=2)
					ans=(ans+(a[i]*(a[i]-1)/2LL%Mod*(a[i<<1]*(a[i<<1]-1)/2)%Mod))%Mod;
					ans = (ANS + (a [i] * a [j]% Mod * (A [i + j] * (A [i + j] -1) / 2)% Mod))% Mod;
			} Else {
				if(a[i]>=1&&a[j]>=1&&a[i+j]>=2)
			}
		}
	printf("%lld\n",ans);
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11306455.html