Codeforces Round #604 (Div. 2) E. Beautiful Mirrors
链接:
https://codeforces.com/contest/1265/problem/E
题意:
Creatnx has n mirrors, numbered from 1 to n. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The i-th mirror will tell Creatnx that he is beautiful with probability pi100 for all 1≤i≤n.
Creatnx asks the mirrors one by one, starting from the 1-st mirror. Every day, if he asks i-th mirror, there are two possibilities:
The i-th mirror tells Creatnx that he is beautiful. In this case, if i=n Creatnx will stop and become happy, otherwise he will continue asking the i+1-th mirror next day;
In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the 1-st mirror again.
You need to calculate the expected number of days until Creatnx becomes happy.
This number should be found by modulo 998244353. Formally, let M=998244353. It can be shown that the answer can be expressed as an irreducible fraction pq, where p and q are integers and q≢0(modM). Output the integer equal to p⋅q−1modM. In other words, output such an integer x that 0≤x<M and x⋅q≡p(modM).
思路:
考虑期望Dp,Dp[i]是从第一天到第i天开心的期望天数。
正向推导\(Dp[i] = Dp[i-1]+ 1 * \frac{p_i}{100} + (1 - \frac{p_i}{100})*(Dp[i]+1)\)
化简得\(Dp[i] = \frac{100*(Dp[i-1]+1)}{p_i}\)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 998244353;
const int MAXN = 2e5+10;
LL Dp[MAXN], inv;
int n;
LL ExGcd(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
LL d = ExGcd(b, a%b, x, y);
LL tmp = y;
y = x-(a/b)*y;
x = tmp;
return d;
}
LL GetInv(int a, int b)
{
//a*x = 1 mod b
LL d, x, y;
d = ExGcd(a, b, x, y);
if (d == 1)
return (x%b+b)%b;
return -1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
Dp[0] = 0;
int a;
for (int i = 1;i <= n;i++)
{
cin >> a;
inv = GetInv(a, MOD);
Dp[i] = 100*(Dp[i-1]+1)%MOD*inv%MOD;
}
cout << Dp[n] << endl;
return 0;
}