3-idiots
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3560 Accepted Submission(s): 1241
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
大家都去mod邝斌吧~
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 using namespace std; 7 const int maxn=500010; 8 const long double PI=acos(-1.0); 9 struct complex{ 10 long double r,i; 11 complex(long double r_=0.0,long double i_=0.0){ 12 r=r_;i=i_; 13 } 14 complex operator +(complex &a){ 15 return complex(a.r+r,a.i+i); 16 } 17 complex operator -(complex &a){ 18 return complex(r-a.r,i-a.i); 19 } 20 complex operator *(complex a){ 21 return complex(r*a.r-i*a.i,i*a.r+a.i*r); 22 } 23 }A[maxn]; 24 25 void Rader(complex *a,int len){ 26 for(int i=1,j=len>>1;i<len-1;i++){ 27 if(i<j)swap(a[i],a[j]); 28 int k=len>>1; 29 while(j>=k){ 30 j-=k; 31 k>>=1; 32 } 33 j+=k; 34 } 35 } 36 37 void FFT(complex *a,int len,int on){ 38 Rader(a,len); 39 for(int h=2;h<=len;h<<=1){ 40 complex wn(cos(-on*PI*2/h),sin(-on*PI*2/h)); 41 for(int j=0;j<len;j+=h){ 42 complex w(1.0,0); 43 for(int k=j;k<j+(h>>1);k++){ 44 complex x=a[k]; 45 complex y=a[k+(h>>1)]*w; 46 a[k]=x+y; 47 a[k+(h>>1)]=x-y; 48 w=w*wn; 49 } 50 } 51 } 52 if(on==-1) 53 for(int i=0;i<len;i++) 54 a[i].r/=len; 55 } 56 int a[maxn]; 57 long long num[maxn]; 58 int main(){ 59 #ifndef ONLINE_JUDGE 60 //freopen("","r",stdin); 61 //freopen("","w",stdout); 62 #endif 63 int T,n,len=1; 64 scanf("%d",&T); 65 while(T--){ 66 scanf("%d",&n); 67 memset(A,0,sizeof(A)); 68 memset(num,0,sizeof(num)); 69 while(len<=200000)len<<=1; 70 for(int i=1;i<=n;i++) 71 scanf("%d",&a[i]); 72 sort(a+1,a+n+1);len=1; 73 while(len<=a[n]*2)len<<=1; 74 for(int i=1;i<=n;i++) 75 A[a[i]].r++; 76 FFT(A,len,1); 77 for(int i=0;i<len;i++) 78 A[i]=A[i]*A[i]; 79 FFT(A,len,-1); 80 for(int i=0;i<len;i++) 81 num[i]=(long long)(A[i].r+0.5); 82 for(int i=1;i<=n;i++) 83 num[a[i]<<1]--; 84 for(int i=0;i<len;i++) 85 num[i]>>=1; 86 for(int i=1;i<len;i++) 87 num[i]+=num[i-1]; 88 long long cnt=0; 89 for(int i=1;i<=n;i++){ 90 cnt+=num[len-1]-num[a[i]]; 91 cnt-=(long long)(n-i)*(i-1); 92 cnt-=n-1; 93 cnt-=(long long)(n-i)*(n-i-1)/2; 94 } 95 long long tot=((long long)n*(n-1)*(n-2))/6; 96 printf("%.7lf\n",1.0*cnt/tot); 97 } 98 return 0; 99 }