Codeforces 1194F Crossword Expert

Crossword Expert

算每个游戏完成的概率加起来就是答案了, 然后组合数前缀暴力转移就完事了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
LL t[N];
LL T, sum[N];
LL bin[N], ibin[N];
LL F[N], Finv[N], inv[N];

int nn, mm;
LL now;

LL power(LL a, LL b) {
    LL ans = 1;
    while(b) {
        if(b & 1) ans = ans * a % mod;
        a = a * a % mod; b >>= 1;
    }
    return ans;
}


LL C(int n, int m) {
    if(n < 0 || n < m) return 0;
    return F[n] * Finv[m] % mod * Finv[n - m] % mod;
}

LL calc(int n, int m) {
    while(nn < n) {
        add(now, now);
        sub(now, C(nn, mm));
        nn++;
    }

    while(mm > m) {
        sub(now, C(nn, mm));
        mm--;
    }

    while(mm < m) {
        mm++;
        add(now, C(nn, mm));
    }

    return now;
}

void init() {
    bin[0] = F[0] = Finv[0] = inv[1] = 1;
    for(int i = 1; i < N; i++) {
        bin[i] = bin[i - 1] * 2 % mod;
        ibin[i] = power(bin[i], mod - 2);
    }
    for(int i = 2; i < N; i++) {
        inv[i] = (mod - mod / i) * inv[mod % i] % mod;
    }
    for(int i = 1; i < N; i++) {
        F[i] = F[i - 1] * i % mod;
    }
    for(int i = 1; i < N; i++) {
        Finv[i] = Finv[i - 1] * inv[i] % mod;
    }
}

int main() {
    init();
    scanf("%d%lld", &n, &T);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &t[i]);
        sum[i] = sum[i - 1] + t[i];
    }
    LL ans = 0;
    now = 1, nn = 0, mm = 0;
    for(int i = 1; i <= n; i++) {
        if(sum[i] > T) break;
        else if(sum[i] + i <= T) {
            add(ans, 1);
        }
        else {
            LL most = T - sum[i];
            add(ans, calc(i, most) * ibin[i] % mod);
        }
    }
    printf("%lld
", ans);
    return 0;
}

/*

*/

  

原文地址:https://www.cnblogs.com/CJLHY/p/11188757.html