HDU 4609(3-idiots-FFT+dp)

来源:互联网 发布:js函数返回值return 编辑:程序博客网 时间:2024/05/21 15:30

3-idiots

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


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
241 3 3 442 3 3 4
 

Sample Output
0.50000001.0000000
 

Source
2013 Multi-University Training Contest 1
 

 把边计数排序,然后ai*x^i 就表示有ai条长度=i的边

自己乘自己

然后得到的多项式,去掉(x,x) 以及 去重 (x,y),(y,x) 

现在对于每一条边z,找序号比它小的使x+y>z





#include<bits/stdc++.h>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define MEM(a) memset(a,0,sizeof(a));#define eps (1e-1)#define MAXN (500000+10)#define pi ((double)3.1415926535897932384626)typedef __int64 ll;typedef complex<double> cd;class fft { public:cd A[MAXN];int n,l;void brc(cd *A,int l) {int i,j,k;for(i=1,j=l>>1;i<l-1;i++) {if (i<j) swap(A[i],A[j]);k=l>>1;while(j>=k) {j-=k;k>>=1;} j+=k;} }void DFT(int l,int on) //on {brc(A,l);for(int h=2;h<=l;h<<=1) {cd wn=cd(cos(on*2*pi/h),sin(on*2*pi/h));for(int j=0;j<l;j+=h) {cd w=cd(1,0);for(int k=j;k<j+h/2;k++) {cd u=A[k],t=w*A[k+h/2];A[k]=u+t;A[k+h/2]=u-t;w*=wn;}}}if (on==-1) Rep(i,l) A[i]/=l;//DFT = Äæ¾ØÕó=-A/l  }void mem(int _n) {MEM(A)  n=_n;l=1; while(l<n) l<<=1; l<<=1;}void scan(ll *a,int n) {MEM(A)Rep(i,n) {A[i]=cd(a[i],0);}}}S;int a[MAXN];ll sum[MAXN],cnt[MAXN],res[MAXN];int main(){//freopen("hdu4609.in","r",stdin);//freopen(".out","w",stdout);int T;cin>>T;while(T--) {MEM(a) MEM(sum) MEM(cnt) MEM(res)int n; cin>>n;int ma=0;For(i,n) scanf("%d",&a[i]),ma=max(ma,a[i]),cnt[a[i]]++;sort(a+1,a+1+n);S.mem(++ma);S.scan(cnt,ma);S.DFT(S.l,1);Rep(i,S.l) S.A[i]*=S.A[i];S.DFT(S.l,-1);Rep(i,S.l) res[i]=(ll)(S.A[i].real()+0.1);For(i,n) res[2*a[i]]--;Rep(i,S.l) res[i]/=2;For(i,S.l-1) sum[i]=sum[i-1]+res[i];ll ans=0,tot=(ll)(n)*(n-1)*(n-2)/6;For(i,n) ans+=(ll)(i-1)*(i-2)/2-sum[a[i]];printf("%.7f\n",(double)ans/(double)tot);}return 0;}



0 0
原创粉丝点击