https://ac.nowcoder.com/acm/contest/368/C
现在一共有n天,第i天如果有流星雨的话,会有wi颗流星雨。
第i天有流星雨的概率是pi。如果第一天有流星雨了,那么第二天有流星雨的可能性是p2+P,否则是p2。相应的,如果第i−1(i≥2)天有流星雨,第i天有流星雨的可能是pi+P,否则是pi。求n天后,流星雨颗数的期望。
因为当天的概率只与前一天的概率有关,所以f[i]=f[i-1]*(p[i]+P)+p[i]*(1-f[i-1]).(f[i]为发生流星雨的概率)。注意按题意处理逆元,a/b->a*inv(b)%mod
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 3;
ll A, B, w[maxn], p[maxn], f[maxn];
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, a, b;
cin >> n >> a >> b;
ll P = a * qpow(b, mod - 2) % mod;
for(int i = 1; i <= n; i++) cin >> w[i];
for(int i = 1; i <= n; i++) {
cin >> A >> B;
p[i] = A * qpow(B, mod - 2) % mod;
}
f[1] = p[1];
for(int i = 2; i <= n; i++) {
f[i] = f[i - 1] * ((p[i] + P) % mod) % mod;
f[i] = (f[i] + p[i] * ((1 - f[i - 1] + mod) % mod) % mod) % mod;
}
ll ans = 0;
for(int i = 1; i <= n; i++)
ans = (ans + f[i] * w[i] % mod) % mod;
cout << ans << endl;
}