HDU 4609(3-idiots-FFT+dp)

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≤10 5).
The following line contains N integers a_i (1≤a_i≤10 5), 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
 

 把边计数排序,然后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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值