zoukankan      html  css  js  c++  java
  • HDU 4609 3-idiots FFT

    3-idiots

    题目连接:

    http://acm.hdu.edu.cn/showproblem.php?pid=4609

    Description

    King OMeGa catched three men who had been streaking in the street. Looking as idiots though, the three men insisted that it was a kind of performance art, and begged the king to free them. Out of hatred to the real idiots, the king wanted to check if they were lying. The three men were sent to the king's forest, and each of them was asked to pick a branch one after another. If the three branches they bring back can form a triangle, their math ability would save them. Otherwise, they would be sent into jail.
    However, the three men were exactly idiots, and what they would do is only to pick the branches randomly. Certainly, they couldn't pick the same branch - but the one with the same length as another is available. Given the lengths of all branches in the forest, determine the probability that they would be saved.

    Input

    An integer T(T≤100) will exist in the first line of input, indicating the number of test cases.
    Each test case begins with the number of branches N(3≤N≤105).
    The following line contains N integers a_i (1≤a_i≤105), which denotes the length of each branch, respectively.

    Output

    Output the probability that their branches can form a triangle, in accuracy of 7 decimal places.

    Sample Input

    2
    4
    1 3 3 4
    4
    2 3 3 4

    Sample Output

    0.5000000
    1.0000000

    Hint

    题意

    给你n个木棍,问你这些木棍我随便选3个,能够组成三角形的概率是多少

    题解:

    其实就是问能够组成三角形的种类有多少种

    FFT求出所有A+B的种类之后,再暴力枚举最大值,然后容斥算出以这个为最大值的三角形有多少个

    然后直接莽一波就完了

    代码

    #include<bits/stdc++.h>
    
    using namespace std;
    
    const int N = 600040;
    const double pi = acos(-1.0);
    
    int len;
    
    struct Complex
    {
        double r,i;
    	Complex(double r=0,double i=0):r(r),i(i) {};
    	Complex operator+(const Complex &rhs)
    	{
    		return Complex(r + rhs.r,i + rhs.i);
    	}
    	Complex operator-(const Complex &rhs)
    	{
    		return Complex(r - rhs.r,i - rhs.i);
    	}
    	Complex operator*(const Complex &rhs)
    	{
    		return Complex(r*rhs.r - i*rhs.i,i*rhs.r + r*rhs.i);
    	}
    } va[N],vb[N];
    
    void rader(Complex F[],int len)	//len = 2^M,reverse F[i] with  F[j] j为i二进制反转
    {
    	int j = len >> 1;
    	for(int i = 1;i < len - 1;++i)
    	{
    		if(i < j) swap(F[i],F[j]);	// reverse
    		int k = len>>1;
    		while(j>=k)
    		{
    			j -= k;
    			k >>= 1;
    		}
    		if(j < k) j += k;
    	}
    }
    
    void FFT(Complex F[],int len,int t)
    {
    	rader(F,len);
    	for(int h=2;h<=len;h<<=1)
    	{
    		Complex wn(cos(-t*2*pi/h),sin(-t*2*pi/h));
    		for(int j=0;j<len;j+=h)
    		{
    			Complex E(1,0);	//旋转因子
    			for(int k=j;k<j+h/2;++k)
    			{
    				Complex u = F[k];
    				Complex v = E*F[k+h/2];
    				F[k] = u+v;
    				F[k+h/2] = u-v;
    				E=E*wn;
    			}
    		}
    	}
    	if(t==-1)	//IDFT
    		for(int i=0;i<len;++i)
    			F[i].r/=len;
    }
    
    void Conv(Complex a[],Complex b[],int len) //求卷积
    {
    	FFT(a,len,1);
    	FFT(b,len,1);
    	for(int i=0;i<len;++i) a[i] = a[i]*b[i];
    	FFT(a,len,-1);
    }
    int n;
    int a[N];
    long long num[N],sum[N];
    void solve()
    {
        memset(num,0,sizeof(num));
        memset(sum,0,sizeof(sum));
        memset(va,0,sizeof(va));
        memset(vb,0,sizeof(vb));
        scanf("%d",&n);
        int Mx = 0;
        for(int i=0;i<n;i++)
        {
            int x;scanf("%d",&a[i]);
            Mx = max(Mx,a[i]);
            num[a[i]]++;
        }
        Mx*=2;
        len=1;
        while(len<=Mx+1)len*=2;
        sort(a,a+n);
        for(int i=0;i<=len;i++)
        {
            va[i].r=num[i];
            va[i].i=0;
            vb[i].r=va[i].r;
            vb[i].i=0;
        }
        Conv(va,vb,len);
        for(int i=0;i<len;i++)
            num[i]=(long long)(va[i].r+0.5);
        for(int i=0;i<n;i++)
            num[a[i]+a[i]]--;
        for(int i=0;i<len;i++)
            num[i]/=2LL;
        sum[0]=0;
        for(int i=1;i<=len;i++)
            sum[i]=sum[i-1]+num[i];
        long long cnt = 0;
        for(int i=0;i<n;i++)
        {
            cnt += sum[len]-sum[a[i]];
            cnt -= (long long)(n-1-i)*i;
            cnt -= (n-1);
            cnt -= (long long)(n-1-i)*(n-i-2)/2;
        }
        long long all = (long long)n*(n-1)*(n-2)/6;
        printf("%.7f
    ",(double)cnt/all);
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)solve();
    	return 0;
    }
  • 相关阅读:
    对Postmaster 对 SIGQUIT 信号处理的理解
    kill bgwriter 的小实验
    我对 execl 的学习
    pid_t 数据类型
    我对bgwriter.c 与 guc 关系的初步理解
    对Linux 下 SIGUSR1 与 SIGUSR2 的理解
    向bgwriter 发送 SIGQUIT 的实验
    常用txt图标符号
    也做网页图标favicon.ico
    TakeColor颜色拾取工具
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5379918.html
Copyright © 2011-2022 走看看