# [BZOJ3513][MUTC2013]idiots（FFT+组合数学）

## 代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
#define N 300005

const double pi=acos(-1.0);
struct complex
{
double x,y;
complex(double X=0,double Y=0)
{
x=X,y=Y;
}
}a[N];
complex operator + (complex a,complex b){return complex(a.x+b.x,a.y+b.y);}
complex operator - (complex a,complex b){return complex(a.x-b.x,a.y-b.y);}
complex operator * (complex a,complex b){return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}

int T,cnt,Max,m,n,L,R[N];
int f[N],g[N];
LL up,down,F[N];

void clear()
{
cnt=Max=m=n=L=0;up=down=0;
memset(R,0,sizeof(R));memset(f,0,sizeof(f));memset(g,0,sizeof(g));memset(F,0,sizeof(F));
for (int i=0;i<=300000;++i) a[i]=complex(0,0);
}
void FFT(complex a[N],int opt)
{
for (int i=0;i<n;++i)
if (i<R[i]) swap(a[i],a[R[i]]);
for (int k=1;k<n;k<<=1)
{
complex wn=complex(cos(pi/k),opt*sin(pi/k));
for (int i=0;i<n;i+=(k<<1))
{
complex w=complex(1,0);
for (int j=0;j<k;++j,w=w*wn)
{
complex x=a[i+j],y=w*a[i+j+k];
a[i+j]=x+y,a[i+j+k]=x-y;
}
}
}
}
int main()
{
scanf("%d",&T);
while (T--)
{
clear();
scanf("%d",&cnt);
for (int i=1;i<=cnt;++i)
{
int x;scanf("%d",&x);
++a[x].x;++f[x];
Max=max(Max,x);
}
for (int i=Max;i>=1;--i) g[i]=g[i+1]+f[i];
m=Max<<1;
for (n=1;n<=m;n<<=1) ++L;
for (int i=0;i<n;++i)
R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
FFT(a,1);
for (int i=0;i<=n;++i) a[i]=a[i]*a[i];
FFT(a,-1);
for (int i=0;i<=n;++i)
{
F[i]=(LL)(a[i].x/n+0.5);
if (!(i&1))
F[i]-=(LL)f[i>>1];
F[i]>>=1;
}
up=down=(LL)cnt*(cnt-1)*(cnt-2)/6;
for (int i=0;i<=n;++i) up-=F[i]*(LL)g[i];
printf("%.7lf\n",(double)up/(double)down);
}
}