Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3560 Accepted Submission(s): 1241
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 using namespace std; 7 const int maxn=500010; 8 const long double PI=acos(-1.0); 9 struct complex{ 10 long double r,i; 11 complex(long double r_=0.0,long double i_=0.0){ 12 r=r_;i=i_; 13 } 14 complex operator +(complex &a){ 15 return complex(a.r+r,a.i+i); 16 } 17 complex operator -(complex &a){ 18 return complex(r-a.r,i-a.i); 19 } 20 complex operator *(complex a){ 21 return complex(r*a.r-i*a.i,i*a.r+a.i*r); 22 } 23 }A[maxn]; 24 25 void Rader(complex *a,int len){ 26 for(int i=1,j=len>>1;i<len-1;i++){ 27 if(i<j)swap(a[i],a[j]); 28 int k=len>>1; 29 while(j>=k){ 30 j-=k; 31 k>>=1; 32 } 33 j+=k; 34 } 35 } 36 37 void FFT(complex *a,int len,int on){ 38 Rader(a,len); 39 for(int h=2;h<=len;h<<=1){ 40 complex wn(cos(-on*PI*2/h),sin(-on*PI*2/h)); 41 for(int j=0;j<len;j+=h){ 42 complex w(1.0,0); 43 for(int k=j;k<j+(h>>1);k++){ 44 complex x=a[k]; 45 complex y=a[k+(h>>1)]*w; 46 a[k]=x+y; 47 a[k+(h>>1)]=x-y; 48 w=w*wn; 49 } 50 } 51 } 52 if(on==-1) 53 for(int i=0;i<len;i++) 54 a[i].r/=len; 55 } 56 int a[maxn]; 57 long long num[maxn]; 58 int main(){ 59 #ifndef ONLINE_JUDGE 60 //freopen("","r",stdin); 61 //freopen("","w",stdout); 62 #endif 63 int T,n,len=1; 64 scanf("%d",&T); 65 while(T--){ 66 scanf("%d",&n); 67 memset(A,0,sizeof(A)); 68 memset(num,0,sizeof(num)); 69 while(len<=200000)len<<=1; 70 for(int i=1;i<=n;i++) 71 scanf("%d",&a[i]); 72 sort(a+1,a+n+1);len=1; 73 while(len<=a[n]*2)len<<=1; 74 for(int i=1;i<=n;i++) 75 A[a[i]].r++; 76 FFT(A,len,1); 77 for(int i=0;i<len;i++) 78 A[i]=A[i]*A[i]; 79 FFT(A,len,-1); 80 for(int i=0;i<len;i++) 81 num[i]=(long long)(A[i].r+0.5); 82 for(int i=1;i<=n;i++) 83 num[a[i]<<1]--; 84 for(int i=0;i<len;i++) 85 num[i]>>=1; 86 for(int i=1;i<len;i++) 87 num[i]+=num[i-1]; 88 long long cnt=0; 89 for(int i=1;i<=n;i++){ 90 cnt+=num[len-1]-num[a[i]]; 91 cnt-=(long long)(n-i)*(i-1); 92 cnt-=n-1; 93 cnt-=(long long)(n-i)*(n-i-1)/2; 94 } 95 long long tot=((long long)n*(n-1)*(n-2))/6; 96 printf("%.7lf\n",1.0*cnt/tot); 97 } 98 return 0; 99 }
FFT(快速傅里叶变换):HDU 4609 3-idiots
原文:http://www.cnblogs.com/TenderRun/p/5518143.html