首页 > ACM题库 > HDU-杭电 > HDU 4609-3-idiots-概率-[解题报告]HOJ
2015
09-17

HDU 4609-3-idiots-概率-[解题报告]HOJ

3-idiots

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

2
4
1 3 3 4
4
2 3 3 4

样例输出:

0.5000000
1.0000000

转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by—cxlove

题意 :给出n条边,问选出三条边能组成三角形的概率 

http://acm.hdu.edu.cn/showproblem.php?pid=4609

第一次搞FFT,理论还不是非常清楚,首先要了解卷积。

我只是来存代码的,具体的可以看kuangbin巨巨的解释

http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html

num[i]表示长度为i的边有几条,求一次num与num的卷积之后,num[i]表示两条边和为i的有多少对。

然后 需要去重一下,最后就可以 O(n)统计了,去重的地方需要注意,blog里有讲很详细

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
//FFT copy from kuangbin
const double pi = acos (-1.0);
// Complex  z = a + b * i  
struct Complex {
    double a, b;
    Complex(double _a=0.0,double _b=0.0):a(_a),b(_b){}
    Complex operator + (const Complex &c) const {
        return Complex(a + c.a , b + c.b);
    }
    Complex operator - (const Complex &c) const {
        return Complex(a - c.a , b - c.b);
    }
    Complex operator * (const Complex &c) const {
        return Complex(a * c.a - b * c.b , a * c.b + b * c.a);
    }
};
//len = 2 ^ k
void change (Complex y[] , int len) {
    for (int i = 1 , j = len / 2 ; i < len -1 ; i ++) {
        if (i < j) swap(y[i] , y[j]);
        int k = len / 2;
        while (j >= k) {
            j -= k;
            k /= 2;
        }
        if(j < k) j += k;
    } 
}
// FFT 
// len = 2 ^ k
// on = 1  DFT    on = -1 IDFT
void FFT (Complex y[], int len , int on) {
    change (y , len);
    for (int h = 2 ; h <= len ; h <<= 1) {
        Complex wn(cos (-on * 2 * pi / h), sin (-on * 2 * pi / h));
        for (int j = 0 ; j < len ; j += h) {
            Complex w(1 , 0);
            for (int k = j ; k < j + h / 2 ; k ++) {
                Complex u = y[k];
                Complex t = w * y [k + h / 2];
                y[k] = u + t;
                y[k + h / 2] = u - t;
                w = w * wn;
            }
        }
    }
    if (on == -1) {
        for (int i = 0 ; i < len ; i ++) {
            y[i].a /= len;
        }
    }
}
const int N = 100005;
typedef long long LL;
int n , a[N];
LL sum[N << 2] , num[N << 2];
Complex x1[N << 2];
int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt" , "r" , stdin);
    #endif
    int t;
    scanf ("%d", &t);
    while (t --) {
        memset (num , 0 , sizeof(num));
        scanf ("%d", &n);
        for (int i = 0 ; i < n ; i ++) {
            scanf ("%d", &a[i]);
            num[a[i]] ++;
        }
        sort (a , a + n);
        int len = a[n - 1] + 1;
        int l = 1;
        while (l < len * 2) l <<= 1;
        for (int i = 0 ; i < len ; i ++) {
            x1[i] = Complex (num[i] , 0);
        }
        for (int i = len ; i < l ; i ++) {
            x1[i] = Complex (0 , 0);
        }
        FFT(x1 , l , 1);
        for (int i = 0 ; i < l ; i ++) {
            x1[i] = x1[i] * x1[i];
        }
        FFT(x1 , l , -1);
        for (int i = 0 ; i < l ; i ++) {
            num[i] = (LL)(x1[i].a + 0.5);
        }
        l = 2 * a[n - 1];
        for (int i = 0 ; i < n ; i ++) {
            num[a[i] << 1] --;
        }
        for (int i = 1 ; i <= l ; i ++) {
            num[i] /= 2;
        }
        sum[0] = 0;
        for (int i = 1 ; i <= l ; i ++) {
            sum[i] = sum[i - 1] + num[i];
        }
        double ans = 0;
        for (int i = 0 ; i < n ; i ++) {
            ans += sum[l] - sum[a[i]];
            ans -= n - 1;
            ans -= (double)i * (n - 1 - i);
            ans -= (double)(n - i - 1) * (n - i - 2) / 2;
        }
        printf ("%.7f\n", ans * 6.0 / n / (n - 1.0) / (n - 2.0));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/acm_cxlove/article/details/9466063