zoukankan      html  css  js  c++  java
  • HDU4609 3-idiots

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    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
     
    Source
     
    Recommend
    liuyiding
     

    卷积可以算出两个数组中各取一个数,可以得到某数值的方案数。

    不能取两遍自己,且“取x再取y”和“取y再取x”等价,需要去重。

    去重之后,枚举第三条边,减去不符合要求的方案数,最终得到符合要求的方案数。

    ↑FFT是用来优化求卷积过程的。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<complex>
     6 #include<cmath>
     7 #define pi acos(-1)
     8 #define LL long long
     9 using namespace std;
    10 const int mxn=100010*3;
    11 int read(){
    12     int x=0,f=1;char ch=getchar();
    13     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    14     while(ch>='0' && ch<='9'){x=x*10-'0'+ch;ch=getchar();}
    15     return x*f;
    16 }
    17 struct com{
    18     double a,b;
    19     com operator + (com &rhs){return (com){a+rhs.a,b+rhs.b};}
    20     com operator - (com &rhs){return (com){a-rhs.a,b-rhs.b};}
    21     com operator * (com &rhs){
    22         return (com){a*rhs.a-b*rhs.b,a*rhs.b+b*rhs.a};
    23     }
    24 };
    25 int n,m,L;
    26 LL ans=0;
    27 com a[mxn],b[mxn];
    28 int rev[mxn];
    29 void FFT(com *a,int flag){
    30     int i,j;
    31     for(i=0;i<n;i++)if(rev[i]>i)swap(a[i],a[rev[i]]);
    32     for(i=1;i<n;i<<=1){
    33         com wn=(com){cos(pi/i),flag*sin(pi/i)};
    34         for(j=0;j<n;j+=(i<<1)){
    35             com w=(com){1,0};
    36             for(int k=0;k<i;k++,w=w*wn){
    37                 com x=a[j+k],y=w*a[j+k+i];
    38                 a[j+k]=x+y;
    39                 a[j+k+i]=x-y;
    40             }
    41         }
    42     }
    43     if(flag==-1)for(i=0;i<n;i++) a[i].a/=n;
    44     return;
    45 }
    46 int c[mxn];
    47 LL cnt[mxn];
    48 int main(){
    49     int i,j;
    50     int T=read();
    51     
    52     while(T--){
    53         n=read();
    54         for(i=0;i<n;i++) c[i]=read();
    55         sort(c,c+n);
    56         m=c[n-1]<<1;int tmp=n;L=0;
    57         for(n=1;n<=m;n<<=1)L++;//
    58         for(i=0;i<n;i++)
    59             rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1));
    60         memset(a,0,sizeof a);    memset(b,0,sizeof b);//
    61         memset(cnt,0,sizeof cnt);
    62         for(i=0;i<tmp;i++)cnt[c[i]]++;
    63         for(i=0;i<=c[tmp-1];i++) a[i].a=cnt[i];
    64         FFT(a,1);
    65         for(i=0;i<n;i++) a[i]=a[i]*a[i];//自乘 
    66         FFT(a,-1);
    67         for(i=0;i<n;i++) cnt[i]=(int)(a[i].a+0.5);
    68         for(i=0;i<tmp;i++) cnt[c[i]+c[i]]--;
    69         for(i=0;i<n;i++) cnt[i]/=2;
    70         LL res=(LL)tmp*(tmp-1)*(tmp-2)/6;
    71         ans=res;
    72         for(int k=0,i=0;i<n;i++){
    73             if(cnt[i]){
    74                 while(k<tmp && c[k]<i)k++;
    75                 if(k==tmp)break;
    76                 ans-=cnt[i]*(tmp-k);
    77             }
    78         }
    79         printf("%.7f
    ",(double)ans/res);
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    JMeter——请求元件——配置元件——参数化——用户自定义变量
    JMeter——结合fiddler查看响应结果
    JMeter——断言——xpath Assertion
    JMeter——断言——响应断言
    JMeter——配置元件——http信息头管理器使用
    JMeter——查看结果树——html使用
    JMeter——查看结果树 ——css_jquery_tester(css选择器测试)
    JMeter——查看结果树
    JMeter——http请求默认值
    java.lang.RuntimeException: Cannot reconnect.
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6359027.html
Copyright © 2011-2022 走看看