Kejin Player(期望dp)

问题 K: Kejin Player

时间限制: 2 Sec  内存限制: 128 MB
提交: 42  解决: 26
[提交] [状态] [命题人:admin]

题目描述

Cuber QQ always envies those Kejin players, who pay a lot of RMB to get a higher level in the game. So he worked so hard that you are now the game designer of this game. He decided to annoy these Kejin players a little bit, and give them the lesson that RMB does not always work.

This game follows a traditional Kejin rule of "when you are level i, you have to pay ai RMB to get to level i+1". Cuber QQ now changed it a little bit: "when you are level i, you pay ai RMB, are you get to level i+1 with probability pi; otherwise you will turn into level xi (xi≤i)".

Cuber QQ still needs to know how much money expected the Kejin players needs to ``ke'' so that they can upgrade from level l to level r, because you worry if this is too high, these players might just quit and never return again.

 

输入

The first line of the input is an integer t, denoting the number of test cases.
For each test case, there is two space-separated integers n (1≤n≤500 000) and q (1≤q≤500 000) in the first line, meaning the total number of levels and the number of queries.
Then follows n lines, each containing integers ri, si, xi, ai (1≤ri≤si≤109, 1≤xi≤i, 0≤ai≤109), space separated. Note that pi is given in the form of a fraction risi.
The next q lines are q queries. Each of these queries are two space-separated integers l and r (1≤l<r≤n+1).
The sum of n and sum of q from all t test cases both does not exceed 106.

 

输出

For each query, output answer in the fraction form modulo 109+7, that is, if the answer is P/Q, you should output P⋅Q−1 modulo 109+7, where Q−1 denotes the multiplicative inverse of Q modulo 109+7.

 

样例输入

复制样例数据

1
3 2
1 1 1 2
1 2 1 3
1 3 3 4
1 4
3 4

样例输出

22
12

 

提示

Huge IO (Over 40MB)! IO optimization is preferred.

 

题意:对于当前第i级,花费ai有pi的概率升到i+1级,有(1-pi)的概率回到xi级,求从L级升到R级的花费期望

思路:没想到期望dp成了签到题,o(╥﹏╥)o

期望概率类题目做的并不多,脑子里只有hbz学长的一句话——概率正着求,期望倒着求

但是这个题期望无法倒着求,那就考虑正着求

期望是概率的倒数,我记得是有这么一句话的

dp[i]表示从i-1级升到i级的花费期望
E[i]表示从1级升到i级的花费期望

考虑从i级升到i+1的期望  

有pi的概率升到i+1级,那么也就是说平均需要升级1/pi次才能升到i+1级(就是升级成功所需次数的期望),其中只有一次是成功的,其他都是失败的,那么就需要从xi级再升到i级,每次的花费是E[i]-E[xi],一共1-1/pi次

所以dp[i+1]=1/pi*ai+(1-1/pi)*(E[i]-E[xi])    再把pi=ri/si 代进去,该求逆元求逆元(因为取模了)

同时更新 E[i+1]=E[i]+dp[i+1]

E[i]就是个前缀和,求区间的期望,做差就ok了

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=5e5+5;
ll dp[N];//dp[i]表示从i-1级升到i级的花费期望
ll E[N];//E[i]表示从1级升到i级的花费期望
ll ksm(ll a,ll b){
    ll s=1;
    while(b){
        if(b&1){
            a%=mod;
            s%=mod;
            s*=a;
        }
        a%=mod;
        a*=a;
        b>>=1;
    }
    return s%mod;
}
int main(){
    ios::sync_with_stdio(false);
    cout.tie(NULL);
    int T;
    scanf("%d",&T);
    while(T--){
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++){
            ll r,s,x,a;
            scanf("%lld%lld%lld%lld",&r,&s,&x,&a);
            ll rn=ksm(r,mod-2);
            dp[i+1]=(s*a%mod+(s-r)*(((E[i]-E[x])%mod+mod)%mod)%mod)*rn%mod;    
            E[i+1]=E[i]+dp[i+1];
        }
        while(q--){
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%lld\n",((E[r]-E[l])%mod+mod)%mod);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值