码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4609 3-idiots

时间:2015-08-12 23:29:09      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

FFT

 

代码

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<complex>
 7 using namespace std;
 8 const int N = 400005;
 9 const double PI = acos(-1.0);
10 typedef complex<double> Comp;
11 Comp a[N],b[N];
12 int len,l,n,i,v,c[N];
13 long long ans[N];
14 double Ans,nn;
15 void Rader(Comp *F,int len)
16 {
17     int j = len >> 1;
18     for (int i=1;i<len-1;i++)
19     {
20         if (i<j) swap(F[i],F[j]);
21         int k = len>>1;
22         while (j >= k)
23         {
24             j -= k;
25             k >>= 1;
26         }
27         if (j < k) j += k;
28     }
29 }
30 void FFT(Comp *F,int len,int on)
31 {
32     Rader(F,len);
33     for (int h=2;h<=len;h<<=1)
34     {
35         Comp wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
36         for (int j=0;j<len;j+=h)
37         {
38             Comp w(1,0);
39             for (int k=j;k<j+h/2;k++)
40             {
41                 Comp u = F[k];
42                 Comp t = w*F[k + h / 2];
43                 F[k] = u + t;
44                 F[k + h / 2] = u - t;
45                 w = w * wn;
46             }
47         }
48     }
49     if (on == -1)
50         for (int i=0;i<len;i++)
51             F[i]/=len;
52 }
53 void Conv(Comp *a,Comp *b,int len)
54 {
55     FFT(a,len,1);
56     FFT(b,len,1);
57     for (int i=0;i<len;i++)
58         a[i] =a[i]*b[i];
59     FFT(a,len,-1);
60     for (int i=0;i<len;i++)
61     ans[i]=a[i].real()+0.5;
62 }
63 int main()
64 {
65     int test;
66     scanf("%d",&test);
67     while (test)
68     {
69     test--;
70     scanf("%d",&n);
71     l=0;
72     memset(a,0,sizeof(a));
73     memset(b,0,sizeof(b));
74     for (i=1;i<=n;i++)
75     {
76         scanf("%d",&v);
77         c[i]=v;
78         int q=a[v].real()+1;
79         a[v].real(q);
80         b[v].real(q);
81         l=max(l,v);
82     }
83     len=1;
84     while (len<=l*2) len*=2;
85     Conv(a,b,len);
86     for (i=1;i<=n;i++)
87     ans[c[i]+c[i]]--;
88     for (i=0;i<len;i++)
89     ans[i]/=2;
90     for (i=1;i<len;i++)
91         ans[i]=ans[i-1]+ans[i];
92     sort(c+1,c+1+n);
93     nn=n;
94     Ans=0;
95     for (i=1;i<=n;i++)
96     Ans+=(ans[len-1]-ans[c[i]]-(nn-1)-(nn-i)*(i-1)-(nn-i)*(nn-i-1)/2);
97     printf("%.7lf\n",Ans/(nn*(nn-1)*(nn-2)/6));
98     }
99 }

 

hdu4609 3-idiots

标签:

原文地址:http://www.cnblogs.com/fzmh/p/4725729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!