3-idiots
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6517 Accepted Submission(s): 2269
Problem Description
King OMeGa catched three men who had been streaking in the street. Looking as idiots though, the three men insisted that it was a kind of performance art, and begged the king to free them. Out of hatred to the real idiots, the king wanted to check if they were lying. The three men were sent to the king’s forest, and each of them was asked to pick a branch one after another. If the three branches they bring back can form a triangle, their math ability would save them. Otherwise, they would be sent into jail.
However, the three men were exactly idiots, and what they would do is only to pick the branches randomly. Certainly, they couldn’t pick the same branch - but the one with the same length as another is available. Given the lengths of all branches in the forest, determine the probability that they would be saved.
Input
An integer T(T≤100) will exist in the first line of input, indicating the number of test cases.
Each test case begins with the number of branches N (3≤N≤105) ( 3 ≤ N ≤ 10 5 ) .
The following line contains N integers a_i (1≤ai≤105) ( 1 ≤ a i ≤ 10 5 ) , which denotes the length of each branch, respectively.
Output
Output the probability that their branches can form a triangle, in accuracy of 7 decimal places.
Sample Input
2
4
1 3 3 4
4
2 3 3 4
Sample Output
0.5000000
1.0000000
给你n根木棍的长度,任意取三根问你能组成三角形的概率
因为n很大所以用常规的母函数等方法枚举肯定超时,这个时候就可以用FFT求解多项式乘积问题了
我们可以把木棍的长度作为数组下标也就是多项式的次幂,木棍长度出现的次数作为多项式的系数如第一组样例
{1,3,3,4} { 1 , 3 , 3 , 4 }
那么可以构成多项式
A(x)=x1+2x3+x4 A ( x ) = x 1 + 2 x 3 + x 4
那么 A(x)∗A(x) A ( x ) ∗ A ( x ) 也就是这么多根木棍中任取两根所能构成的长度
若以 B(x)=A(x)∗A(x) B ( x ) = A ( x ) ∗ A ( x ) ,那么多项式 B(x) B ( x ) 的某个次幂的系数就代表这个这些木棍能构成这个长度的方案数
注意
上面的多项式把木棍取3,4和木棍取4,3的方案认为不同,所以所有方案数要除2
然后还要减掉自己取自己的情况
然后我们获得了一个任取两根木棍所能构成的长度的方法数,所能构成三角的情况是两边之和大于第三边,我们还得做一个前缀和,那么大于某个长度的方法数就是 sum[len]−sum[i] s u m [ l e n ] − s u m [ i ]
然后我们再枚举一遍木棍的长度,我们假定这次枚举的木棍的长度是三角形中最长的那一根,那么
因为新的多项式中的系数中包含的元素可能是存在有比我枚举的木棍长度大的情况,我们还得减去这些情况,枚举前把木棍排个序
系数里面包括一个比要枚举的木棍大的和一个比枚举木棍小的
这个时候要减去这些木棍的组合就是 (n−1−i)∗i ( n − 1 − i ) ∗ i
系数里面包括两个都大于要枚举的木棍的
这个时候组合就是(n-1-i)*(n-2-i)/2
系数里面包括一个枚举的数和一个其他数
这个时候要减去的就是n-1
#include
#include
#include
#include
#include
#include
#define PI acos(-1)
using namespace std;
typedef long long ll;
const int MAXN = 400040;
struct complex
{
double a, b;
complex(double aa = 0.0, double bb = 0.0)
{
a = aa;
b = bb;
}
complex operator +(const complex &e)
{
return complex(a + e.a, b + e.b);
}
complex operator -(const complex &e)
{
return complex(a - e.a, b - e.b);
}
complex operator *(const complex &e)
{
return complex(a * e.a - b * e.b, a * e.b + b * e.a);
}
} x1[MAXN], x[MAXN];
void change(complex *y, int len)
{
int i, j, k;
for(i=1,j=len/2; i1; i++)
{
if(i < j) swap(y[i], y[j]);
k = len/2;
while(j >= k)
{
j-=k;
k>>=1;
}
if(j < k) j += k;
}
}
void fft(complex *y, int len, int on)
{
change(y, len);
for(int h=2; h<=len; h<<=1)
{
complex wn(cos(-on*2*PI/h), sin(-on*2*PI/h));
for(int j=0; jcomplex w(1, 0);
for(int k=j; k2; k++)
{
complex u = y[k], t = w*y[k+h/2];
y[k] = u + t, y[k+h/2] = u- t, w = w*wn;
}
}
}
if(on == -1) for(int i=0; ilong long a[MAXN/4];
long long ans[MAXN];
long long sum[MAXN];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long n;
scanf("%lld",&n);
memset(x1,0,sizeof(x1));
memset(ans,0,sizeof(ans));
long long maxn=0;
for(int i=0; iscanf("%lld",&a[i]);
x1[a[i]].a++;
maxn=max(a[i],maxn);
}
sort(a,a+n);
int len=1;
while(len<2*maxn+2) len<<=1;
fft(x1,len,1);
memset(x,0,sizeof(x));
for(int i=0; i1);
for(int i=0; i0.5);
for(int i=0; i2]--;
for(int i=1;i<=len;i++)
ans[i]/=2;
memset(sum,0,sizeof(sum));
for(int i=1;i<=len;i++)
sum[i]+=sum[i-1]+ans[i];
long long res=0;
for(long long i=0;i1);
res-=(n-1-i)*i;
res-=(n-1-i)*(n-i-2)/2;
}
long long tot = (long long)n*(n-1)*(n-2)/6;
printf("%.7lf\n",(double)res/tot);
}
return 0;
}