给你n个木棍,长度都是10W以内,问你选三根构成三角形的概率
数据范围小的话应该有各种n2的姿势
但是现在给10W,考虑母函数,长度作为指数,系数是这个长度的个数
然后先考虑任选两根,能组合出的长度有多少种
两个相同的多项式做FFT,对于后来的长度,然后要去掉同一根选两次的
然后选1和2,和选2和1,是同一种,方案数还得除以2,然后求个系数的前缀和
然后现在有了所有两根木棍组合的方案数,考虑枚举最长的木棍是i
然后另外两根加起来要大于i,所以是sum[2∗maxn]−sum[i]
然后要去重啊,因为i是三根里最长的,可能一根选择的比他大,一根比他小,或者两根都比他大,或者其中一根选了这根自己
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 100010
#define MAXN 1000005
#define maxnode 5
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
//const double PI = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
const double PI = acos(-1.0);
struct Complex {
double real, image;
Complex(double _real, double _image) {
real = _real;
image = _image;
}
Complex(){}
};
Complex operator + (const Complex &c1, const Complex &c2) {
return Complex(c1.real + c2.real, c1.image + c2.image);
}
Complex operator - (const Complex &c1, const Complex &c2) {
return Complex(c1.real - c2.real, c1.image - c2.image);
}
Complex operator * (const Complex &c1, const Complex &c2) {
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}
int rev(int id, int len) {
int ret = 0;
for(int i = 0; (1 << i) < len; i++) {
ret <<= 1;
if(id & (1 << i)) ret |= 1;
}
return ret;
}
Complex A[280000]; /////////大小要修改,不要忘记
void FFT(Complex* a, int len, int DFT){//对a进行DFT或者逆DFT, 结果存在a当中
//Complex* A = new Complex[len]; 这么写会爆栈
for(int i = 0; i < len; i++)
A[rev(i, len)] = a[i];
for(int s = 1; (1 << s) <= len; s++) {
int m = (1 << s);
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
for(int k = 0; k < len; k += m) {
Complex w = Complex(1, 0);
for(int j = 0; j < (m >> 1); j++) {
Complex t = w*A[k + j + (m >> 1)];
Complex u = A[k + j];
A[k + j] = u + t;
A[k + j + (m >> 1)] = u - t;
w = w*wm;
}
}
}
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
for(int i = 0; i < len; i++) a[i] = A[i];
return;
}
Complex a[280000], b[280000];//对应的乘积的长度不会超过100000, 也就是不超过(1 << 17) = 131072
int num[MAX];
int x[MAX];
LL y[200005];
LL sum[200005];
int main(){
//freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
mem(num,0);
int maxn=0;
for(int i=0;iscanf("%d",&x[i]);
num[x[i]]++;
maxn=max(maxn,x[i]);
}
int sa=0;
while((1<1) sa++;
int len=(1<<(sa+1));
for(int i=0;iif(i<=maxn){
a[i]=Complex(num[i],0);
b[i]=Complex(num[i],0);
}
else{
a[i]=Complex(0,0);
b[i]=Complex(0,0);
}
}
FFT(a,len,1);
FFT(b,len,1);
for(int i=0;i1);
for(int i=0;i<=2*maxn;i++) y[i]=floor(a[i].real+0.5);
for(int i=0;i<=2*maxn;i+=2) y[i]-=num[i/2];
for(int i=0;i<=2*maxn;i++) y[i]/=2;
sum[0]=y[0];
for(int i=1;i<=2*maxn;i++) sum[i]=sum[i-1]+y[i];
LL ans=0;
sort(x,x+n);
for(int i=0;i2*maxn]-sum[x[i]];
ans-=(LL)(n-i-1)*i;
ans-=(LL)(n-i-1)*(n-1-i-1)/2;
ans-=n-1;
}
double ret=ans*6.0/n/(n-1)/(n-2);
printf("%.7f\n",ret);
}
return 0;
}