Back to the Past ZOJ-3582 期望DP

Back to the Past

在这里插入图片描述

solution

C n m = C n − 1 m − 1 + C n − 1 m C_n^m=C_{n-1}^{m-1}+C_{n-1}^{m} Cnm=Cn1m1+Cn1m

d p [ i ] [ j ] = ∑ u = 0 i + u ≤ n ∑ v = 0 j + v ≤ n d p [ i + u ] [ j + v ] C i + u u p u q i C j + v v p v q j + d p [ i ] [ j ] C i 0 q i C j 0 q j + 1 dp[i][j]=\sum\limits_{u=0}^{i+u≤n}\sum\limits_{v=0}^{j+v≤n}dp[i+u][j+v]C_{i+u}^up^uq^iC_{j+v}^vp^vq^j+dp[i][j]C_{i}^0q^iC_j^0q^j+1 dp[i][j]=u=0i+unv=0j+vndp[i+u][j+v]Ci+uupuqiCj+vvpvqj+dp[i][j]Ci0qiCj0qj+1

P [ n ] [ m ] = C n m p m q n − m P[n][m]=C_n^mp^mq^{n-m} P[n][m]=Cnmpmqnm

= C n − 1 m − 1 p m q n − m + C n − 1 m p m q n − m =C_{n-1}^{m-1}p^mq^{n-m}+C_{n-1}^{m}p^mq^{n-m} =Cn1m1pmqnm+Cn1mpmqnm

= C n − 1 m − 1 p m − 1 q n − m ∗ p + C n − 1 m p m q n − m − 1 ∗ q =C_{n-1}^{m-1}p^{m-1}q^{n-m}*p+C_{n-1}^{m}p^{m}q^{n-m-1}*q =Cn1m1pm1qnmp+Cn1mpmqnm1q

= P [ n − 1 ] [ m − 1 ] ∗ p + P [ n − 1 ] [ m ] ∗ q =P[n-1][m-1]*p+P[n-1][m]*q =P[n1][m1]p+P[n1][m]q

d p [ i ] [ j ] = ∑ u = 0 i + u ≤ n ∑ v = 0 j + v ≤ n d p [ i + u ] [ j + v ] ∗ P [ i + u ] [ u ] ∗ P [ j + v ] [ v ] + d p [ i ] [ j ] ∗ P [ i ] [ 0 ] ∗ P [ j ] [ 0 ] + 1 dp[i][j]=\sum\limits_{u=0}^{i+u≤n}\sum\limits_{v=0}^{j+v≤n}dp[i+u][j+v]*P[i+u][u]*P[j+v][v]+dp[i][j]*P[i][0]*P[j][0]+1 dp[i][j]=u=0i+unv=0j+vndp[i+u][j+v]P[i+u][u]P[j+v][v]+dp[i][j]P[i][0]P[j][0]+1

d p [ i ] [ j ] = ∑ u = 0 i + u ≤ n ∑ v = 0 j + v ≤ n d p [ i + u ] [ j + v ] ∗ P [ i + u ] [ u ] ∗ P [ j + v ] [ v ] + 1 1 − P [ i ] [ 0 ] ∗ P [ j ] [ 0 ] dp[i][j]=\frac{\sum\limits_{u=0}^{i+u≤n}\sum\limits_{v=0}^{j+v≤n}dp[i+u][j+v]*P[i+u][u]*P[j+v][v]+1}{1-P[i][0]*P[j][0]} dp[i][j]=1P[i][0]P[j][0]u=0i+unv=0j+vndp[i+u][j+v]P[i+u][u]P[j+v][v]+1

code

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

typedef long long ll;

int n, m;
double p, q;
double dp[110][110];
double P[110][110];

void solve() {
    while (cin >> n >> m >> p) {
        q = 1.0 - p;
        double _p = 1, _q = 1;
        if (n + m + (int) p == 0) return;
        memset(dp, 0, sizeof dp);

        P[0][0] = 1;
        P[1][0] = q, P[1][1] = p;
        for (int i = 2; i <= n; ++i) {
            P[i][0] = P[i - 1][0] * q;
            P[i][i] = P[i - 1][i - 1] * p;
            for (int j = 1; j < i; ++j) {
                P[i][j] = P[i - 1][j] * q + P[i - 1][j - 1] * p;
            }
        }

        for (int i = n; i >= 0; --i) {
            for (int j = n; j >= 0; --j) {
                double ans = 0;
                if (i < m || j < m) {
                    for (int u = 0; i + u <= n; ++u) {
                        for (int v = 0; j + v <= n; ++v) {
                            ans += dp[i + u][j + v] * P[n - i][u] * P[n - j][v];
                        }
                    }
                    dp[i][j] = (ans + 1.0) / (1 - P[n - i][0] * P[n - j][0]);
                }
            }
        }
        cout << fixed << setprecision(6) << dp[0][0] << endl;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值