0
点赞
收藏
分享

微信扫一扫

hdu 4609 3-idiots


3-idiots

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


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


2013 Multi-University Training Contest 1




【代码】(被暴力卡常数光荣TLE)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<complex>
#include<cstdio>
#define pi acos(-1)
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=200005;
typedef complex <double> E;
int n,m,L,T,mx;
E a[mxn],b[mxn];
ll sum[mxn],num[mxn];
int R[mxn],c[mxn],len[mxn];
inline void init()
{
	M(R),M(c),M(num),M(sum),M(a);
	n=m=L=mx=0;
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*f;
}
inline void fft(E *a,int f)
{
	int i,j,k;
	fo(i,0,mx-1) if(i<R[i]) swap(a[i],a[R[i]]);
	for(i=1;i<mx;i<<=1)
	{
		E wn(cos(pi/i),f*sin(pi/i));
		for(j=0;j<mx;j+=(i<<1))
		{
			E w(1,0);
			for(k=0;k<i;k++,w*=wn)
			{
				E x=a[j+k],y=w*a[j+k+i];
				a[j+k]=x+y,a[j+k+i]=x-y;
			}
		}
	}
	if(f==-1) fo(i,0,mx-1) a[i]/=mx;
}
int main()
{
	int i,j;
	T=read();
	while(T--)
	{
		init();
		n=read()-1;
		fo(i,0,n)
		{
			len[i]=read();
			mx=max(mx,len[i]);
			num[len[i]]++;
		}
		sort(len,len+n+1);
		m=2*mx;for(mx=1;mx<=m;mx<<=1) L++;
    	fo(i,0,mx-1) R[i]=(R[i>>1]>>1|((i&1)<<(L-1)));
		fo(i,0,mx) a[i]=num[i];
		fft(a,1);
		fo(i,0,mx) a[i]*=a[i];
		fft(a,-1);
		fo(i,0,m)
		  num[i]=(ll)(a[i].real()+0.1);
//		  cout<<i<<":"<<num[i]<<endl;
		fo(i,0,n) num[len[i]+len[i]]--;
		fo(i,0,m) num[i]>>=1;
		fo(i,1,m) sum[i]=sum[i-1]+num[i];
		ll cnt=0;
		fo(i,0,n)
		{
			cnt+=sum[m]-sum[len[i]];
			cnt=cnt-(ll)i*(n-i)-n;
			cnt-=(ll)(n-i)*(n-i-1)/2;
		}
		ll tot=(ll)n*(n-1)*(n+1)/6;
		printf("%.7lf\n",(double)cnt/tot);
	}
	return 0;
}




举报

相关推荐

0 条评论