HDU 4609 3-idiots(FFT优化 + 计数方法)——2013 Multi-University Training Contest 1

97 篇文章 0 订阅
39 篇文章 0 订阅

传送门

3-idiots

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


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
 

题目大意:

给你 N(3N105) 个木棍,每个木棍的长度是 ai(1ai105) ,求的是从中任意选取 3 根组成三角形的概率。

解题思路:

我们现在用一个 num 数组来记录每条边的数目, num[i]i 条边有 num[i] 个,然后利用两个 num 数组做一下 FFT ,做完

FFT 之后现在的 num[i] 数组就是表示两个木棍的和为 i 的有多少个,但是在这里需要去一下重,因为我们首先要减去的是本身是自己

的那一个,然后又因为是组合数,所以要除以 2,然后然后求一下前缀和, sum[i] 两个长度 >i 的个数,我们枚举第三条边,假设当

前边是最大的边,但是现在遇到一个问题就是可能我们多算了一些值,然后我们再进行一下去重就行了,具体的细节可以看一下 kuangbin 巨巨的博客他写的很好。

My Code

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;
const LL MAXN = 4e5+100;
struct complex {
    double a, b;
    complex(double aa = 0.0, double bb = 0.0) { a = aa; b = bb; }
    complex operator +(const complex &e) { return complex(a + e.a, b + e.b); }
    complex operator -(const complex &e) { return complex(a - e.a, b - e.b); }
    complex operator *(const complex &e) { return complex(a * e.a - b * e.b, a * e.b + b * e.a); }
}x1[MAXN], x2[MAXN], x[MAXN];


void change(complex * y, LL len){
    LL i, j, k;
    for (i = 1, j = len / 2; i < len - 1; i++){
        if (i < j) swap(y[i], y[j]);
        k = len / 2;
        while (j >= k){
            j -= k;
            k /= 2;
        }
        if (j < k) j += k;
    }
}

void fft(complex *y, LL len, LL on){
    change(y, len);
    for (LL h = 2; h <= len; h <<= 1){
        complex wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
        for (LL j = 0; j < len; j += h){
            complex w(1, 0);
            for (LL 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 (LL i = 0; i < len; i++)
            y[i].a /= len;
}
LL a[MAXN], num[MAXN], sum[MAXN];///num[i]:长度为 i 的有 num[i] 个
int main(){
    int T;
    LL n;
    scanf("%d",&T);
    while(T--){
        scanf("%lld",&n);
        memset(num, 0, sizeof(num));
        for(LL i=0; i<n; i++){
            scanf("%lld",&a[i]);
            num[a[i]]++;
        }
        sort(a, a+n);
        LL len = 1LL;
        LL len1 = a[n-1] + 1LL;
        while(len < 2*len1)len<<=1LL;
        ///cout<<len<<endl;
        for(LL i=0; i<len1; i++)
            x1[i].a = num[i], x1[i].b = 0;
        for(LL i=len1; i<len; i++)
            x1[i].a = 0, x1[i].b = 0;
        fft(x1, len, 1);
        for(LL i=0; i<len; i++)
            x[i] = x1[i] * x1[i];
        fft(x, len, -1);
        ///fft 之后num[i] 表示的是两个长度和为 i 的个数
        for(LL i=0; i<len; i++)
            num[i] = (LL)(x[i].a+0.5);
        for(LL i=0; i<n; i++)///去重
            num[a[i]*2]--;
        for(LL i=0; i<len; i++)///是两个数的组合应该除以2
            num[i]>>=1LL;
        sum[0] = 0;///sum[i]: 前缀和 sum[len-1]-sum[i]:长度和 > i 的
        for(LL i=1; i<len; i++)
            sum[i] = sum[i-1] + num[i];
        double ans = 0, tmp = sum[len-1];
        for(LL i=0; i<n; i++){
            ///长度和 > a[i] 的
            ans += tmp - sum[a[i]];
            ///a[i] 是第二大的
            ans -= (n-1-i)*i;
            ///包含 a[i] 的,从(n-1)个中任选一个
            ans -= (n-1);
            ///a[i] 是最小的,从后面两个选两个
            ans -= (n-1-i)*(n-1-i-1)/2;
        }
        double tp = n*(n-1)*(n-2)/6;
        printf("%.7f\n",ans/tp);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值