HDU 6656 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≤10^9, 1≤xi≤i, 0≤ai≤10^9), space separated. Note that pi is given in the form of a fraction r i s i \frac{ri}{si} siri.

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 1e9+7, that is, if the answer is P Q \frac{P}{Q} QP, you should output P⋅Q−1 modulo 1e9+7, where Q−1 denotes the multiplicative inverse of Q modulo 1e9+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.

解题思路:

我们先定义几个量: r , s , x , m r,s,x,m r,s,x,m,分别代表升级概率的分子、分母,退级到第 x x x个,每次需要的钱数 m m m。然后我们再定义 f [ i ] f[i] f[i]表示从第 i i i级升到第 i + 1 i+1 i+1级的期望, s u m [ i ] sum[i] sum[i]表示从第 1 1 1级升到第 i + 1 i+1 i+1级的期望。

如果我们要算出来从第 i i i级升到第 i + 1 i+1 i+1级的期望 f ( i ) f(i) f(i),不妨列一个中学数学学过的期望表格:

m m m m + 从 x 到 i 的 期 望 + f [ i ] m+从x到i的期望+f[i] m+xi+f[i]
r s \frac{r}{s} sr 1 − r s 1-\frac{r}{s} 1sr

x x x i i i的期望,我们可以用 s u m [ i − 1 ] − s u m [ x − 1 ] sum[i-1]-sum[x-1] sum[i1]sum[x1]表示,

所以嘛,我们可以得出以下方程式:
f [ i ] = m × r s + ( m + s u m [ i − 1 ] − s u m [ x − 1 ] + f [ i ] ) × ( 1 − r s ) f[i]=m\times \frac{r}{s}+(m+sum[i-1]-sum[x-1]+f[i])\times (1-\frac{r}{s}) f[i]=m×sr+(m+sum[i1]sum[x1]+f[i])×(1sr)

然后移项化简,得:
f [ i ] = s × m + ( s − r ) ( s u m [ i − 1 ] − s u m [ x − 1 ] ) r f[i]=\frac{s\times m+(s-r)(sum[i-1]-sum[x-1])}{r} f[i]=rs×m+(sr)(sum[i1]sum[x1])
这样我们就可以算出来 s u m sum sum数组了,然后每次询问 x x x y y y的期望,答案就是: s u m [ y − 1 ] − s u m [ x − 1 ] sum[y-1]-sum[x-1] sum[y1]sum[x1]

程序如下

#include 
using namespace std;
typedef long long LL;
const LL maxn = 5*1e5+10;
const long long MOD = 1e9+7;
long long n,q;
struct nodes{
    long long r,s,x,mon;
}val[maxn];
struct node{
    long long x,y;// 表示 x/y
}f;
long long sum[maxn];
inline long long read(){//读入优化
    LL x=0,f=1; char ch=getchar();
    while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
long long exgcd(long long a,long long b,long long &x,long long &y){//拓展欧几里得 
    if(b==0){
        x=1,y=0;
        return a;
    }
    long long ret=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return ret;
}
inline long long getInv(long long a,long long mod){//求a在mod下的逆元,不存在逆元返回-1 
    long long x,y;
    long long d=exgcd(a,mod,x,y);
    return d==1?(x%mod+mod)%mod:-1;
}
inline void outs(long long x){//输出优化
    if(x>=10){
        outs(x/10);
    }
    putchar(x%10+'0');
}
int main(){
    int T;
    T=read();
    while (T--){
        n=read(),q=read();
        for (register int i=1;i<=n;++i){
            val[i].r=read(),val[i].s=read(),val[i].x=read(),val[i].mon=read();    
        }
        
        f.x=(val[1].mon%MOD*val[1].s)%MOD;f.y=val[1].r;    
        sum[0]=0;
        sum[1]=(f.x*getInv(f.y,MOD))%MOD;
        for (register int i=2;i<=n;++i){
            f.x=((val[i].mon%MOD*val[i].s)%MOD+((val[i].s-val[i].r)%MOD*((sum[i-1]-sum[val[i].x-1]+MOD)%MOD)%MOD)%MOD)%MOD;//因为这一块的少MOD了导致WA了好几次,于是这一块有点丧心病狂
            f.y=val[i].r;
            sum[i]=(sum[i-1]+f.x*getInv(f.y,MOD))%MOD;//前缀和
        }
        while (q--){
            int xx,yy;
            xx=read(),yy=read();
            long long ss=(sum[yy-1]-sum[xx-1]+MOD)%MOD;
            outs(ss);
            puts("");
        }            
    }
    return 0;
}

你可能感兴趣的:(acm)