2019 杭电多校 7 1011

题目链接:HDU 6656

比赛链接:2019 Multi-University Training Contest 7

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 \(a_i\) RMB to get to level \(i+1\)". Cuber QQ now changed it a little bit: "when you are level \(i\), you pay \(a_i\) RMB, are you get to level \(i+1\) with probability \(p_i\); otherwise you will turn into level \(x_i (x_i\le 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\le n\le 500 000)\) and \(q (1\le q\le 500 000)\) in the first line, meaning the total number of levels and the number of queries.

Then follows \(n\) lines, each containing integers \(r_i, s_i, x_i, a_i\) \((1\le r_i\le s_i\le 10^9, 1\le x_i\le i, 0\le a_i\le 10^9)\), space separated. Note that \(p_i\) is given in the form of a fraction \(\frac{r_i}{s_i}\).

The next \(q\) lines are \(q\) queries. Each of these queries are two space-separated integers \(l\) and \(r\) \((1\le l < r\le n+1)\).

The sum of \(n\) and sum of \(q\) from all \(t\) test cases both does not exceed \(10^6\).

Output

For each query, output answer in the fraction form modulo \(10^9+7\), that is, if the answer is \(\frac{P}{Q}\), you should output \(P\cdot Q^{−1}\) modulo \(10^9+7\), where \(Q^{−1}\) denotes the multiplicative inverse of \(Q\) modulo \(10^9+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

Solution

题意:

从 \(i\) 级升级到 \(i + 1\) 级需要花费 \(a_i\) RMB,成功的概率为 \(p_i = \frac{r_i}{s_i}\),若失败则降到 \(x_i\) 级,然后给出 \(q\) 个询问求 \(l\) 级升级到 \(r\) 级花费的期望。

题解:

期望DP 逆元

设 \(g(l, r)\) 为 \(l\) 升到 \(r\) 的期望,这种期望满足减法 \(g(l, r) = g(1, r) − g(1, l)\)。因为升级只能一级一级升, 所以要从 \(1\) 升级到 \(r\), 必然要经过 \(l\)。可以降维,用 \(dp[i]\) 表示从 \(1\) 升到 \(i\) 的期望,则 \(g(l, r) = dp[r] − dp[l]\)。

从 \(dp[i]\) 转移至 \(dp[i + 1]\),假设尝试了 \(t\) 次才成功,那么也就是前面 \(t - 1\) 次都是失败的,所以下一状态的花费为当前状态的花费 + 成功的花费 + 失败的花费 + 失败后再次回到当前状态的花费。于是:

\[dp[i + 1] = dp[i] + 1 \times a[i] + (t - 1) \times a[i] + (t- 1) \times (dp[i] - dp[x_i])
\]

又 \(\frac{t - 1}{t} = 1 - \frac{r_i}{s_i}\),即 \(t = \frac{s_i}{r_i}\)

于是状态转移方程为:

\[dp[i + 1] = dp[i] + \frac{s_i}{r_i} \times a[i] + (\frac{s_i}{r_i} - 1) \times (dp[i] - dp[x_i])
\]

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
const ll mod = 1e9 + 7; ll r[maxn], s[maxn], x[maxn], a[maxn]; ll dp[maxn]; ll qmod(ll a, ll b, ll p) {
ll ans = 1;
while(b) {
if(b & 1) ans = (a * ans) % p;
a = (a * a) % p;
b >>= 1;
}
return ans;
} int main() {
int T;
cin >> 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 t = (s[i] * qmod(r[i], mod - 2, mod)) % mod;
dp[i + 1] = (dp[i] + (t * a[i]) % mod + ((t - 1) * (dp[i] - dp[x[i]])) % mod + mod) % mod;
}
for(int i = 0; i < q; ++i) {
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", (dp[r] - dp[l] + mod) % mod);
}
}
return 0;
}

HDU 6656 Kejin Player (期望DP 逆元)的更多相关文章

  1. 杭电多校HDU 6656 Kejin Player(概率DP)题解

    题意: 最低等级\(level\ 1\),已知在\(level\ i\)操作一次需花费\(a_i\),有概率\(p_i\)升级到\(level\ i+1\),有\(1 - p_i\)掉级到\(x_i( ...

  2. 2019杭电多校第七场 HDU - 6656 Kejin Player——概率&&期望

    题意 总共有 $n$ 层楼,在第 $i$ 层花费 $a_i$ 的代价,有 $pi$ 的概率到 $i+1$ 层,否则到 $x_i$($x_i \leq 1$) 层.接下来有 $q$ 次询问,每次询问 $ ...

  3. HDU 6656 Kejin Player

    hdu题面 Time limit 5000 ms Memory limit 524288 kB OS Windows 解题思路 因为升级只能一级一级地升,所以所求期望满足了区间加的性质,可以一级一级地 ...

  4. 2019 Multi-University Training Contest 7 Kejin Player 期望dp

    题目传送门 题意:有n个等级,在每个等级花费$ai$的代价有$pi$的几率升到$i+1$级,$1-pi$的概率降级降到$xi$(xi<=i),给出q次询问,每次询问从$l$级到$r$级的代价的期 ...

  5. HDU 4405 Aeroplane chess 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...

  6. HDU 3853 LOOPS:期望dp【网格型】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意: 有一个n*m的网格. 给出在每个格子时:留在原地.向右走一格,向下走一格的概率. 每走一 ...

  7. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  8. hdu多校第七场 1011 (hdu6656) Kejin Player 概率dp

    题意: 一个游戏,有许多关,到下一关要花费金钱,做出尝试,有概率成功,若成功则到达下一关,若失败则停在此关或退回到前面某关,询问第l关到第r关的期望费用 题解: 显然,第r关到第l关的费用是dp[r] ...

  9. HDU 5781 ATM Mechine 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 ATM Mechine Time Limit: 6000/3000 MS (Java/Othe ...

随机推荐

  1. (转)GitHub上想下载单个文件方法

    找到该文件,单机raw,如下图: 然后会在网页打开该文件,复制URL,下载即可(如果是不可预览文件,会自动下载). 转自: GitHub上想下载单个文件方法 - Smallcaff的博客 - CSDN ...

  2. windbg bp condition

    0:000> bp 0012f2fc "j @ecx == 0 '';'gc'" 0:000> g j代表judgement,与c++中的condition?A:B类似 ...

  3. Device Drivers

    Types of Device Drivers Windows可能会有User-mode的驱动,但是我们只关注Kernel-Mode的驱动. WDM Drivers WDM是一种驱动模型,是比较常用的 ...

  4. Python科学计算:用NumPy快速处理数据

    创建数组 import numpy as np a=np.array([1,2,3]) b=np.array([[1,2,3],[4,5,6],[7,8,9]]) b[1,1]=10 print(a. ...

  5. USACO Milk Routing /// 优先队列广搜

    题目大意: 在n个点 m条边的无向图中 需要运送X单位牛奶 每条边有隐患L和容量C 则这条边上花费时间为 L+X/C 求从点1到点n的最小花费 优先队列维护 L+X/C 最小 广搜到点n #inclu ...

  6. Hibernate4教程六(2):基本实现原理

    整体流程 1:通过configuration来读cfg.xml文件 2:得到SessionFactory 工厂 3:通过SessionFactory 工厂来创建Session实例 4:通过Sessio ...

  7. 将 XML 架构(XSD)附加到Word文档

    附加到文档中的 XML 架构是为您的组织进行自定义而设计的.XML 架构通常由 IT 专业人员创建,他们的职责就是在 Word 中为您的组织构建专用的模板或解决方案. 可用于附加到文档的架构在架构库中 ...

  8. ASE19 团队项目 alpha 阶段 Frontend 组 scrum7 记录

    本次会议于11月11日,11:30 在微软北京西二号楼13158,持续15分钟. 与会人员:Jingyi Xie, , Ziwei Wu, Jiaqi Xu, Jingwei Yi, Hanyue T ...

  9. WPF 从服务器下载文件

    1.先获取服务器下载地址,给出要下载到的目标地址 public void DownloadFileFromServer() { string serverFilePath = "http:/ ...

  10. 【Luogu】【关卡2-16】线性动态规划(2017年10月)【还差三道题】

    任务说明:这也是基础的动态规划.是在线性结构上面的动态规划,一定要掌握. P1020 导弹拦截 导弹拦截 P1091 合唱队形 老师给同学们排合唱队形.N位同学站成一排,音乐老师要请其中的(N-K)位 ...