题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609
算不合法的比较方便;
枚举最大的边,每种情况算了2次,而全排列算了6次,所以还要乘3;
注意枚举最大边的范围是 mx 而不是 lim !!否则会超过开的数组范围!!!
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef double db; typedef long long ll; int const xn=(1<<20),xm=1e5+5; db const Pi=acos(-1.0); int n,rev[xn],lim,num[xm]; struct com{db x,y;}a[xn]; com operator + (com a,com b){return (com){a.x+b.x,a.y+b.y};} com operator - (com a,com b){return (com){a.x-b.x,a.y-b.y};} com operator * (com a,com b){return (com){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};} int rd() { int ret=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();} while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar(); return f?ret:-ret; } void fft(com *a,int tp) { for(int i=0;i<lim;i++) if(i<rev[i])swap(a[i],a[rev[i]]); for(int mid=1;mid<lim;mid<<=1) { com wn=(com){cos(Pi/mid),tp*sin(Pi/mid)}; for(int j=0,len=(mid<<1);j<lim;j+=len) { com w=(com){1,0}; for(int k=0;k<mid;k++,w=w*wn) { com x=a[j+k],y=w*a[j+mid+k]; a[j+k]=x+y; a[j+mid+k]=x-y; } } } if(tp==1)return; for(int i=0;i<lim;i++)a[i].x=a[i].x/lim; } int main() { int T=rd(); while(T--) { n=rd(); int mx=0; memset(num,0,sizeof num); for(int i=1,x;i<=n;i++)x=rd(),num[x]++,mx=max(mx,x); lim=1; int l=0; while(lim<=mx+mx)lim<<=1,l++; for(int i=0;i<lim;i++) rev[i]=((rev[i>>1]>>1)|((i&1)<<(l-1))); for(int i=0;i<lim;i++)a[i].x=0,a[i].y=0; for(int i=0;i<=mx;i++)a[i].x=num[i]; fft(a,1); for(int i=0;i<lim;i++)a[i]=a[i]*a[i]; fft(a,-1); for(int i=2;i<lim;i+=2)a[i].x=(ll)(a[i].x+0.5)-num[i/2]; ll sum=(ll)n*(n-1)*(n-2),ans=sum; ll pre=0; for(int i=0;i<=mx;i++)//mx { pre+=3*(ll)(a[i].x+0.5); if(num[i])ans-=num[i]*pre;//num[i]*...! } printf("%.7f\n",1.0*ans/sum); } return 0; }