Codeforces 1265E

Description

思路

一开始dp列得乱七八糟,看了题解才知道原来可以设以每个位置为起点的期望。
设dp[i]为以位置i为起点期望天数。可得dp[i] = 1 + (p_i)dp[i] + ((1-p_i))dp[1]
dp[n + 1] = 0,答案为dp[1]。
推一推公式求出dp[1]即可。

by the way:
像这种无限的期望值,一般都是设期望,用递归的思想求解吧。
看了一篇相关博客,感觉挺好,记录一下:link

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 10;
const int M = 998244353;

ll inv(ll x) {
    ll res = 1;
    ll p = M - 2;
    while(p) {
        if(p & 1) res = (res * x) % M;
        x = (x * x) % M;
        p = p >> 1;
    }
    return res;
}

ll p[N];

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        ll v;
        cin >> v;
        p[i] = v * inv(100) % M;
    }
    ll tplus = 1;
    ll tot = 1;
    ll div = (1 - p[1] + M) % M;
    for(int i = 1; i < n; i++) {
        tplus = (tplus * p[i]) % M;
        tot = (tot + tplus) % M;
        div = (div + tplus * (1 - p[i + 1] + M) % M) % M;
    }
    cout << tot * inv((1 - div + M) % M) % M << endl;
}
原文地址:https://www.cnblogs.com/limil/p/12623009.html