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≤10 5).
The following line contains N integers a_i (1≤a_i≤10 5), 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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的小工具;种子转磁力链接的

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值