book书 (期望dp)

来源:互联网 发布:大闹天宫化身进阶数据 编辑:程序博客网 时间:2024/06/11 03:21

book书

9.11
思路:
用E[x]表示抽第X本书期望花费的体力值,P[X]表示事件X发生的概率。
Ans=∑pi*Ei
=∑pi*(1+∑p[最后一次被抽到的时间更晚的书的数量])
=∑ pi*(1+∑pj/(pi+pj))

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define LL long longusing namespace std;const LL mod = 1000000007LL;const int N = 100010;int n;LL p[N];LL inv(LL a){    register LL res = 1LL;    register int idx = (int)mod - 2;    for( ; idx; idx>>=1, a = (a * a) % mod){        if(idx & 1)              res = (res * a) % mod;    }    return res;}int main() {    freopen ("book.in", "r", stdin);      freopen ("book.out", "w", stdout);      scanf("%d", &n);    for(register int i=1; i<=n; i++) {        int x, y; scanf("%d%d", &x, &y);        p[i] = x * inv(y) % mod;    }    LL ans = 0;    for(register int i=1; i<=n; i++) {        LL dep = 1;        for(register int j=1; j<=n; j++) {            if(i == j) continue;            dep = (dep + 1LL * p[j] * (inv(p[i] + p[j]) % mod) % mod) % mod;            //很长的时间之后,抽j书的次数比上抽i书的次数近乎=p[j]/p[i]            //所以设抽j书的p[j]次,抽i书的p[i]次,总共抽了p[j]+p[i]次            //那么最后一次抽中j的p=p[j]/(p[i] + p[j])        }        ans = (ans + p[i] * dep % mod) % mod;//i近乎是从上至下第dep本book     }    printf("%I64d", ans);    return 0;}
原创粉丝点击