[HDU4609]3-idiots(生成函数+FFT)

3-idiots

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

 

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

代码用时:3h

比较裸的生成函数应用。理清容斥关系就好。

应为一个非常低级的错误(复数减法运算符重载出错),调了非常长的时间。

以后还是应该尽量自己写程序以免受别人程序干扰,FFT和复数运算模板要熟练。

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #define rep(i,l,r) for (int i=l; i<=r; i++)
 6 #define mem(a) memset(a,0,sizeof(a))
 7 typedef long long ll;
 8 using namespace std;
 9 
10 const int N=(1<<18)+100;
11 const double pi=acos(-1.);
12 int T,n,nn,m,q[N],rev[N];
13 ll s[N];
14 
15 struct C{
16    double x,y;
17    C (double a=0,double b=0):x(a),y(b){}
18 }a[N];
19 C operator +(C a,C b){ return C(a.x+b.x,a.y+b.y); }
20 C operator -(C a,C b){ return C(a.x-b.x,a.y-b.y); }
21 C operator *(C a,C b){ return C(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x); }
22 
23 void DFT(C a[],int f){
24    for (int i=0;i<nn;i++)
25       if (i<rev[i]) swap(a[i],a[rev[i]]);
26    for (int i=1; i<nn; i<<=1){
27       C wn(cos(pi/i),f*sin(pi/i));
28       for (int p=i<<1,j=0; j<nn; j+=p){
29          C w(1,0);
30          for (int k=0; k<i; k++,w=w*wn){
31             C x=a[j+k],y=w*a[i+j+k]; a[j+k]=x+y; a[i+j+k]=x-y;
32          }
33       }
34    }
35    if (f==-1) rep(i,0,nn-1) a[i].x/=nn;
36 }
37 
38 int main(){
39    freopen("hdu4609.in","r",stdin);
40    freopen("hdu4609.out","w",stdout);
41    scanf("%d",&T);
42    while (T--){
43       m=0; mem(a); mem(q); mem(s); mem(rev); scanf("%d",&n);
44       rep(i,1,n) scanf("%d",&q[i]),m=max(m,q[i]);
45       rep(i,1,n) a[q[i]].x++;
46       m<<=1; int L=0; for (nn=1; nn<=m; nn<<=1) L++;
47       rep(i,0,nn-1) rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1));
48       DFT(a,1); rep(i,0,nn-1) a[i]=a[i]*a[i]; DFT(a,-1);
49       rep(i,1,m) s[i]=(ll)(a[i].x+0.5);
50       rep(i,1,n) s[q[i]<<1]--;
51       rep(i,1,m) s[i]=s[i-1]+(s[i]>>1);
52       sort(q+1,q+n+1); ll ans=0,tot=1ll*n*(n-1)*(n-2)/6;
53       rep(i,1,n) ans+=1ll*s[m]-s[q[i]]-1ll*(n-i+1)*(n-1)+1ll*(n-i+1)*(n-i)/2;
54       printf("%.7lf\n",(double)ans/tot);
55    }
56    return 0;
57 }

 

posted @ 2018-01-11 21:35  HocRiser  阅读(475)  评论(0编辑  收藏  举报