# HDU 4609 3-idiots

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
Source
2013 Multi-University Training Contest 1
Recommend
liuyiding

R数组的计算不能放到外面。。。。。

``````<pre>
#include <bits/stdc++.h>
using namespace std;
typedef complex<double> cp;
#define maxn 262145
#define F0(i, n) for (int i = 0; i < (n); i++)
#define F1(i, n) for (int i = 1; i <= (n); i++)
cp a[maxn * 4];
int n, m, L, R[maxn * 4];
const double pi = acos(-1);
void fft(cp a[], int flag){
F0(i, n) if (i < R[i]) swap(a[i], a[R[i]]);
for (int i = 1; i < n; i <<= 1) {
cp wn(cos(pi / i), sin(flag * pi / i)), x, y;
for (int j = 0; j < n; j += i << 1) {
cp w(1, 0);
for (int k = 0; k < i; k++, w *= wn) {
x = a[j + k];
y = w * a[j + i + k];
a[j + k] = x + y;
a[j + i + k] = x - y;
}
}
}
}

int T, nd;
int d[maxn * 4], e[maxn * 4];
long long sum[maxn * 4];
long long p1, p2;
int main(){
scanf("%d", &T);
while (T--) {
int maxns = 0;
F0(i, maxn) a[i] = 0; L = 0; p1 = 0;
scanf("%d", &nd); memset(sum, 0, sizeof sum);
F0(i, nd) scanf("%d", &d[i]), a[d[i]] += 1, sum[d[i]]++, maxns = max(maxns, d[i]);
m = maxns * 2 + 1;
for (n = 1; n <= m; n <<= 1) L++;
F0(i, n) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
fft(a, 1);
F0(i, n) a[i] *= a[i];
fft(a, -1);
F0(i, n) e[i] = int(a[i].real() / n + 0.5);
F0(i, n) e[i << 1] -= sum[i];
F0(i, n) e[i] >>= 1;
F0(i, n) sum[i + 1] += sum[i];
F0(i, n) p1 += e[i] * (sum[n] - sum[i - 1]);
p2 = nd * (nd - 1ll) * (nd - 2ll) / 6ll;
printf("%.7f\n", 1 - p1 * 1.0 / p2);
}
}
</pre>``````