Kejin Player(HDU-6656)

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

题意:t 组数据,每组给出 n 组数据与 q 组查询,对于每组数据,有 r[i]、s[i]、x[i]、a[i] 四个数,代表从第 i 级升到第 i+1 级需要花费 a[i],成功的概率为 r[i]/p[i],如果失败了则降级到 x[i],对于每组询问,给出 l[i]、r[i],询问从 l[i] 级升到 r[i] 级的期望

思路:

设 E(l,r) 为从 l 升级到 r 的期望,由于升级只能一级一级的升,因此从 1 升级到 r 必然要经过 l,那么满足:E(l,r)=E(1,r)-E(1,l)

因此,可以由 dp[l][r] 表示从 l 升到 r 的期望降维,用 dp[i] 表示从 1 级升到 i 级的期望,因此,E(l,r)=dp[r]-dp[l]

从 dp[i] 转移到 dp[i+1],假设用了 t 次成功,那么前面的 t-1 次全是失败的,因此,下一状态花费为当前花费+成功的花费+失败的花费+失败后回到前面 x[i] 状态的花费

即有:dp[i+1] = dp[i] + a[i] + (t-1)*a[i] + (t-1)*( dp[i]-dp[x[i]] )

那么,第 t 次成功,第 t-1 次不成功,可以理解为总共用了 t 次,有 t-1 次不成功,即:(t-1)/t = 1-r[i]/s[i],有 t=s[i]/r[i]

于是,状态转移方程即为:dp[i+1] = dp[i] + s[i]/r[i]*a[i] + (s[i]/r[i]-1)*( dp[i]-dp[x[i]] )

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 1000000000+7;
const int N = 500000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

LL r[N], s[N], x[N], a[N];
LL dp[N];
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 inv = getInv(r[i], MOD);
            LL t = (s[i] % MOD * inv % MOD) % MOD;
            LL temp1 = dp[i] % MOD; //前一状态
            LL temp2 = (t % MOD * a[i] % MOD) % MOD; //成功花费与失败花费的和
            LL temp3 = ((t - 1 + MOD) % MOD * (dp[i] - dp[x[i]] + MOD) % MOD) % MOD; //失败后回到x[i]状态的花费
            dp[i + 1] = (temp1 + temp2 + temp3 + MOD) % MOD;
        }

        for (int i = 1; i <= q; i++) {
            int left, right;
            scanf("%d%d", &left, &right);
            LL res = (dp[right] - dp[left] + MOD) % MOD;
            printf("%lld\n", res);
        }
    }
    return 0;
}
发布了1871 篇原创文章 · 获赞 702 · 访问量 194万+

猜你喜欢

转载自blog.csdn.net/u011815404/article/details/102748890