Kejin Player
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
思路
题意:支付a元有
r
s
r \over s
sr的概率从i级升到i+1级,升级失败则降回x级,问从l升级到r需要多少钱,如出现分数则用逆元表示。
计算前缀和,并求逆元,考虑到求逆元后,下一级比之前等级所需钱更少的情况,求前缀和以及求答案过程中需要进行(sum[r] - sum[l] +mod)%mod防止出现负数。
代码
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 5e5+5;
typedef long long ll;
ll mod = 1000000007;
struct node {
ll r, s, a;
int x;
} b[maxn];
struct node2 {
ll fz, fm, s;
} sum[maxn];
ll qkpow(ll a,ll p,ll mod) {
ll t = 1,tt = a % mod;
while(p) {
if (p & 1) t = t * tt % mod;
tt = tt * tt % mod;
p >>= 1;
}
return t;
}
ll getInv(ll a,ll mod) {
return qkpow(a,mod-2,mod);
}
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%d%lld", &b[i].r, &b[i].s, &b[i].x, &b[i].a);
}
for(int i = 1; i <= n; i++) {
sum[i+1].fz = (b[i].s - b[i].r);
sum[i+1].fm = b[i].r;
sum[i+1].s = (((b[i].a + (sum[i].s - sum[b[i].x].s + mod) % mod ) * sum[i+1].fz) % mod) * getInv(sum[i+1].fm, mod) + b[i].a % mod;
sum[i+1].s += sum[i].s;
sum[i+1].s %= mod;
}
for(int i = 1; i <= q; i++) {
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", (sum[r].s - sum[l].s + mod) % mod);
}
}
}
题目来源
HDU 6656 Kejin Player [2019 Multi-University Training Contest 7]