您的位置:首页 > 大数据 > 物联网

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

2018-01-11 21:35 369 查看

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 2013 Multi-University Training Contest 1    Recommend liuyiding

代码用时:3h

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

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

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

#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rep(i,l,r) for (int i=l; i<=r; i++)
#define mem(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;

const int N=(1<<18)+100;
const double pi=acos(-1.);
int T,n,nn,m,q
,rev
;
ll s
;

struct C{
double x,y;
C (double a=0,double b=0):x(a),y(b){}
}a
;
C operator +(C a,C b){ return C(a.x+b.x,a.y+b.y); }
C operator -(C a,C b){ return C(a.x-b.x,a.y-b.y); }
C operator *(C a,C b){ return C(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x); }

void DFT(C a[],int f){
for (int i=0;i<nn;i++)
if (i<rev[i]) swap(a[i],a[rev[i]]);
for (int i=1; i<nn; i<<=1){
C wn(cos(pi/i),f*sin(pi/i));
for (int p=i<<1,j=0; j<nn; j+=p){
C w(1,0);
for (int k=0; k<i; k++,w=w*wn){
C x=a[j+k],y=w*a[i+j+k]; a[j+k]=x+y; a[i+j+k]=x-y;
}
}
}
if (f==-1) rep(i,0,nn-1) a[i].x/=nn;
}

int main(){
freopen("hdu4609.in","r",stdin);
freopen("hdu4609.out","w",stdout);
scanf("%d",&T);
while (T--){
m=0; mem(a); mem(q); mem(s); mem(rev); scanf("%d",&n);
rep(i,1,n) scanf("%d",&q[i]),m=max(m,q[i]);
rep(i,1,n) a[q[i]].x++;
m<<=1; int L=0; for (nn=1; nn<=m; nn<<=1) L++;
rep(i,0,nn-1) rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1));
DFT(a,1); rep(i,0,nn-1) a[i]=a[i]*a[i]; DFT(a,-1);
rep(i,1,m) s[i]=(ll)(a[i].x+0.5);
rep(i,1,n) s[q[i]<<1]--;
rep(i,1,m) s[i]=s[i-1]+(s[i]>>1);
sort(q+1,q+n+1); ll ans=0,tot=1ll*n*(n-1)*(n-2)/6;
rep(i,1,n) ans+=1ll*s[m]-s[q[i]]-1ll*(n-i+1)*(n-1)+1ll*(n-i+1)*(n-i)/2;
printf("%.7lf\n",(double)ans/tot);
}
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: