Solution: 题解 CF1265E Beautiful Mirrors
一种特殊的概率期望dp方法
设\(e_i\)表示当前在第\(i\)个镜子的期望开心天数,则答案为\(e_1\),而且可以设\(e_{n+1}=0\)
给出\(e_i\)的转移方程(假设\(p_i\)为第\(i\)天镜子回答'beautiful'的概率)
\[e_i=p_ie_{i+1}+(1-p_i)e_1+1,1\leqslant i\leqslant n
\]
移项,以\(e_{i+1}\)为主元
\[e_{i+1}=\dfrac{e_i+(p_i-1)e_1-1}{p_i}
\]
以此可以将所有\(e_i,1\leqslant i\leqslant n+1\)都用\(e_1\)表示,并且用归纳法可知它们均可表示为\(e_i=ae_1+b\)的形式(\(a,b\)为常数)
所以可以初始化\(a=1,b=0\)(即\(e_1=1e_1+0\)),根据输入的\(p_i\)转移\(a,b\)直到最后的\(ae_1+b=e_{n+1}=0\)得到\(e_1=-\frac{b}{a}\)
可以一边读入一边做,所以不需要开任何数组
Time complexity: \(O(n\log P)\)
Memory complexity: \(O(1)\)
上代码
//This program is written by Brian Peng.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define Rd(a) (a=rd())
#define Gc(a) (a=getchar())
#define Pc(a) putchar(a)
int rd(){
int x;char c(getchar());bool k;
while(!isdigit(c)&&c^'-')if(Gc(c)==EOF)exit(0);
c^'-'?(k=1,x=c&15):k=x=0;
while(isdigit(Gc(c)))x=x*10+(c&15);
return k?x:-x;
}
void wr(int a){
if(a<0)Pc('-'),a=-a;
if(a<=9)Pc(a|'0');
else wr(a/10),Pc((a%10)|'0');
}
signed const INF(0x3f3f3f3f),NINF(0xc3c3c3c3);
long long const LINF(0x3f3f3f3f3f3f3f3fLL),LNINF(0xc3c3c3c3c3c3c3c3LL);
#define Ps Pc(' ')
#define Pe Pc('\n')
#define Frn0(i,a,b) for(int i(a);i<(b);++i)
#define Frn1(i,a,b) for(int i(a);i<=(b);++i)
#define Frn_(i,a,b) for(int i(a);i>=(b);--i)
#define Mst(a,b) memset(a,b,sizeof(a))
#define File(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
#define P (998244353)
int n,p,a(1),b;
int fpw(int a,int b){int r(1);while(b)a%=P,b&1?r=r*a%P:0,b>>=1,a*=a;return r;}
int const i100=fpw(100,P-2);
signed main(){
Rd(n);
while(n--)p=rd()*i100%P,a+=p-1,--b,a=a*fpw(p,P-2)%P,b=b*fpw(p,P-2)%P;
wr((P-(b*fpw(a,P-2))%P)%P),exit(0);
}