hdu 4609 3-idiots (FFT+計數)

3-idiots

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


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≤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
 


題意 :給出n條邊,問選出三條邊能組成三角形的概率。

第一次搞FFT,瞭解了下卷積,具體實現是借鑑了別人的代碼。

用num[i]表示長度爲i的出現幾次。對於樣例1 3 3 4,我們得到num={0,1,0,2,1},

num數組和num數組卷積的含義:就是從{1 3 3 4}取一個數,從{1 3 3 4}再取一個

數,他們的和每個值各有多少個?

即{0 1 0 2 1}*{0 1 0 2 1} 卷積的結果應該是{0 0  1  0  4  2  4  4  1 }。

這個結果的意義如下:

從{1 3 3 4}取一個數,從{1 3 3 4}再取一個數

取兩個數和爲 2 的取法是一種:1+1

           和爲 4 的取法有四種:1+3, 1+3  ,3+1 ,3+1

           和爲 5 的取法有兩種:1+4 ,4+1;

           和爲 6的取法有四種:3+3,3+3,3+3,3+3,3+3

           和爲 7 的取法有四種: 3+4,3+4,4+3,4+3

           和爲 8 的取法有 一種:4+4


<span style="font-size:14px;">#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define ll long long
using namespace std;
const double pi=acos(-1.0);
const int maxn=100010;

struct Complex
{
    double a,b;
    Complex(){}
    Complex(double _a,double _b):a(_a),b(_b){}
    Complex operator + (const Complex &p)
    {
        return Complex(a+p.a,b+p.b);
    }
    Complex operator - (const Complex &p)
    {
        return Complex(a-p.a,b-p.b);
    }
    Complex operator * (const Complex &p)
    {
        return Complex(a*p.a-b*p.b,a*p.b+b*p.a);
    }
}s[maxn*4];

int n,len,cnt,a[maxn*4];
ll num[maxn*4],sum[maxn*4];

void initial()
{
    len=1;
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
}

void input()
{
    int co;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        num[a[i]]++;
    }
}

void ready()
{
    sort(a,a+n);
    int Max=a[n-1]+1;
    while(len<2*Max)  len<<=1;
    for(int i=0;i<Max;i++)    s[i]=Complex(num[i],0);
    for(int i=Max;i<len;i++)  s[i]=Complex(0,0);
}

void change()
{
     for(int i=1,j=len/2;i<len-1;i++)
     {
         if(i<j)  swap(s[i],s[j]);
         int k=len/2;
         while(j>=k)
         {
             j-=k;
             k/=2;
         }
         if(j<k)  j+=k;
     }
}

void FFT(int on)
{
    change();
    for(int i=2;i<=len;i<<=1)
    {
        Complex wn=Complex(cos(-on*2*pi/i),sin(-on*2*pi/i));
        for(int j=0;j<len;j+=i)
        {
            Complex w(1,0);
            for(int k=j;k<j+i/2;k++)
            {
                Complex u=s[k];
                Complex t=w*s[k+i/2];
                s[k]=u+t;
                s[k+i/2]=u-t;
                w=w*wn;
            }
        }
    }
    if(on==-1)
    {
        for(int i=0;i<len;i++)
            s[i].a/=len;
    }
}

void deal()
{
    FFT(1);
    for(int i=0;i<len;i++)  s[i]=s[i]*s[i];
    FFT(-1);
    cnt=2*a[n-1];
    for(int i=0;i<=cnt;i++)  num[i]=(ll)(s[i].a+0.5);
}

void solve()
{
    ll ans=0;
    for(int i=0;i<n;i++)    num[a[i]+a[i]]--;          //  減去兩次取相同的
    for(int i=1;i<=cnt;i++) num[i]/=2;                //   對於兩次去不同的算了兩邊,去重
    for(int i=1;i<=cnt;i++)  sum[i]=sum[i-1]+num[i];

    for(int i=0;i<n;i++)
    {
         ll t=sum[cnt]-sum[a[i]];
         ll b=n-1;                     //  減去與a[i]組合的
         ll c=(ll)i*(n-i-1);           //  減去一大一小組合的
         ll d=(ll)(n-i-1)*(n-i-2)/2;   //  減去兩大組合的
         ans+=t-b-c-d;
    }
    ll mul=(ll)(n)*(n-1)*(n-2)/6;
    printf("%.7lf\n",ans*1.0/mul);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        initial();
        input();
        ready();
        deal();
        solve();
    }
    return 0;
}</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章