CF 908D New Year and Arbitrary Arrangement 期望DP

Let dp[i][j] be the expected answer given that there are i subsequences of the form ‘a’ in the prefix and j subsequences of the form ‘ab’ in the prefix.

Then, we have something like dp[i][j] = (pa * dp[i + 1][j] + pb * dp[i][i + j]) / (pa + pb).The first term in this sum comes from adding another ‘a’ to our sequence, and the second comes from adding a ‘b’ to our sequence. We also have dp[i][j] = j if j ≥ k as a base case. 
The answer should be dp[0][0].

https://blog.csdn.net/Icefox_zhx/article/details/78937774

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <bitset>
#include <map>
#include <vector>

#ifdef LOCAL
#define debug(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define debug(x) 1;
#endif

#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lowbit(x) x&-x
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MAXN = 2e6 + 5;

ll d[1111][1111];

ll fastpow(ll a, int n) {
    ll base = a, ret = 1;
    while(n) {
        if(n & 1) ret = ret * base % MOD;
        base = base * base % MOD;
        n >>= 1;
    }
    return ret;
}


int main() {
#ifdef LOCAL
    freopen ("input.txt", "r", stdin);
#endif
    int k, PA, PB;
    cin >> k >> PA >> PB;
    ll pa = PA * fastpow(PB + PA, MOD - 2) % MOD, pb = PB * fastpow(PA + PB, MOD - 2) % MOD;
    for(int i = k; i >= 1; i--) {
        for(int j = k - 1; j >= 0; j--) {
            if(i + j >= k) d[i][j] = pb * (i+j) % MOD * fastpow(1 - pa + MOD, MOD - 2) % MOD
                + pb * fastpow(1 - pa, MOD - 2) % MOD * fastpow(1 - pa, MOD - 2) %MOD;
            else d[i][j] = (pa * d[i + 1][j] + pb * d[i][i + j]) % MOD;
        }
    }
    printf("%lld\n", (d[1][0] - 1 + MOD) % MOD);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c6376315qqso/article/details/81909923