Kejin Player//HDU - 6656//概率dp/求逆元

Kejin Player//HDU - 6656//概率dp/求逆元


题目

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
题意
给出氪金到下一级升级成功的概率和失败降到的级数,求l到r级的金额期望
链接:https://vjudge.net/contest/349029#problem/J

思路

概率dp,找出方程:dp[i+1]=1/pi * ai+(1/pi-1)*(E[i]-E[xi]),Ei是从1升级到i的期望,dp是升到下一级的期望,中间需要求逆元

代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll dp[500100];
ll E[500100];
ll qn(ll a,ll b) {            //求逆元
	ll s = 1;
	while (b) {
		if (b&1) {
			a%=mod;
			s%=mod;
			s*=a;
		}
		a%=mod;
		a*=a;
		b>>=1;
	}
	return s%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++){
			ll r,s,x,a;
			scanf("%lld%lld%lld%lld",&r,&s,&x,&a);
			ll rn = qn(r, mod - 2);
			dp[i + 1] = (s * a % mod+abs(r-s)*(((E[i]-E[x])%mod+mod)%mod)%mod)*rn% mod;
			E[i + 1] = E[i] + dp[i + 1];
		}
		while (q--){
			int l, r;
			scanf("%d%d",&l,&r);
			printf("%lld\n",((E[r] - E[l])%mod+mod)%mod);
		}
	}
	return 0;
}

注意

发布了50 篇原创文章 · 获赞 19 · 访问量 2584

猜你喜欢

转载自blog.csdn.net/salty_fishman/article/details/104244697