CodeForces 908D New Year and Arbitrary Arrangement

Source: Good Bye 2017

Problem: 一个空串, Pa 的概率往末尾加 a Pb 的加 b ,问子序列 "ab" 的数量大于等于 K "ab" 数量的期望。

Idea:
dp(i,j) 表示 a 的数量有 i 个且 ab 的数量有 j 个时的期望。状态转移方程则容易得到。
i+jk 时可以往末尾加入 b,ab,aab... ,那么可以得到期望 E=(i+j)Pb+(i+j+1)PaPb+(i+j+2)P2aPb...=i+j+PaPb
注意到前缀 b 没有贡献,那么答案就是 dp(1,0)

Code:

#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define ALL(A) (A).begin(), (A).end()
#define CLR(A, X) memset(A, X, sizeof(A))
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-10;
const double PI = acos(-1.0);
const auto INF = 0x3f3f3f3f;
int dcmp(double x) { if(fabs(x) < eps) return 0; return x<0?-1:1; }

const int MAXN = 1005;
const LL MOD = 1e9+7;

LL dp[MAXN][MAXN];

LL qpow(LL a, LL b) {
    LL ret = 1;
    while(b) {
        if(b&1) ret = ret*a%MOD;
        a = a*a%MOD;
        b >>= 1;
    }
    return ret;
}

LL inv(LL t) { return qpow(t, MOD-2); }

int main() {
    int n; LL pa, pb;
    scanf("%d%lld%lld", &n, &pa, &pb);
    LL t = pa+pb;
    pa = pa*inv(t)%MOD;
    pb = pb*inv(t)%MOD;
    LL p = pa*inv(pb)%MOD;
    for(int i = n; i >= 0; i--) {
        for(int j = n; j >= 0; j--) {
            if(i+j >= n) dp[i][j] = (i+j+p)%MOD;
            else dp[i][j] = (dp[i+1][j]*pa%MOD+dp[i][i+j]*pb%MOD)%MOD;
        }
    }
    printf("%lld\n", dp[1][0]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_32506797/article/details/78942286
今日推荐