Test instructions: Give the length of the n stick, take three sticks, find the same stick and can constitute the probability of the triangle;
Reference: http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html
Idea: The meaning of the result of the FFT is computed convolution;
Convolution refers to the number of various values that can occur in each of the two sets, and the number of values that may occur for each of the various possible values in each set;
The case of two numbers is obtained, and the same stick and the order rod are excluded;
Then enumerate the longest sides in the three sides, excluding the case of not less than the enumeration edge;
Be sure to use a long long;
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#definePi ACOs (-1.0)intLen1,len2;Long Longa[500100],num[500100],sum[500100];structcomplex{DoubleR,i; Complex () {}; Complex (DoubleXDoubley) {R=x,i=y; } Complexoperator+(ConstComplex &t)Const{ returnComplex (r+t.r,i+t.i); } Complexoperator-(ConstComplex &t)Const{ returnComplex (r-t.r,i-t.i); } Complexoperator*(ConstComplex &t)Const{ returnComplex (r*t.r-i*t.i,r*t.i+i*T.R); }}x1[500100];voidFFT (Complex y[],intNintrev) { for(intI=1, j,k,t;i<n;i++){ for(j=0,k=n>>1, t=i;k;k>>=1, t>>=1) j=j<<1|t&1; if(i<j) Swap (Y[i],y[j]); } for(ints=2, ds=1; s<=n;ds=s,s<<=1) {Complex WN=complex (cos (rev*2*pi/s), sin (rev*2*PI/S)), W=complex (1,0), t; for(intk=0; k<ds;k++,w=w*WN) { for(inti=k;i<n;i+=s) {T=w*y[i+DS]; Y[i+ds]=y[i]-T; Y[i]=y[i]+T; } } } if(rev==-1) for(intI=0; i<n;i++) y[i].r/=N;}Long LongTemp,ans;intMain () {inti,j,k,t,n,m; while(SCANF ("%d", &t)! =EOF) { while(t--) {memset (num,0,sizeof(num)); memset (SUM,0,sizeof(sum)); Ans=0; scanf ("%d",&N); for(i=0; i<n;i++) {scanf ("%d",&A[i]); Num[a[i]]++; } sort (A,a+N); Len1=a[n-1]; intlen=1; while(len<=len1*2) len<<=1; for(i=0; i<=len1;i++) X1[i]=complex (Num[i],0); for(; i<len;i++) X1[i]=complex (0,0);//ExtendedFFT (X1,len,1);//DFT for(i=0; i<len;i++) X1[i]=x1[i]*x1[i];//Point MultiplicationFFT (x1,len,-1);//IDFT for(i=0; i<len;i++) Num[i]=(Long Long) (x1[i].r+0.5);//Resultslen=2*a[n-1]; for(i=0; i<n;i++) Num[a[i]*2]--;//Two sticks are the same for(i=1; i<=len;i++) Num[i]/=2;//a different order takes only one for(i=1; i<=len;i++) {Sum[i]=sum[i-1]+Num[i]; } for(i=0; i<n;i++)//enumerate the longest edges in three sides{ans+=sum[len]-sum[a[i]];//the sum of the sides is greater than the third sideans-= (Long Long) (n-i-1) *i;//a big, smallans-= (n1);//same side as the enumeration edgeans-= (Long Long) (n-i-1) * (n-i-2)/2;//two big} temp=(Long Long) N (n1) * (n2)/6;//must be a long longprintf"%.7f\n",(Double) ans/temp); } } return 0;}
HDU 4609 3-idiots (Fast Fourier convolution)