HDU 4609 3-idiots (FFT)

3-idiots

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6517 Accepted Submission(s): 2269

Problem 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 ≤ 1 0 5 ) (3≤N≤10^5) (3N105).
The following line contains N integers a_i ( 1 ≤ a i ≤ 1 0 5 ) (1≤a_i≤10^5) (1ai105), 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

题意##

给你n根木棍的长度,任意取三根问你能组成三角形的概率

思路

因为n很大所以用常规的母函数等方法枚举肯定超时,这个时候就可以用FFT求解多项式乘积问题了
我们可以把木棍的长度作为数组下标也就是多项式的次幂,木棍长度出现的次数作为多项式的系数如第一组样例
{ 1 , 3 , 3 , 4 } \left \{ 1,3,3,4\right \} {1,3,3,4}
那么可以构成多项式
A ( x ) = x 1 + 2 x 3 + x 4 A(x)=x^1+2x^3+x^4 A(x)=x1+2x3+x4
那么 A ( x ) ∗ A ( x ) A(x)*A(x) A(x)A(x)也就是这么多根木棍中任取两根所能构成的长度
若以 B ( x ) = A ( x ) ∗ A ( x ) B(x)=A(x)*A(x) B(x)=A(x)A(x),那么多项式 B ( x ) B(x) B(x)的某个次幂的系数就代表这个这些木棍能构成这个长度的方案数

注意
上面的多项式把木棍取3,4和木棍取4,3的方案认为不同,所以所有方案数要除2
然后还要减掉自己取自己的情况

然后我们获得了一个任取两根木棍所能构成的长度的方法数,所能构成三角的情况是两边之和大于第三边,我们还得做一个前缀和,那么大于某个长度的方法数就是 s u m [ l e n ] − s u m [ i ] sum[len]-sum[i] sum[len]sum[i]

然后我们再枚举一遍木棍的长度,我们假定这次枚举的木棍的长度是三角形中最长的那一根,那么

因为新的多项式中的系数中包含的元素可能是存在有比我枚举的木棍长度大的情况,我们还得减去这些情况,枚举前把木棍排个序

  1. 系数里面包括一个比要枚举的木棍大的和一个比枚举木棍小的
    这个时候要减去这些木棍的组合就是 ( n − 1 − i ) ∗ i (n-1-i)*i (n1i)i

  2. 系数里面包括两个都大于要枚举的木棍的
    这个时候组合就是(n-1-i)*(n-2-i)/2

  3. 系数里面包括一个枚举的数和一个其他数
    这个时候要减去的就是n-1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>
#include <stack>
#define PI acos(-1)
using namespace std;
typedef long long ll;
const int MAXN = 400040;
struct complex
{
    double a, b;
    complex(double aa = 0.0, double bb = 0.0)
    {
        a = aa;
        b = bb;
    }
    complex operator +(const complex &e)
    {
        return complex(a + e.a, b + e.b);
    }
    complex operator -(const complex &e)
    {
        return complex(a - e.a, b - e.b);
    }
    complex operator *(const complex &e)
    {
        return complex(a * e.a - b * e.b, a * e.b + b * e.a);
    }
} x1[MAXN], x[MAXN];
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>>=1;
        }
        if(j < k) j += k;
    }
}
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], 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].a /= len;
}
long long a[MAXN/4];
long long ans[MAXN];
long long sum[MAXN];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long n;
        scanf("%lld",&n);
        memset(x1,0,sizeof(x1));
        memset(ans,0,sizeof(ans));
        long long maxn=0;
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&a[i]);
            x1[a[i]].a++;
            maxn=max(a[i],maxn);
        }
        sort(a,a+n);
        int len=1;
        while(len<2*maxn+2) len<<=1;
        fft(x1,len,1);
        memset(x,0,sizeof(x));
        for(int i=0; i<len; i++)
            x[i]=x1[i]*x1[i];
        fft(x,len,-1);
        for(int i=0; i<len; i++)
                ans[i]=(ll)(x[i].a+0.5);
        for(int i=0; i<n; i++)
            ans[a[i]*2]--;
        for(int i=1;i<=len;i++)
            ans[i]/=2;
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=len;i++)
            sum[i]+=sum[i-1]+ans[i];
        long long res=0;
        for(long long i=0;i<n;i++)
        {
            res+=sum[len]-sum[a[i]];
            res-=(n-1);
            res-=(n-1-i)*i;
            res-=(n-1-i)*(n-i-2)/2;
        }
        long long tot = (long long)n*(n-1)*(n-2)/6;
        printf("%.7lf\n",(double)res/tot);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值