Fft
Code
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cmath>5#include <algorithm>6#include <complex>7 using namespacestd;8 Const intN =400005;9 Const DoublePI = ACOs (-1.0);Tentypedef complex<Double>Comp; One Comp A[n],b[n]; A intLen,l,n,i,v,c[n]; - Long LongAns[n]; - DoubleAns,nn; the voidRader (Comp *f,intlen) - { - intj = Len >>1; - for(intI=1; i<len-1; i++) + { - if(i<j) Swap (F[i],f[j]); + intK = len>>1; A while(J >=k) at { -J-=K; -K >>=1; - } - if(J < k) J + =K; - } in } - voidFFT (Comp *f,intLenintOn ) to { + rader (f,len); - for(intH=2; h<=len;h<<=1) the { *Comp wn (cos (-on*2*pi/h), sin (-on*2*pi/h)); $ for(intj=0; j<len;j+=h)Panax Notoginseng { -Comp W (1,0); the for(intk=j;k<j+h/2; k++) + { AComp U =F[k]; theComp t = w*f[k + H/2]; +F[k] = U +T; -F[k + H/2] = U-T; $w = w *WN; $ } - } - } the if(On =-1) - for(intI=0; i<len;i++)WuyiF[i]/=Len; the } - voidConv (Comp *a,comp *b,intlen) Wu { -FFT (A,len,1); AboutFFT (B,len,1); $ for(intI=0; i<len;i++) -A[i] =a[i]*B[i]; -FFT (a,len,-1); - for(intI=0; i<len;i++) AAns[i]=a[i].real () +0.5; + } the intMain () - { $ inttest; thescanf"%d",&test); the while(test) the { thetest--; -scanf"%d",&n); inL=0; theMemset (A,0,sizeof(a)); thememset (b,0,sizeof(b)); About for(i=1; i<=n;i++) the { thescanf"%d",&v); thec[i]=v; + intQ=a[v].real () +1; - a[v].real (q); the b[v].real (q);BayiL=Max (l,v); the } thelen=1; - while(len<=l*2) len*=2; - Conv (A,b,len); the for(i=1; i<=n;i++) theans[c[i]+c[i]]--; the for(i=0; i<len;i++) theAns[i]/=2; - for(i=1; i<len;i++) theans[i]=ans[i-1]+Ans[i]; theSort (c+1, c+1+n); thenn=N;94ans=0; the for(i=1; i<=n;i++) theans+= (ans[len-1]-ans[c[i]]-(nn-1)-(nn-i) * (I-1)-(nn-i) * (nn-i-1)/2); theprintf"%.7lf\n", ans/(nn* (nn-1) * (nn-2)/6));98 } About}
hdu4609 3-idiots