[BZOJ3513][MUTC2013]idiots(FFT+组合数学)

版权声明:转载请注明出处:http://blog.csdn.net/clove_unique https://blog.csdn.net/Clove_unique/article/details/71171163

题目描述

传送门

题目大意:给定n个长度分别为ai的木棒,问随机选择3个木棒能够拼成三角形的概率。

题解

这道题要根据三角形的两边之和大于第三边来做,其实判定的条件就是随机选出来的三条边中较小的两条之和大于第三条边
首先容斥一下,用总的方案数减去不合法的方案数
令F(i)表示两条边和为i的方案数,a(i)表示长为i的木棒有多少个,那么可以写成一个卷积的形式F(i)=a(ij)a(j),注意求出卷积了之后真正的F(i)还需要去掉选了同一条边(i为偶数),并且/2(因为ab和ba会算两遍)
然后g(i)表示长度大于等于i的有多少个,那么不合法的数量就是F(i)g(i)
计算概率就行了

代码

#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);
    }
}

没有更多推荐了,返回首页