Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1830 Accepted Submission(s): 745
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
一个人在i等级可以花费ai来获得pi的概率升到i+1等级,也有(1-pi)的概率掉到xi等级,给了Q次查询,问L到R等级的期望花费。
设dp[i]表示到N+1等级还需要多少花费。
递推式子很显然的推出来。
但是对于这个式子,我们不好处理。
又因为发现,可以发现当i为1的时候,xi肯定是1。
所以我们可以容易的得到dp[1]和dp[2]的转移方程。
而对于i>1的来说,
这个式子中的dp[xi]是可以从前面顺序递推中得到关系的,我们可以按照顺序慢慢地得到和的关系。然后用迭代,可以将转化成关于的式子,然后带进去计算可以得到:
然后我们递推维护sum数组就行了。
#include
using namespace std;
typedef long long ll;
const int maxn=5e5+5;
const int mod=1e9+7;
ll sum[maxn];
ll r[maxn],s[maxn],x[maxn],a[maxn];
ll dp[maxn];
ll quickmod(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b%2==1)
ans=ans*a%mod;
b=b/2;
a=a*a%mod;
}
return ans;
}
int main()
{
int t,n,q,L,R;
scanf("%d",&t);
while(t--)
{
memset(sum,0,sizeof(sum));
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d",&r[i],&s[i],&x[i],&a[i]);
if(i==1)
{
ll p=(r[i]*quickmod(s[i],mod-2))%mod;
sum[i+1]=(a[i]*quickmod(p,mod-2))%mod;
}
else
{
ll p=(r[i]*quickmod(s[i],mod-2))%mod;
ll tmp=((1-p+mod)%mod*(sum[i]-sum[x[i]]+mod)%mod+a[i])*quickmod(p,mod-2)%mod;
sum[i+1]=(sum[i]+tmp+mod)%mod;
}
}
dp[n+1]=0;
for(int i=n;i>=1;i--)
dp[i]=(dp[i+1]+(sum[i+1]-sum[i])+mod)%mod;
for(int i=1;i<=q;i++)
{
scanf("%d%d",&L,&R);
printf("%lld\n",(dp[L]-dp[R]+mod)%mod);
}
}
return 0;
}