codeforces 1194F. Crossword Expert

题解:

    首先可以想到dp[i][j]状态:i代表准备完成第i件事,总共用了j个单位时间的总概率,转移方程为dp[i][j] = 1/2(dp[ i-1 ][ j-t[i] ] + dp[ i-1 ][ j-t[i]-1 ]。第二维T非常大,先考虑优化第二维,第二维有一部分的值总是固定的那就是ti的前缀和。ti前缀和那部分不用算在状态里。所以重新定义状态:dp[i][j]状态:i代表准备完成第i件事,前i件事有j件事是 在原本时间上+1的总概率。然后状态第二维下降到n。

转移方程:dp[i][j] = 1/2(dp[i-1][j-1] + dp[i-1][j])。

可以知道这是个杨辉三角的递推式。用组合数公式求解一下就行。

然后就可以O(n logn)的求解了,优化一下O(n)就行。

#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("in.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];
    }
    int posmax = n;

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

    int num;
    int inv2 = invF[2];
    LL res = 0;
    LL posmaxtmp = sum[posmax]-1;

    for(int i = 0; i <= posmax; i++) {
        ++posmaxtmp;
        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) * C(posmax, i) % mod * posmax % mod;
        res %= mod;
    }
    cout<<res<<endl;

    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值