Description
King OMeGa catched Three men who had been to the street. Looking as idiots though, the three men insisted that it is a kind of art, and performance the king to free begged. 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 is 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 do are only to would the pick branches. 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 is would.
Input
An integer T (t≤100) would exist in the ' the ' 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
the
Given some numbers to indicate the length of the line segment, choose any three probability that the triangle can be formed.
train of Thought
The total number of three selected in all numbers is c3n c_n^3, so the problem is transformed into the number of combinations that can form triangles with three numbers.
To make a triangle, we need to find the sum of the lengths of the two short sides greater than a long edge, so we have to calculate what the length of the selected two edges can be combined.
And the length of the two-edge combination is essentially the same, so we can use Num[i to record the number of edges with length I.
And then the NUM array itself to its own convolution, the result is the case of all 22 combinations.
for {1,3,3,4}, num = {0,1,0,2,1}.
{0,1,0,2,1} * {0,1,0,2,1} convolution result is {0,0,1,0,4,2,4,4,1}.
The meaning of convolution is actually the increment , the translation is added . (Consistent with the principle of multiplication)
We know: Time domain convolution, frequency domain product.
So we can use FFT (DFT) first to convert Num to another representation (evaluation), and then 22 do (product) and then use FFT (IDFT) to convert it back (interpolation).
At this point we have obtained NUM's own convolution result.
The value in the result represents the 22 combination of NUM itself. Because it contains a combination of the number itself and itself, we have to get rid of the situation first. The first selects the A1, the second selects the A2 with the first selected A2, and the second selection A1 we think is the same, so the NUM array needs to be divided by 2.
To prefix the NUM array and to order the original array A, we then need to find the third edge, which satisfies the other two edges and is greater than the edge and the label is less than this edge.
At this point we assume that A[i] is the longest edge in the triangle, then the length greater than a[i] is sum[len]-sum[a[i].
But we need to meet these two edges of the edges are less than a[i], assuming that these two edges are x,y, the following conditions may occur: X,y has a value greater than a[i], another value is less than a[i], a total of (n-i-1) *i is greater than x,y, total Total (n-i-1) * (n-i-2)/2 situation x,y a bit in the position of I, a total of n-1
So lose these three kinds of situation is to satisfy the meaning of the combination slightly, cumulative statistics can.
AC Code
#include <iostream> #include <stdio.h> #include <algorithm> #include <iomanip> #include <
cmath> #include <cstring> #include <vector> typedef __int64 LL;
#define PI ACOs ( -1.0) using namespace std;
const int MAXN = 400005;
Complex structure body struct complex {double r,i;
Complex (double r=0,double i=0) {r=r;
I=i;
} complex operator+ (const complex &a) {return complex (R+A.R,I+A.I);
} complex operator-(const complex &a) {return complex (R-A.R,I-A.I);
} complex operator* (const complex &a) {return complex (R*A.R-I*A.I,R*A.I+I*A.R);
}
};
* * * for FFT and IFFT before the reverse conversion * position I and I of the binary reversal of the position swap, (such as 001 after the reversal is) *len must go to 2 power/void change (complex x[],int len) {int i,j,k;
for (i = 1, j = len/2 I <len-1; i++) {if (I < j) Swap (X[i],x[j]);
Swap the elements of each other for a small reversal, i<j guaranteed to exchange once//i do normal +1,j do the reverse type of +1, always I and j are reversed k = LEN/2;
while (J >= K) {J-= k;
K/= 2;
} if (J < k) J = k;
}/* * Do FFT *len must be 2^n form, the shortfall is 0 *on=1 when dft,on=-1 is idft/void FFT (complex x[],int on) {change (len,int); for (int i=2; i<=len; i<<=1) {Complex wn (cos (-on*2*pi/i), sin (-on*2*pi/i));
The unit complex root e^ (2*pi/m) is expanded with Euler formula for (int j=0; j<len; j+=i) {complex W (1,0);
for (int k=j; k<j+i/2; k++) {Complex u = x[k];
Complex t = W*X[K+I/2];
X[k] = u+t;
X[K+I/2] = u-t;
W = w*wn;
}} if (on = = 1) for (int i=0; i<len; i++) x[i].r/= Len;
} complex X[MAXN];
int A[MAXN];
LL NUM[MAXN],SUM[MAXN];
int main () {int T;
scanf ("%d", &t);
while (t--) {int n;
scanf ("%d", &n);
memset (num,0,sizeof (num));
for (int i=0; i<n; i++) {scanf ("%d", a+i);
num[a[i]]++;
Sort (a,a+n);
int len1 = a[n-1]+1;
int len=1;
while (LEN<2*LEN1) len<<=1;
for (int i=0; i<len1; i++) X[i]=complex (num[i],0);
for (int i=len1; i<len; i++)//Complement 0 X[i]=complex (0,0); FFT (x,len,1);
Evaluate for (int i=0; i<len; i++)//multiplication x[i]=x[i]*x[i]; FFT (x,len,-1); interpolation for (int i=0; i<len; i++) num[i]= (x[i].r+0.5);
Rounding for (int i=0; i<n; i++)//Removing the same number of cases num[a[i]+a[i]]--;
for (int i=1; i<len; i++)//selection is independent of order num[i]/=2;
for (int i=1; i<=len; i++) sum[i]=sum[i-1]+num[i];
LL ans=0;
for (int i=0; i<n; i++) {ans+=sum[len]-sum[a[i]]; Ans-=ll (n-i-1) *i; One freshman small ans-=ll (n-i-1) * (n-i-2)/2; Two big
ans-=n-1; One point in the current bit} LL tot=1ll*n* (n-1) * (n-2)/6;
All combinations printf ("%.7lf\n", (double) ans/tot);
return 0; }