CF1265E Beautiful Mirrors 题解
CF1265E Beautiful Mirrors 题解
题目大意
你有 \(n\) 个点,当你在第 \(i\) 个点时,有 \(p_i\) 的概率到达点 \(i+1\),有 \(1-p_i\) 的概率回到点 1。当到达点 \(n+1\) 时,游戏结束。且期望进行的游戏次数。
\(1\le n\le2\times10^5\)。
题目分析
设 \(f_i\) 表示到达点 \(i\) 后还需进行的期望游戏次数,则有 \(f_{n+1}=0\)。
对于点 \(i(1\le i\le n)\),有
\[f_i=p_if_{i+1}+(1-p_i)f_1+1
\]
对于 \(i=n\) 时,因为有 \(f_{n+1}=0\),所以有
\[f_n=(1-p_n)f_1+1
\]
当 \(i=1\) 时,可以化简为
\[\begin{aligned}
f_1&=p_1f_2+(1-p_1)f_1+1\\
&=f_2+\frac{1}{p_1}
\end{aligned}
\]
将上式带入 \(i=2\) 时,可得
\[\begin{aligned}
f_2&=p_2f_3+(1-p_2)f_1+1\\
&=p_2f_3+(1-p_2)(f_2+\frac{1}{p_1})+1\\
&=f_3+\frac{-p_2+1}{p_1p_2}+\frac{1}{p_1}
\end{aligned}
\]
将上式代入 \(f_1=f_2+\frac{1}{p_1}\),可得
\[f_1=f_3+\frac{p_1+1}{p_1p_2}
\]
类似地,可以求出
\[\begin{aligned}
f_3&=f_4+\frac{-p_1p_3+p_1-p_3+1}{p_1p_2p_3}+\frac{1}{p_3}\\
f_4&=f_5+\frac{-p_1p_2p_4+p_1p_2-p_1p_4+p_1-p_4+1}{p_1p_2p_3p_4}+\frac{1}{p_4}\\
\end{aligned}
\]
同样代入后可得
\[\begin{aligned}
f_1&=f_4+\frac{p_1p_2+p_1+1}{p_1p_2p_3}\\
f_1&=f_5+\frac{p_1p_2p_3+p_1p_2+p_1+1}{p_1p_2p_3p_4}
\end{aligned}
\]
于是,可以发现以下规律
\[f_1=f_i+\frac{\sum_{j=0}^{i-2}\prod_{k=1}^jp_k}{\prod_{j=1}^{i-1}p_j}
\]
则有
\[f_1=f_n+\frac{\sum_{i=0}^{n-2}\prod_{j=1}^ip_j}{\prod_{i=1}^{n-1}p_i}
\]
代入 \(f_n=(1-p_n)f_1+1\),可得
\[f_n=\frac{1-p_n}{p_n}\frac{\sum_{i=0}^{n-2}\prod_{j=1}^ip_j}{\prod_{i=1}^{n-1}p_i}+\frac{1}{p_n}
\]
于是可以先求出 \(f_n\),再求出 \(f_1\)。
代码
//Author:LIUIR
//2024.02.29
//Start coding:19:29:42
//Finish debugging:19:42:15
#include <bits/stdc++.h>
#define int long long
const int N = 2e5 + 5;
const int MOD = 998244353;
int n, ans, sum, p[N], mul[N];
int Pow(int, int);
signed main()
{
int inv100 = Pow(100, MOD - 2);
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &p[i]), p[i] = p[i] * inv100 % MOD;
mul[0] = 1;
for (int i = 1; i <= n; i++)
mul[i] = mul[i - 1] * p[i] % MOD;
for (int i = 0; i < n - 1; i++)
sum = (sum + mul[i]) % MOD;
sum = sum * Pow(mul[n - 1], MOD - 2) % MOD;
ans = (1 - p[n] + MOD) % MOD * Pow(p[n], MOD - 2) % MOD * sum % MOD;
ans = (ans + Pow(p[n], MOD - 2)) % MOD;
ans = (ans + sum) % MOD;
printf("%lld", ans);
return 0;
}
int Pow(int x, int y)
{
int res = 1;
for (; y; x = x * x % MOD, y >>= 1)if (y & 1)
res = res * x % MOD;
return res;
}