3-idiots HDU - 4609 (FFT +生成函数)

大致题意

给n条线段ai,询问不重复的选择3条(i!=j,i!=k,j!=k)线段,能凑成三角形 的概率是多少。(3<=n<=100000,1<=ai<=100000)

思路

和那道Triple Sums容斥的类似,先选择两个,A(x)*A(x)-B(x ^2)得到不重复选择两个数得到的各种数的方式,然后求一个前缀和sum[i],然后考虑反过来求,求出不满足三角形的个数,枚举三角形最大的边ai,不满足的就是
sum[a[i]],所以cnt+=sum[a[i]]; 所以最终答案是1.0-(cnt/tot)

代码

重点还是要明确哪些需要卷积,哪些可以直接在实数域计算得到。比如使用两个相同的可以直接用cnt[a[i]*2]++;不需要卷积。

#include<bits/stdc++.h>
using namespace std;
#define maxn 400010
#define maxm 1000006
#define ll long long int
#define INF 0x3f3f3f3f
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) (x*x)
#define inf (ll)2e18+1
#define PI acos(-1)
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
     return f*x;
}
struct Complex
{
    double x, y;    //  实部和虚部 x + yi
    Complex(double _x = 0.0, double _y = 0.0){x = _x;y = _y;}
    Complex operator - (const Complex &b) const{return Complex(x - b.x, y - b.y);}
    Complex operator + (const Complex &b) const{return Complex(x + b.x, y + b.y);}
    Complex operator * (const Complex &b) const{return Complex(x * b.x - y * b.y, x * b.y + y * b.x);}
};
void change(Complex y[], int len)
{
    int i,j,k;
    for(i=1,j=len/2;i<len-1;i++){
        if(i<j)swap(y[i],y[j]);
        k=len/2;
        while (j>=k){j-=k;k/=2;}
        if (j<k)j+=k;
    }
    return ;
}
void fft(Complex y[], int len, int on)
{
    change(y,len);
    for(int h=2;h<=len;h<<=1){
        Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
        for(int j=0;j<len;j+=h){
            Complex w(1,0);
            for(int k=j;k<j+h/2;k++){
                Complex u=y[k];
                Complex t=w*y[k+h/2];
                y[k]=u+t;
                y[k+h/2]=u-t;
                w=w*wn;
            }
        }
    }
    if(on==-1)for(int i=0;i<len;i++)y[i].x/=len;
}
int t,n,a[maxn/4],len;
Complex x1[maxn];
ll cnt[maxn],sum[maxn];
int main()
{
    t=read();
    while(t--){
        n=read();
        mem(cnt);
        inc(i,1,n){a[i]=read();cnt[a[i]]++;}
        sort(a+1,a+n+1);
        int len1=a[n];
        len=1;
        while(len<2*len1)len<<=1;
        inc(i,0,len1)x1[i]=Complex(cnt[i],0);
        inc(i,len1+1,len-1)x1[i]=Complex(0,0);
        fft(x1,len,1);
        inc(i,0,len-1)x1[i]=x1[i]*x1[i];
        fft(x1,len,-1);
        inc(i,0,len-1)cnt[i]=(ll)(x1[i].x+0.5);
        inc(i,1,n)cnt[a[i]*2]--;
        len=2*a[n];
        inc(i,1,len)cnt[i]/=2;
        inc(i,1,len)sum[i]=sum[i-1]+cnt[i];
        ll ans=0,tot=1ll*n*(n-1)*(n-2)/6;
        inc(i,1,n)ans+=sum[a[i]];
        printf("%.7lf\n",1.0-1.0*ans/tot);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值