CF1265E Beautiful Mirrors (概率dp)
原题链接
思路:
d
p
[
i
]
dp[i]
dp[i]表示走到第
i
i
i个镜子的期望天数:
d
p
[
i
]
=
(
d
p
[
i
−
1
]
+
1
)
∗
p
[
i
]
+
(
1
−
p
[
i
]
)
∗
(
d
p
[
i
−
1
]
+
1
+
d
p
[
i
]
)
dp[i]=(dp[i-1]+1)*p[i]+(1-p[i])*(dp[i-1]+1+dp[i])
dp[i]=(dp[i−1]+1)∗p[i]+(1−p[i])∗(dp[i−1]+1+dp[i])
当这个镜子说她漂亮时,只需要花费
1
1
1天;
否则,需要从起点重新开始,也就是
d
p
[
i
]
dp[i]
dp[i]
化简得:
d
p
[
i
]
=
d
p
[
i
−
1
]
+
1
p
[
i
]
dp[i] = \frac{dp[i-1]+1}{p[i]}
dp[i]=p[i]dp[i−1]+1
其中
p
[
i
]
=
x
100
p[i]=\frac{x}{100}
p[i]=100x (x为输入的数)
注意求逆元的时候要用long long
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+100,mod=998244353;
ll n,p[maxn],dp[maxn];
ll ksm(ll a,ll b){
ll res=1;
while(b){
if(b&1) res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int main(){
ll n;cin>>n;
dp[0]=0;
for(int i=1;i<=n;i++){
cin>>p[i];
}
for(int i=1;i<=n;i++){
dp[i]=(dp[i-1]+1)%mod*100%mod*ksm(p[i],mod-2)%mod;
}
cout<<dp[n]<<endl;
return 0;
}