HDU 4609 3-idiots 组成三角形的概率

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

题意就是给你n根线段,问任意选三根,能够构成三角形的概率。

首先总的选择方案很容易得到,直接组合数公式就可以得到。

然后就是关于三角形的怎么组成。

对于一个每一个长度len[i],记录一个num数组。

然后num与num相卷积,就可以得到,任意选两条线段,得到的长度的组合方案数。

比如 1 3 3 4 他的num数组{0,1,0,2,1}卷积 *{0,1,0,2,1}

得到{0,0,1,0,4,2,4,4,1}表示组成长度的方案数。但不过我们要求是三角形的组成,因此

我们减去重复选的情况,以及选了两次同一线段的数目。

然后我们对线段进行排序,遍历每一个条线段,作为第三边的情况,这里要求选的两天边要小于他,但不过加起来又要大于他

因此,对num求前缀和,sum[tot]-sum[len[i]]就是大于的总的方案数,其中包含了选了她本身的,以及一大一小和两个大的情况

分别减去就行了。具体看代码就行了。

关于FFT部分,这道题内存卡得很紧,因此尽量少开数组,同时尽可能的优化常数。

#include "bits/stdc++.h"

using namespace std;
const double eps = 1e-6;
#define reg register
#define lowbit(x) x&-x
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fi first
#define se second
#define makp make_pair
#define cp complex<double>

int dcmp(double x) {
    if (fabs(x) < eps) return 0;
    return (x > 0) ? 1 : -1;
}

typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const ull hash2 = 50331653;
const ll N = 450000 + 10;
const int M = 1000000;
const int inf = 0x3f3f3f3f;
const ll mod = 1000000000 + 7;
const double Pi = acos(-1.0);

struct Complex {
    double x, y;

    Complex(double xx = 0, double yy = 0) { x = xx, y = yy; }
} a[N], omg[N];

Complex operator+(Complex a, Complex b) { return Complex(a.x + b.x, a.y + b.y); }

Complex operator-(Complex a, Complex b) { return Complex(a.x - b.x, a.y - b.y); }

Complex operator*(Complex a, Complex b) { return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }

ll num[N], sum[N];
int len[N], r[N], lim, tot, n;

void init() {
    for (int i = 0; i < tot; i++) {
        omg[i] = Complex(cos(2 * Pi * i / tot), sin(2 * Pi * i / tot));
    }
    for (int i = 0; i < tot; i++) {
        r[i] = (r[i >> 1] >> 1) | ((i & 1) << (lim - 1));
    }
}

void fft(Complex *a, int inv) {
    for (int i = 0; i < tot; i++) {
        if (i < r[i]) swap(a[i], a[r[i]]);
    }
    for (int l = 2; l <= tot; l <<= 1) {
        int m = l / 2;
        for (Complex *p = a; p != a + tot; p += l) {
            for (int i = 0; i < m; i++) {
                Complex tt = omg[tot / l * i];
                ///Complex tt = Complex(cos(2 * Pi * i / l), sin(2 * Pi * i / l));
                if (inv) tt.y = -tt.y;
                Complex tmp = tt * p[i + m];
                p[i + m] = p[i] - tmp;
                p[i] = p[i] + tmp;
            }
        }
    }
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        memset(num, 0, sizeof(num));
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &len[i]);
            num[len[i]]++;
        }
        sort(len, len + n);
        tot = 1, lim = 0;
        int n1 = len[n - 1] + 1;
        int n2 = 2 * n1 - 1;
        while (tot < n2 + 1) tot <<= 1, lim++;
        for (int i = 0; i < n1; i++)
            a[i] = Complex(num[i], 0);
        for (int i = n1; i < tot; i++)
            a[i] = Complex(0, 0);
        init();
        fft(a, 0);
        for (int i = 0; i < tot; i++) {
            a[i] = a[i] * a[i];
        }
        fft(a, 1);
        for (int i = 0; i < n2; i++) {
            num[i] = (ll) (a[i].x / tot + 0.5);
        }
        for (int i = 0; i < n; i++) num[2 * len[i]]--;
        for (int i = 0; i < n2; i++) num[i] /= 2;
        for (int i = 0; i < n2; i++) sum[i] = sum[i - 1] + num[i];
        ll cnt = 0;
        for (int i = 0; i < n; i++) {
            cnt += sum[n2 - 1] - sum[len[i]];
            cnt -= 1LL * (n - i - 1) * i;
            cnt -= (n - 1);
            cnt -= 1LL * (n - i - 1) * (n - i - 2) / 2;
        }
        ll vsum = 1LL * n * (n - 1) * (n - 2) / 6;
        printf("%.7f\n", 1.0 * cnt / vsum);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值