HDU 6656 (期望DP 逆元)

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 RMB to get to level ". Cuber QQ now changed it a little bit: "when you are level , you pay RMB, are you get to level with probability ; otherwise you will turn into level ".

Cuber QQ still needs to know how much money expected the Kejin players needs to ``ke'' so that they can upgrade from level to level , 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 and in the first line, meaning the total number of levels and the number of queries.

Then follows lines, each containing integers , space separated. Note that is given in the form of a fraction .

The next lines are queries. Each of these queries are two space-separated integers and .

The sum of and sum of from all test cases both does not exceed .

Output

For each query, output answer in the fraction form modulo , that is, if the answer is , you should output modulo , where denotes the multiplicative inverse of modulo .

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

题意:

级升级到 级需要花费 RMB,成功的概率为 ,若失败则降到 级,然后给出 个询问求 级升级到 级花费的期望。

题解:

期望DP 逆元

升到 的期望,这种期望满足减法 。因为升级只能一级一级升, 所以要从 升级到 , 必然要经过 。可以降维,用 表示从 升到 的期望,则

转移至 ,假设尝试了 次才成功,那么也就是前面 次都是失败的,所以下一状态的花费为当前状态的花费 + 成功的花费 + 失败的花费 + 失败后再次回到当前状态的花费。于是:

,即

于是状态转移方程为:

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;
}
全部评论

相关推荐

秋招进行到现在终于能写总结了。完全没想到战线会拉这么长,过程会如此狼狈,不过更应该怪自己太菜了。好在所有的运气都用在了最后,也是有个去处。背景:双2本硕科班,无竞赛,本科一段研究所实习,硕士一段大厂暑期实习但无转正。技术栈是C++&nbsp;&amp;&nbsp;Golang,实习是客户端音视频(而且是鸿蒙端开发),简历两个C++项目一个Golang项目。主要投递岗位:后端,cpp软开,游戏服务端,测开,以及一些不拘泥于Java的岗位。从8月起总共投递123家公司,笔试数不清了,约面大约30家。offer/oc/意向:友塔游戏(第一个offer,面试体验很好,就是给钱好少南瑞继保(计算机科班点击就送(限男生),不...
乡土丁真真:佬很厉害,羡慕~虽然我还没有到校招的时候,也想讲一下自己的看法:我觉得不是CPP的问题,佬的背书双2,技术栈加了GO,有两段实习。投了123,面了30.拿到11个offer。这个数据已经很耀眼了。这不也是CPP带来的吗?当然也不止是CPP。至少来说在这个方向努力过的也会有好的结果和选择。同等学历和项目选java就会有更好的吗?我个人持疑问态度。当然CPP在方向选择上确实让人头大,但是我觉得能上岸,至于最后做什么方向,在我看来并不重要。至于CPP特殊,有岗位方向的随机性,java不是不挑方向,只是没得选而已。也希望自己以后校招的时候能offer满满
点赞 评论 收藏
分享
面试摇了我吧:啊哈哈面试提前五个小时发,点击不能参加就是放弃
点赞 评论 收藏
分享
10-11 17:30
湖南大学 C++
我已成为0offer的糕手:羡慕
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务