G - Kejin Player HDU—6656(概率DP&&逆元(区间升级期望))

HDU—6656

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 ii, you have to pay aiai RMB to get to level i+1i+1”. Cuber QQ now changed it a little bit: “when you are level ii, you pay aiai RMB, are you get to level i+1i+1 with probability pipi; otherwise you will turn into level xixi (xi≤ixi≤i)”.

Cuber QQ still needs to know how much money expected the Kejin players needs to ``ke’’ so that they can upgrade from level ll to level rr, because you worry if this is too high, these players might just quit and never return again.
Input
The first line of the input is an integer tt, denoting the number of test cases.

For each test case, there is two space-separated integers nn (1≤n≤500 0001≤n≤500 000) and qq (1≤q≤500 0001≤q≤500 000) in the first line, meaning the total number of levels and the number of queries.

Then follows nn lines, each containing integers riri, sisi, xixi, aiai (1≤ri≤si≤1091≤ri≤si≤109, 1≤xi≤i1≤xi≤i, 0≤ai≤1090≤ai≤109), space separated. Note that pipi is given in the form of a fraction risirisi.

The next qq lines are qq queries. Each of these queries are two space-separated integers ll and rr (1≤l<r≤n+11≤l<r≤n+1).

The sum of nn and sum of qq from all tt test cases both does not exceed 106106.
Output
For each query, output answer in the fraction form modulo 109+7109+7, that is, if the answer is PQPQ, you should output P⋅Q−1P⋅Q−1 modulo 109+7109+7, where Q−1Q−1 denotes the multiplicative inverse of QQ modulo 109+7109+7.
Sample Input
1
3 2
1 1 1 2
1 2 1 3
1 3 3 4
1 4
3 4
Sample Output
22
12

题意
从 i级升级到 i+1 级需要花费 ai RMB,成功的概率为 pi=ri/si,若失败则降到 xi级,然后给出 q 个询问求 ll 级升级到 rr 级花费的期望。

题解
期望DP 逆元

设 g(l,r) 为 l 升到r 的期望,这种期望满足减法 g(l,r)=g(1,r)−g(1,l)。因为升级只能一级一级升, 所以要从 1 升级到 r, 必然要经过l。可以降维,用 dp[i] 表示从 1 升到 i 的期望,则 g(l,r)=dp[r]−dp[l]。

从 dp[i] 转移至 dp[i+1],假设尝试了 t 次才成功,那么也就是前面 t−1 次都是失败的,所以下一状态的花费为当前状态的花费 + 成功的花费 + 失败的花费 + 失败后再次回到当前状态的花费。于是:

dp[i+1]=dp[i]+1×a[i]+(t−1)×a[i]+(t−1)×(dp[i]−dp[xi])
又 (t−1)/t=1−ri/si,即 t=si/ri

于是状态转移方程为:

dp[i+1]=dp[i]+si/ri×a[i]+(si/ri−1)×(dp[i]−dp[xi])

#include <iostream>
#include <algorithm>
#include <cmath>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
const ll mod = 1e9+7;

ll r[maxn],s[maxn],x[maxn],a[maxn];
ll dp[maxn];

ll qmod(ll a,ll b,ll p)//求逆元(a的逆对p取余)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=(a*ans)%p;
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld%lld%lld%lld",&r[i],&s[i],&x[i],&a[i]);
            ll t=(s[i]*qmod(r[i],mod-2,mod))%mod;
            dp[i+1]=(dp[i]+(t*a[i])%mod+((t-1)*(dp[i]-dp[x[i]]))%mod+mod)%mod;

        }
        for(int i=0;i<q;i++)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%lld\n",(dp[r]-dp[l]+mod)%mod);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值