[Codeforces 1265E]Beautiful Mirrors


Description

題庫鏈接

一共有 \(n\) 個關卡,你初始在第一個關卡。通過第 \(i\) 個關卡的概率為 \(p_i\)。每一輪你可以挑戰一個關卡。若通過第 \(i\) 個關卡,則進入第 \(i+1\) 個關卡,否則重新回到第 \(1\) 個關卡。通過第 \(n\) 個關卡則算成功。問期望多少輪游戲才能成功。

\(1\leq n\leq 2\cdot 10^5\)

Solution

設從第 \(i\) 個關卡通關的期望為 \(E_i\)。顯然
\[ E_i=p_i(E_{i+1}+1)+(1-p_i)(E_1+1) \]

特別地,\(E_{n+1}=0\),且答案為 \(E_1\)

那么有
\[ E_1=p_1(E_2+1)+(1-p_1)(E_1+1)\Rightarrow E_1=\frac{1}{p_1}+E_2 \]

同理將上述式子代入
\[ E_2=p_2(E_3+1)+(1-p_2)(E_2+1)\Rightarrow E_1=\frac{1+\frac{1}{p_1}}{p_2}+E_3 \]

繼續推導可以發現答案為
\[ E_1=\frac{1+\frac{1+\frac{1+\cdots}{p_{n-2}}}{p_{n-1}}}{p_n} \]

Code

#include <bits/stdc++.h>
using namespace std;
const int yzh = 998244353;

int quick_pow(int a, int b) {
    int ans = 1;
    while (b) {
        if (b&1) ans = 1ll*ans*a%yzh;
        b >>= 1, a = 1ll*a*a%yzh;   
    }
    return ans;
}
int main() {
    int ans = 0, p, n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p);
        ans = (ans+1)%yzh;
        ans = 1ll*ans*100%yzh*quick_pow(p, yzh-2)%yzh;
    }
    printf("%d\n", ans);
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM