很难和FFT联系在一起。。。网上百度题解做的。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 400005
#define maxm 1000005
#define eps 1e-10
#define mod 1000000007
#define INF 999999999
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head
struct complex
{
double r, i;
complex(double r = 0, double i = 0) : r(r), i(i) {}
complex operator + (const complex b) const {
return complex(r + b.r, i + b.i);
}
complex operator - (const complex b) const {
return complex(r - b.r, i - b.i);
}
complex operator * (const complex b) const {
return complex(r * b.r - i * b.i, r * b.i + i * b.r);
}
}x[maxn];
void chang(complex y[], int len)
{
for(int i = 1, j = len / 2; i < len-1; i++) {
if(i < j) swap(y[i], y[j]);
int k = len / 2;
while(j >= k) {
j -= k;
k >>= 1;
}
j += k;
}
}
void fft(complex y[], int len, int on)
{
chang(y, len);
for(int i = 2; i <= len; i <<= 1) {
complex wn = complex(cos(-on * 2 * PI / i), sin(-on * 2 * PI / i));
for(int j = 0; j < len; j += i) {
complex w = complex(1.0, 0.0);
for(int k = j; k < j + i / 2; k++) {
complex u = y[k];
complex t = w * y[k + i / 2];
y[k] = u + t;
y[k + i / 2] = u - t;
w = w * wn;
}
}
}
if(on == -1) for(int i = 0; i < len; i++) y[i].r /= len;
}
int num[maxn], c[maxn];
LL sum[maxn];
int mx, n;
int cmp(int a, int b)
{
return a < b;
}
void init(void)
{
memset(c, 0, sizeof c);
}
void read(void)
{
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &num[i]);
sort(num, num+n, cmp);
for(int i = 0; i < n; i++) c[num[i]]++;
mx = num[n-1] + 1;
}
void work(void)
{
int len = 1;
while(len < 2 * mx) len <<= 1;
for(int i = 0; i < len; i++) x[i] = complex(c[i], 0.0);
fft(x, len, 1);
for(int i = 0; i < len; i++) x[i] = x[i] * x[i];
fft(x, len, -1);
for(int i = 0; i < len; i++) sum[i] = (LL)(x[i].r + 0.5);
for(int i = 0; i < n; i++) sum[num[i] + num[i]]--;
for(int i = 0; i < len; i++) sum[i] /= 2;
for(int i = 1; i < len; i++) sum[i] += sum[i-1];
LL ans = 0;
for(int i = 0; i < n; i++) {
ans += sum[len-1] - sum[num[i]];
ans -= (LL)(n - i - 1) * i;
ans -= (LL)(n-1);
ans -= (LL)(n - i - 1) * (n - i - 2) / 2;
}
double res = 1.0 * ans / n / (n - 1) / (n - 2) * 6;
printf("%.7f\n", res);
}
int main(void)
{
int _;
while(scanf("%d", &_)!=EOF) {
while(_--) {
init();
read();
work();
}
}
return 0;
}