E - Revenge of "The Salary of AtCoder Inc."
E - Revenge of "The Salary of AtCoder Inc."
Problem Statement
Aoki, an employee at AtCoder Inc., has his salary for this month determined by an integer and a sequence of length as follows.
First, he is given an -sided die (dice) that shows the integers from to with equal probability, and a variable .
Then, the following steps are repeated until terminated.
- Roll the die once and let be the result.
- If , pay him yen and let .
- Otherwise, terminate the process.
Aoki's salary for this month is the total amount paid through this process.
Find the expected value of Aoki's salary this month, modulo .
How to find an expected value modulo
It can be proved that the sought expected value in this problem is always a rational number. Also, the constraints of this problem guarantee that if the sought expected value is expressed as a reduced fraction , then is not divisible by . Here, there is exactly one such that . Print this .Constraints
- All inputs are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3
3 2 6
Sample Output 1
776412280
Here is an example of how the process goes.
- Initially, .
- Roll the die once, and it shows . Since , pay him yen and let .
- Roll the die once, and it shows . Since , pay him yen and let .
- Roll the die once, and it shows . Since , terminate the process.
In this case, his salary for this month is yen.
It can be calculated that the expected value of his salary this month is yen, whose representation modulo is .
Sample Input 2
1
998244352
Sample Output 2
998244352
Sample Input 3
9
3 14 159 2653 58979 323846 2643383 27950288 419716939
Sample Output 3
545252774
解题思路
现在一遇到概率期望题就不会做......
如果直接按照期望的公式来求,则需要把所有合法方案的权重以及对应的概率求出来,很明显这是很困难的。考虑贡献法,由于期望具有线性性,因此可以考虑每个 对期望的贡献是多少,那么答案就是 ,其中 为获得能获得 的概率。
那么在哪些情况下能获得 呢?分情况讨论:
- 如果第 个数就是 ,则对应的概率为 。
- 如果第 个数是 ,那么前面的 个数只能从 中选,则对应的概率为 。
- 如果第 个数是 ,那么前面的 个数只能从 中选,并且要升序排序,则对应的概率为 。
- 如果第 个数是 ,那么前面的 个数只能从 中选,并且要升序排序,则对应的概率为 。
- 如果第 个数是 ,那么前面的 个数只能从 中选,并且要升序排序,则对应的概率为 。
因此 。根据二项式定理有 ,因此所求的期望值就是 。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10, mod = 998244353;
int a[N];
int qmi(int a, int k) {
int ret = 1;
while (k) {
if (k & 1) ret = 1ll * ret * a % mod;
a = 1ll * a * a % mod;
k >>= 1;
}
return ret;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
int ret = 0, p = qmi(n, mod - 2);
for (int i = 1, t = 1; i <= n; i++) {
ret = (ret + 1ll * a[i] * t) % mod;
t = t * (1ll + p) % mod;
}
printf("%d", 1ll * ret * p % mod);
return 0;
}
参考资料
Editorial - Panasonic Programming Contest 2023(AtCoder Beginner Contest 326):https://atcoder.jp/contests/abc326/editorial/7547
AtCoder Beginner Contest 326 A 至 G 題讲解 by dreamoon:https://www.bilibili.com/video/BV1Ez4y1N7p1/
AtCoder Beginner Contest 326 题解:https://www.cnblogs.com/binary1110011/p/17794737.html
题解 ABC326E【Revenge of "The Salary of AtCoder Inc.":https://www.cnblogs.com/ruierqwq/p/abc326e.html
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18002315
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2023-02-01 Put Marbles in Bags
2023-02-01 牡牛和牝牛
2022-02-01 dfs时间复杂度分析