【New Year and Arbitrary Arrangement】
定义dp(i,j)为子序列"ab"的个数为i且存在j个"a"时的期望。
状态转移为:
dp(i,j)=Pa+PbPadp(i,j+1)+Pa+PbPbdp(i+j,j)
特殊的,当i=0,j=0时:
dp(0,0)==Pa+PbPadp(0,1)+Pa+PbPbdp(0,0)dp(0,1)
如果我一直不选"b",那么dp(i,j)的j就会增大到无限,这显然是不行的。
当i+j≥k时,只要选取一个"b",就能到达末态:
dp(i,j)=======Pa+PbPb(i+j)+(Pa+Pb)2PaPb(i+j+1)+(Pa+Pb)3Pa2Pb(i+j+2)...∑t≥0(Pa+Pb)t+1PatPb(i+j+t)(i+j)Pa+PbPb∑t≥0(Pa+PbPa)t+(Pa+Pb)2PaPb∑t≥1(Pa+PbPa)t−1⋅t(i+j)Pa+PbPb1−Pa+PbPa1+(Pa+Pb)2PaPb(∑t≥1(Pa+PbPa)t)′(i+j)Pa+PbPb1−Pa+PbPa1+(Pa+Pb)2PaPb(1−Pa+PbPaPa+PbPa)′(i+j)Pa+PbPbPa+PbPb1+(Pa+Pb)2PaPb(1−Pa+PbPa)21i+j+PbPa
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
ll power(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
ll inv(ll x) { return power(x, mod - 2); }
ll k, pa, pb, dp[N][N];
ll res = 0;
int main() {
cin >> k >> pa >> pb;
ll qa = pa * inv((pa + pb) % mod) % mod;
ll qb = pb * inv((pa + pb) % mod) % mod;
for (int i = k - 1; i >= 0; i--) {
for (int j = k; j >= 1; j--) {
if (i + j >= k)
dp[i][j] = ((i + j) % mod + pa * inv(pb) % mod) % mod;
else
dp[i][j] =
(qb * dp[i + j][j] % mod + qa * dp[i][j + 1] % mod) % mod;
}
}
dp[0][0] = dp[0][1];
cout << dp[0][0];
return 0;
}