CF1265E Beautiful Mirrors 题解
Solution 1
设 \(f_i\) 为从 \(1\) 问到第 \(i\) 个镜子,且通过了第 \(i\) 个镜子的期望天数。
\(f_0 = 0\)
设 \(P_i = \frac{p_i}{100}\) \[ f_i = f_{i-1} + P_i \cdot 1 + (1-P_i) \cdot (1 + f_i ) \]
\[ f_i = \frac{f_{i-1}+ 1}{P_i} \]
\[ f_i = \frac{f_{i-1}+1}{\frac{p_i}{100}} = \frac{(f_{i-1}+1) \cdot 100}{p_i} \]
预处理 \(p_i\) 的逆元即可。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5, P=100, mod=998244353;
int t, n, m, p[N], inv[N], f[N];
int read() {
int a=0, f=1; char c=getchar();
while(!isdigit(c)) {
if(c=='-') f=-1;
c=getchar();
}
while(isdigit(c)) a=a*10+c-'0', c=getchar();
return a;
}
signed main() {
n=read();
for(int i=1;i<=n;++i) p[i]=read();
inv[0]=inv[1]=1;
for(int i=2;i<=100;++i) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
for(int i=1;i<=n;++i) {
f[i]=(f[i-1]+1)%mod*P%mod*inv[p[i]]%mod;
}
printf("%lld\n",f[n]);
}
Solution 2
根据期望的线性性,设 \(f_i\) 表示 \((i-1 \rightarrow i)\) 的期望步数。
设 \(S_i = \sum_{j=1}^{i-1} f_j\) \[ f_i = P_i \cdot 1 + (1-P_i) \cdot ( 1 + \sum_{j=1}^{i-1} f_j +f_i) \]
\[ f_i = P_i + (1-P_i) \cdot (1 + S_i + f_i) \]
\[ f_i = \frac{(1-P_i) \cdot S_i + 1}{P_i} \]
\[ f_i = \frac{(1-\frac{p_i}{100}) \cdot S_i + 1}{\frac{p_i}{100}} = \frac{(100-p_i) \cdot S_i + 100}{p_i} \]
简单维护 \(S\),最后答案为 \(\sum_{i=1}^n f_i\)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5, P=100, mod=998244353;
int t, n, m, p[N], inv[N], f[N];
int read() {
int a=0, f=1; char c=getchar();
while(!isdigit(c)) {
if(c=='-') f=-1;
c=getchar();
}
while(isdigit(c)) a=a*10+c-'0', c=getchar();
return a;
}
signed main() {
n=read();
for(int i=1;i<=n;++i) p[i]=read();
inv[0]=inv[1]=1;
for(int i=2;i<=100;++i) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
int s=0;
for(int i=1;i<=n;++i) {
f[i]=(100+(100-p[i])*s)*inv[p[i]]%mod;
(s+=f[i])%=mod;
}
printf("%lld\n",s);
}
CF1265E Beautiful Mirrors 题解
https://yozora0908.github.io/2022/cf1265e-solution/