思路:
http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 其实我是懒得写了....
一定要define int long long……(否则不知道自己怎么死的别怪我..)
有用C++写好的虚数 的版本 (是慢一些)
(写完本地编译过了 交上去各种CE)
哦 还有.. 不要每回都搞1<<18个初始量,,,,,,, 会T到死的 QAQ
100组数据 ..
每回找最大值就好啦
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <complex>
#include <algorithm>
using namespace std;
#define int long long
const int N=270000;
const double pi=acos(-1);
int n,L,maxx,cases,m,a[N],R[N],cnt,sum[N];
typedef complex<double> cplxd;
cplxd num[N],jy(1,0);
void FFT(cplxd *a,int f){
for(int i=0;i<n;i++)if(i<R[i])swap(a[i],a[R[i]]);
for(int i=1;i<n;i<<=1){
cplxd wn(cos(pi/i),f*sin(pi/i));
for(int j=0;j<n;j+=(i<<1)){
cplxd w(1,0);
for(int k=0;k<i;k++,w=w*wn){
cplxd x=a[j+k],y=w*a[j+k+i];
a[j+k]=x+y,a[j+k+i]=x-y;
}
}
}
if(!~f)for(int i=0;i<n;i++)a[i]/=n;
}
signed main(){
scanf("%lld",&cases);
while(cases--){
memset(sum,0,sizeof(sum)),memset(num,0,sizeof(num));
scanf("%lld",&m),cnt=0,maxx=L=0;
for(int i=1;i<=m;i++)scanf("%lld",&a[i]),num[a[i]]+=jy,maxx=max(maxx,a[i]);
for(n=1;n<=maxx*2+1;n<<=1)L++;
for(int i=1;i<=n;i++)R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
FFT(num,1);for(int i=0;i<n;i++)num[i]=num[i]*num[i];FFT(num,-1);
sort(a+1,a+1+m);
for(int i=1;i<n;i++)sum[i]=(num[i].real()+0.1);
for(int i=1;i<=m;i++)sum[a[i]+a[i]]-=1;
for(int i=1;i<n;i++)sum[i]/=2,sum[i]+=sum[i-1];
for(int i=1;i<=m;i++)
cnt+=sum[n-1]-sum[a[i]]-(0ll+m-i)*(i-1)-(m-i)*(m-i-1)/2-m+1;
printf("%.7lf\n",(double)cnt*6.0/(m*(m-1)*(m-2)));
}
}
手写complex的:
//By SiriusRen
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int N=270000;
const double pi=acos(-1);
int n,L,maxx,cases,m,a[N],R[N],cnt,sum[N];
struct cplxd{
double x,y;
cplxd(double a,double b){x=a,y=b;}
cplxd(){}
};
cplxd operator*(cplxd a,cplxd b){cplxd c;c.x=a.x*b.x-a.y*b.y;c.y=a.x*b.y+b.x*a.y;return c;}
cplxd operator+(cplxd a,cplxd b){cplxd c;c.x=a.x+b.x;c.y=a.y+b.y;return c;}
cplxd operator-(cplxd a,cplxd b){cplxd c;c.x=a.x-b.x;c.y=a.y-b.y;return c;}
cplxd operator/(cplxd a,int b){cplxd c;c.x=a.x/n;c.y=a.y/n;return c;}
cplxd num[N];
void FFT(cplxd *a,int f){
for(int i=0;i<n;i++)if(i<R[i])swap(a[i],a[R[i]]);
for(int i=1;i<n;i<<=1){
cplxd wn(cos(pi/i),f*sin(pi/i));
for(int j=0;j<n;j+=(i<<1)){
cplxd w(1,0);
for(int k=0;k<i;k++,w=w*wn){
cplxd x=a[j+k],y=w*a[j+k+i];
a[j+k]=x+y,a[j+k+i]=x-y;
}
}
}
if(!~f)for(int i=0;i<n;i++)a[i]=a[i]/n;
}
signed main(){
scanf("%lld",&cases);
while(cases--){
memset(sum,0,sizeof(sum));
scanf("%lld",&m),cnt=0,maxx=L=0;
for(int i=1;i<=m;i++)scanf("%lld",&a[i]),num[a[i]].x+=1.0,maxx=max(maxx,a[i]);
for(n=1;n<=maxx*2+1;n<<=1)L++;
for(int i=1;i<=n;i++)R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
FFT(num,1);for(int i=0;i<n;i++)num[i]=num[i]*num[i];FFT(num,-1);
sort(a+1,a+1+m);
for(int i=1;i<n;i++)sum[i]=(num[i].x+0.1);
for(int i=1;i<=m;i++)sum[a[i]+a[i]]-=1;
for(int i=1;i<n;i++)sum[i]/=2,sum[i]+=sum[i-1];
for(int i=1;i<=m;i++)
cnt+=sum[n-1]-sum[a[i]]-(0ll+m-i)*(i-1)-(m-i)*(m-i-1)/2-m+1;
printf("%.7lf\n",(double)cnt*6.0/(m*(m-1)*(m-2)));
for(int i=0;i<n;i++)num[i].x=num[i].y=0;
}
}