【题目分析】
一堆小木棍,问取出三根能组成三角形的概率是多少。
Kuangbin的博客中讲的很详细。
构造一个多项式 ai=i的个数。
然后卷积之后去重。
统计也需要去重。
挺麻烦的一道题。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 500005
#define db double
#define ll long long
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
}
int Getint()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
struct Complex{
double x,y;
Complex operator + (Complex a) { Complex ret; ret.x=x+a.x; ret.y=y+a.y; return ret;};
Complex operator - (Complex a) { Complex ret; ret.x=x-a.x; ret.y=y-a.y; return ret;};
Complex operator * (Complex a) { Complex ret; ret.x=x*a.x-y*a.y; ret.y=x*a.y+y*a.x; return ret;};
}a[maxn];
ll rev[maxn],n,m,len,T,b[maxn],sum;
ll pre_sum[maxn],cnt;
const double pi=acos(-1.0);
void FFT(Complex * x,int n,int f)
{
F(i,0,n-1) if (rev[i]>i) swap(x[rev[i]],x[i]);
for (int m=2;m<=n;m<<=1)
{
Complex wn;
wn.x=cos(2.0*pi/m*f); wn.y=sin(2.0*pi/m*f);
for (int i=0;i<n;i+=m)
{
Complex w;
w.x=1; w.y=0;
for (int j=0;j<(m>>1);++j)
{
Complex u=x[i+j],v=x[i+j+(m>>1)]*w;
x[i+j]=u+v; x[i+j+(m>>1)]=u-v;
w=w*wn;
}
}
}
}
bool cmp(int a,int b){return a<b;}
int main()
{
Finout();T=Getint();
while (T--)
{
memset(a,0,sizeof a);
cnt=0;
sum=n=Getint();
F(i,1,n) b[i]=Getint(),a[b[i]].x+=1;
sort(b+1,b+sum+1,cmp);
m=1,len=0;n=b[sum]*2+1;
while (m<=n) m<<=1,len++; n=m;
F(i,0,n-1)
{
int t=i,r=0;
F(j,1,len) r<<=1,r|=t&1,t>>=1;
rev[i]=r;
}
FFT(a,n,1); F(i,0,n-1) a[i]=a[i]*a[i];
FFT(a,n,-1);
F(i,0,n-1) a[i].x=a[i].x/n;
F(i,1,sum) a[b[i]<<1].x-=1;
F(i,0,n-1) a[i].x/=2;
pre_sum[0]=a[0].x+0.5;
F(i,1,n-1) pre_sum[i]=pre_sum[i-1]+a[i].x+0.5;
F(i,1,sum)
{
cnt+=pre_sum[n-1]-pre_sum[b[i]];
cnt-=(ll)(i-1)*(sum-i);
cnt-=(ll)(sum-1);
cnt-=(ll)(sum-i)*(sum-i-1)/2;
}
ll tot=(ll)sum*(sum-1)*(sum-2)/6;
printf("%.7f\n",(db)cnt/tot);
}
}