[hdu 4609] 3-idiots

3-idiots

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


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
 
题意:求任选三个数能够构成三角形的概率
数均为正整数且范围不大
那么用卷积求出任选两个数之和的情况,然后枚举最大边就好了
 
#include <bits/stdc++.h>
using namespace std;
const int maxn = 262144;
struct comp{
    double x, y;
    comp(double _x = 0, double _y = 0){
        x = _x;
        y = _y;
    }
    friend comp operator * (const comp &a, const comp &b){
        return comp(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
    }
    friend comp operator + (const comp &a, const comp &b){
        return comp(a.x + b.x, a.y + b.y);
    }
    friend comp operator - (const comp &a, const comp &b){
        return comp(a.x - b.x, a.y - b.y);
    }
}f[maxn];
int rev[maxn];
void dft(comp A[], int len, int kind){
    for(int i = 0; i < len; i++){
        if(i < rev[i]){
            swap(A[i], A[rev[i]]);
        }
    }
    for(int i = 1; i < len; i <<= 1){
        comp wn(cos(acos(-1.0) / i), kind * sin(acos(-1.0) / i));
        for(int j = 0; j < len; j += (i << 1)){
            comp tmp(1, 0);
            for(int k = 0; k < i; k++){
                comp s = A[j + k], t = tmp * A[i + j + k];
                A[j + k] = s + t;
                A[i + j + k] = s - t;
                tmp = tmp * wn;
            }
        }
    }
    if(kind == -1) for(int i = 0; i < len; i++) A[i].x /= len;
}
void init(int &len){
    int L = 0;
    for(len = 1; len < maxn; len <<= 1, L++);
    for(int i = 0; i < len; i++){
        rev[i] = rev[i >> 1] >> 1 | (i & 1) << L - 1;
    }    
}
int a[maxn], val[maxn] = {0};
long long num[maxn];
int main(){
    int t;
    scanf("%d", &t);
    int len;
    init(len);
    while(t--){    
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            scanf("%d", a + i);
            val[a[i]]++;
        }
        for(int i = 0; i < len; i++){
            f[i].x = val[i];
        }
        dft(f, len, 1);
        for(int i = 0; i < len; i++) f[i] = f[i] * f[i];
        dft(f, len, -1);
        for(int i = 1; i < len; i++) num[i] = (long long)(f[i].x + 0.5);
        for(int i = 1; i <= n; i++){
            num[a[i] << 1]--;
        }
        for(int i = 1; i < len; i++) num[i] = num[i] >> 1;
        num[0] = 0;
        for(int i = 1; i < len; i++) num[i] += num[i - 1]; 
        long long ans = 0;
        for(int i = 1; i <= n; i++){
            ans += num[a[i]];
        }
        printf("%.7lf\n", 1 - 6.0 * ans / n / (n - 1) / (n - 2));
        for(int i = 1; i <= n; i++) val[a[i]]--;
        for(int i = 0; i < len; i++) f[i] = comp();
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ruoruoruo/p/11782321.html