HDU 5481 Desiderium

问题描述
有一条数轴,还有一个区间的集合,集合大小为nn。
现在等概率的从集合中选出集合的一个子集,求取出的子集的区间并集的期望长度。
空集的区间并长度被认为是00
输入描述
输入文件包含多组数据,第一行为数据组数TT。
对于每组数据,第一行为集合的大小nn。
接下来的nn行,每行两个数ll , rr代表集合内区间的左右端点坐标。

1 \leq n \leq 100,0001n100,000.
-1,000,000,000 \leq l \leq r \leq 1,000,000,0001,000,000,000lr1,000,000,000.
输出描述
对于每组数据,输出期望乘2^n2n1000000007(10^9+7)1000000007(109+7) 取模的结果。
输入样例
2
1
0 1
2
0 2
1 3
输出样例
1
7
Hint
对于第二个例子,期望为\frac{0+2+2+3}{4}=\frac{7}{4}40+2+2+3=47

找到递推方法。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn = 300005;
const LL base = 1e9 + 7;
int T, n, tot, l, r;

struct point
{
    int t, x;
    point(){}
    point(int t, int x) :t(t), x(x){}
    bool operator <(const point&a)const
    {
        if (x == a.x) return t > a.t;
        return x < a.x;
    }
}f[maxn];

LL get(int x)
{
    LL ans = 1;
    for (LL i = 2; x; x >>= 1)
    {
        if (x & 1) (ans *= i) %= base;
        i = i*i%base;
    }
    return ans - 1;
}

int main()
{
    while (cin >> T)
    {
        while (T--)
        {
            scanf("%d", &n);
            for (int i = tot = 0; i < n; i++)
            {
                scanf("%d%d", &l, &r);
                f[tot++] = point(1, l);
                f[tot++] = point(-1, r);
            }
            sort(f, f + tot);
            LL ans = 0;
            for (int i = 0, j = 0, k = f[0].x; i < tot; i++)
            {
                (ans += (get(n - j) + 1)*get(j) % base*(f[i].x - k)) %= base;
                j += f[i].t;        k = f[i].x;
            }
            printf("%I64d\n", ans);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值