码迷,mamicode.com
首页 > 其他好文 > 详细

[hdu 4609] 3-idiots

时间:2019-11-02 15:39:40      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:ted   include   math   number   win   first   key   ranch   acm   

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≤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
 
题意:求任选三个数能够构成三角形的概率
数均为正整数且范围不大
那么用卷积求出任选两个数之和的情况,然后枚举最大边就好了
 
#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;
}

 

[hdu 4609] 3-idiots

标签:ted   include   math   number   win   first   key   ranch   acm   

原文地址:https://www.cnblogs.com/ruoruoruo/p/11782321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!