codeforces 1194F (组合数学)

Codeforces 11194F (组合数学)

传送门:https://codeforces.com/contest/1194/problem/F

题意:

你有n个事件,你需要按照1~n的顺序完成这些事件,每个事件需要\(t_i\)的时间完成,你现在一共有T的时间去做这些事情,每做一件事情的时候,你有0.5的概率花费\(t_i\)的时间完成他,也有0.5的概率花费\(t_i+1\)的时间去完成他,如果在做这个事件的时候时间花完了,你就相当于没有做成这个事件,现在问你在T的时间内完成的事件的个数的期望是多少

题解:

读完题后想到的应该是一个概率dp,但是T有\(2e14\)的大小,我们现在着手于如何优化他

首先,我的每一个事件都只有两种情况,如果是情况1 ,就按照原本的时间去完成他,如果是情况2,我就要在原本的时间的基础上加上一个1,那么我完成k个事件时,我始终是要花费sum[k]的时间去做这些事件的,这里的sum是t的前缀和

所以我们其实不用管时间这个限制条件了,我们只需要得到,前i个事件中我有多少个事件需要花费+1的这个时间去完成他的事件的个数,那么这样我们就变成了一个组合数的问题了

我们可以得到在我们完成k个事件时,有i个事件出现了+1的情况

那么这个概率就是\((\frac{1}{2})^k*C_k^{i}\)这个就很容易理解,如果求期望那么乘上一个k就可以了

那么我总的期望就是\(\sum_{i=1}^{posmax}(\frac{1}{2})^(posmax)*C_k^{i}*posmax\)

posmax表示,我当前最多可以完成的事件

我们用一个副本posmaxtmp来表示posmax完成这些事件的时间

那么在循环中posmaxtmp每次+1时就可以使得完成一个事件的时间+1

如果posmaxtmp大于T了,我们后面的事件就完不成,posmax就得往前走,这里需要处理一下下

并且,当posmaxtmp大于T时,我也需要算出这一部分的期望即完成posmax-1个事件时对答案的贡献

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll;

ll F[maxn], invF[maxn];
ll power(ll a, ll b) {
    ll ret = 1;
    while(b) {
        if(b & 1) ret = (ret * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ret;
}
void init() {
    F[0] = 1;
    for(int i = 1; i < maxn; i++) {
        F[i] = (F[i - 1] * i) % mod;
    }
    invF[maxn - 1] = power(F[maxn - 1], mod - 2);
    for(int i = maxn - 2; i >= 0; i--) {
        invF[i] = invF[i + 1] * (i + 1) % mod;
    }
}

ll C(int n, int m) {
    if(n < 0 || m < 0 || m > n) return 0;
    if(m == 0 || m == n)    return 1;
    return F[n] * invF[n - m] % mod * invF[m] % mod;
}
LL t[maxn];
LL sum[maxn];

int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    IO;
    init();
    int n;
    LL T;
    cin >> n >> T;
    for(int i = 1; i <= n; i++) {
        cin >> t[i];
        sum[i] = sum[i - 1] + t[i];
    }
    // debug1(T);
    int posmax = n;

    for(int i = 1; i <= n; i++) {
        // debug1(sum[i]);
        if(sum[i] > T) {
            posmax = i - 1;
            break;
        }
    }

    // debug1(posmax);
    int num;
    int inv2 = invF[2];
    LL res = 0;
    LL posmaxtmp = sum[posmax] - 1;
    // debug1(inv2);
    for(int i = 0; i <= posmax; i++) {
        ++posmaxtmp;
        // debug3(i, posmaxtmp, C(posmax, i));
        // debug1(posmax);
        while(posmaxtmp > T) {
            res += power(inv2, posmax) * C(posmax - 1, i - 1) % mod * (posmax - 1) % mod;
            res %= mod;
            posmaxtmp -= t[posmax];
            --posmax;
        }
        if(i > posmax) break;
        res += power(inv2, posmax) % mod * C(posmax, i) % mod * posmax % mod;
        res %= mod;
    }
    cout << res << endl;

    return 0;
}
posted @ 2019-07-15 11:53  buerdepepeqi  阅读(416)  评论(0编辑  收藏  举报