FFT (Fast Fourier transform): HDU 4609 3-idiots

Source: Internet
Author: User

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 is 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 the them were 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 is sent into jail.
However, the three men were exactly idiots, and "what they would" do are 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 on the forest, determine the probability that they would is saved.

Input an integer T (t≤100) would exist in the first line of input, indicating the number of test cases.
Each test case is 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 Input241 3 3 442 3 3 4

Sample Output0.50000001.0000000 everyone go to mod Bingbin Bar ~
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <cmath>6 using namespacestd;7 Const intmaxn=500010;8 Const Long DoublePi=acos (-1.0);9 structcomplex{Ten     Long Doubler,i; OneComplexLong Doubler_=0.0,Long Doublei_=0.0){ AR=r_;i=I_; -     } -Complexoperator+ (Complex &a) { the         returnComplex (a.r+r,a.i+i); -     } -Complexoperator-(Complex &a) { -         returnComplex (r-a.r,i-A.I); +     } -Complexoperator*(Complex a) { +         returnComplex (r*a.r-i*a.i,i*a.r+a.i*R); A     } at }A[MAXN]; -  - voidRader (Complex *a,intLen) { -      for(intI=1,j=len>>1; i<len-1; i++){ -         if(i<j) Swap (A[i],a[j]); -         intK=len>>1; in          while(j>=k) { -j-=K; tok>>=1; +         } -j+=K; the     } * } $ Panax Notoginseng voidFFT (Complex *a,intLenintOn ) { - rader (a,len); the      for(intH=2; h<=len;h<<=1){ +Complex WN (cos (-on*pi*2/h), sin (-on*pi*2/h)); A          for(intj=0; j<len;j+=h) { theComplex W (1.0,0); +              for(intk=j;k<j+ (h>>1); k++){ -Complex x=A[k]; $Complex y=a[k+ (h>>1)]*W; $a[k]=x+y; -a[k+ (h>>1)]=x-y; -w=w*WN; the             }     -         }Wuyi     } the     if(on==-1) -          for(intI=0; i<len;i++) WuA[i].r/=Len; - } About intA[MAXN]; $ Long LongNUM[MAXN]; - intMain () { - #ifndef Online_judge -     //freopen ("", "R", stdin); A     //freopen ("", "w", stdout); + #endif the     intt,n,len=1; -scanf"%d",&T); $      while(t--){ thescanf"%d",&n); thememset (A,0,sizeof(A)); thememset (NUM,0,sizeof(num)); the          while(len<=200000) len<<=1; -          for(intI=1; i<=n;i++) inscanf"%d",&a[i]); theSort (A +1, a+n+1); len=1; the          while(len<=a[n]*2) len<<=1; About          for(intI=1; i<=n;i++)     thea[a[i]].r++; theFFT (A,len,1); the          for(intI=0; i<len;i++) +a[i]=a[i]*A[i]; -FFT (a,len,-1); the          for(intI=0; i<len;i++)BayiNum[i]= (Long Long) (a[i].r+0.5); the          for(intI=1; i<=n;i++) thenum[a[i]<<1]--; -          for(intI=0; i<len;i++)     -num[i]>>=1; the          for(intI=1; i<len;i++) thenum[i]+=num[i-1]; the         Long LongCnt=0; the          for(intI=1; i<=n;i++){ -cnt+=num[len-1]-Num[a[i]]; thecnt-= (Long Long) (n-i) * (I-1); thecnt-=n-1; thecnt-= (Long Long) (n-i) * (n-i-1)/2;94         } the         Long LongTot= ((Long Long) N (n1) * (n2))/6; theprintf"%.7lf\n",1.0*cnt/tot); the     }98     return 0; About}

FFT (Fast Fourier transform): HDU 4609 3-idiots

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.