HDU6656 Kejin Player

Kejin Player

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1082    Accepted Submission(s): 423


 

Problem Description

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.

 

 

Input

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 .

 

 

Output

For each query, output answer in the fraction form modulo 109+7 , that is, if the answer is PQ , you should output P⋅Q−1 modulo 109+7 , where Q−1 denotes the multiplicative inverse of Q modulo 109+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
HintHuge IO (Over 40MB)! IO optimization is preferred.

题意:(游戏升级)共有n+1个级别,给出前n个级别的属性,分别为r,s,x,a( r/s :成功升至第i+1级的概率,x :若升级不成功,则掉至第x级,且x比当前 i 小,a:由当前级升至第i+1级时所需要的花费)。现给出q个询问:每次求从第 L 级升至 R 级需要钱的期望。

思路:因为只能一级一级的往上升,所以存在递推关系:(队内大佬教的)

                        dp[i+1] = (r/s)*(dp[i] + a)+(1 - r/s)*( dp[i]+a+dp[i+1]-dp[x] )

                        (前者为成功需要的钱,后者为不成功所需要的钱)

            =>        dp[i+1] = dp[i] + a + (1 - r/s)*( dp[i+1] - dp[x] )

                         (dp[i]表示升至第i级所需要的金钱期望)

化简得  =>       dp[i+1] = (s/r)*( dp[i] + a - dp[x] ) + dp[x+1]

注意:该题给的数很大,在每次算加法时都要防止所得的数为负数,故要(+modd)%modd,在这里我得到了惨痛的狂wa

baymax

并且此题涉及到(a/b)%modd 故要要用到求逆元算法(详见此处  悄悄推销自己哈哈哈)

代码:

#include <bits/stdc++.h>
using namespace std;
const int modd=1e9+7;
const int maxn=500005;
long long dp[500005];

long long qkpow(long long a,long long p,long long m)
{
    long long t=1,tt=a%m;
    while(p)
    {
        if(p&1)t=t*tt%m;
        tt=tt*tt%m;
        p>>=1;
    }
    return t;
}
//快速幂求逆元
long long getInv2(long long a,long long mod)
{
    return qkpow(a,mod-2,mod);
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    int n,q,r,s,x,a;
    long long tmp;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&q);
        dp[1]=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d%d",&r,&s,&x,&a);
            dp[i+1]=(dp[i]+a+modd)%modd;//防止得到负数
            dp[i+1]=(dp[i+1]+(r-s)%modd*getInv2(s,modd)%modd*dp[x]%modd+modd)%modd;
            dp[i+1]=(dp[i+1]%modd*s%modd*getInv2(r,modd)%modd)%modd;

        }
        int xx,yy;
        for(int i=0; i<q; i++)
        {
            scanf("%d%d",&xx,&yy);
            printf("%lld\n",(dp[yy]-dp[xx]+modd)%modd);
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值