A Little spit groove: I see a lot of analysis on the Internet, are in the analysis of the problem, said a half-day of the FFT, in fact, I feel more of the FFT as a tool for the good
Analysis: This problem if the data is small, statistics two is added to the number of X this step (this step is actually to find convolution AH), completely can the mother function, but the data is very large, with the FFT
Then the difficulty lies in the final statistics, to subtract themselves, two are large, a big one small, including themselves, this is used to tolerate, and then do similar problems, should look at this aspect more
Note: Again highly admire kuangbin God, this is my second question of the FFT, is also the second time with the Kuangbin FFT template
#include <stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<vector>#include<math.h>#include<map>using namespaceStd;typedefLong LongLL;Const intinf=0x3f3f3f3f;Const intn=4e5+5;Const DoublePI = ACOs (-1.0);structcomplex{DoubleR,i; Complex (DoubleR=0,DoubleI=0) {R=r;i=I; } Complexoperator+(ConstComplex &a)Const{ returnComplex (r+a.r,i+A.I); } Complexoperator-(ConstComplex &a)Const{ returnComplex (r-a.r,i-A.I); } Complexoperator*(ConstComplex &a)Const{ returnComplex (r*a.r-i*a.i,r*a.i+i*A.R); }};voidChange (complex x[],intLen) { inti,j,k; for(i=1, j=len/2; i<len-1;++i) { if(i<j) Swap (X[i],x[j]); K=len/2; while(j>=k) {j-=k;k>>=1;} if(j<k) j+=K; }}voidFFT (complex x[],intLenintOn ) {Change (X,len); for(intI=2; i<=len;i<<=1) {Complex wn (cos (-on*2*pi/i), sin (-on*2*pi/i)); for(intj=0; j<len;j+=i) {Complex W (1,0); for(intk=j;k<j+i/2;++k) {Complex U=X[k]; Complex T= w*x[k+i/2]; X[K]=u+T; X[k+i/2]=u-T; W=w*WN; } } } if(on==-1) for(intI=0; i<len;++i) x[i].r/=Len;} Complex x[n];intA[n>>1]; LL Num[n],sum[n];intMain () {intT,n; scanf ("%d",&T); while(t--) {scanf ("%d",&N); memset (num,0,sizeof(num)); for(intI=0; i<n;++i) {scanf ("%d", &a[i]); + +Num[a[i]];} Sort (A,a+N); intLen1 = a[n-1] +1, Len =1; while(len<2*LEN1) len<<=1; for(intI=0; i<len1;++i) X[i]=complex (Num[i],0); for(intI=len1;i<len;++i) X[i]=complex (0,0); FFT (X,len,1); for(intI=0; i<len;++i) x[i]=x[i]*X[i]; FFT (X,len,-1); for(intI=0; i<len;++i) num[i]=(Long Long) (x[i].r+0.5); Len=2*a[n-1]; for(intI=0; i<n;++i)--num[a[i]+A[i]]; for(intI=1; i<=len;++i) num[i]>>=1; for(intI=1; i<=len;++i) sum[i]=sum[i-1]+Num[i]; LL CNT=0; for(intI=0; i<n;++i) {CNT+=sum[len]-Sum[a[i]]; CNT-=1ll* (n1-I.) *i; CNT-= (n1); CNT-=1ll* (n1-i) * (n-i-2)/2; } LL tot=1ll*n* (n1) * (n2)/6; printf ("%.7f\n",1.0*cnt/tot); } return 0;}
View Code
HDU 4609 3-idiots fft+ tolerance