(概率dp)牛客练习赛39-C

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值