hdu6656概率dp

 

Kejin Player

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


 

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
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

Hint

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

 

 

Source

2019 Multi-University Training Contest 7

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6742 6741 6740 6739 6738 

 

题意:

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

设dp[i]表示升级到i级花费的期望,一级一级升,期望有线性,dp[r]-dp[l]即为最终要求答案,设第t次升级到i级,dp[i+1]=dp[i]+a[i]+(t-1)*(dp[i]-dp[x[i]+a[i]) (t-1)/t可以表示为1-ri/si,然后求解即可,注意一下逆元

#include
#include
#include
#include
#define ll long long
#define maxn 500005
using namespace std;
int t;
int n,q;
ll dp[maxn];
ll r[maxn];
ll s[maxn];
ll x[maxn];
ll a[maxn];
const ll mod=1e9+7;
ll mul(ll a,ll b)
{ll ans=0;
    while(b)
    {
        if(b&1)
        ans=(ans+a)%mod;
          b>>=1;
        a=(a+a)%mod;
    }
    return ans%mod;
}
ll qpow(ll a,ll b)
{ll ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=ans*a%mod;

        }
        b>>=1;
        a=a*a%mod;
    }
    return ans%mod;
}
int main()
{scanf("%d",&t);
while(t--)
{scanf("%d%d",&n,&q);
dp[1]=0;
for(int i=1;i<=n;i++)
{
    scanf("%lld%lld%lld%lld",&r[i],&s[i],&x[i],&a[i]);
    ll t=mul(s[i],qpow(r[i],mod-2));
    dp[i+1]=(t*dp[i]%mod+t*a[i]%mod-(t-1)*dp[x[i]]%mod+mod)%mod;
}
    while(q--)
    {
        ll l,r;
       scanf("%lld%lld",&l,&r);
       printf("%lld\n",(dp[r]-dp[l]+mod)%mod);
    }
}
return 0;

}

 

你可能感兴趣的:(概率)