首页 > 其他分享 >E - Revenge of "The Salary of AtCoder Inc."

E - Revenge of "The Salary of AtCoder Inc."

时间:2024-02-01 23:12:26浏览次数:28  
标签:Salary AtCoder frac int 个数 Revenge cdot 998244353

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 $N$ and a sequence $A$ of length $N$ as follows.
First, he is given an $N$-sided die (dice) that shows the integers from $1$ to $N$ with equal probability, and a variable $x=0$.

Then, the following steps are repeated until terminated.

  • Roll the die once and let $y$ be the result.
    • If $x<y$, pay him $A_y$ yen and let $x=y$.
    • 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 $998244353$.

How to find an expected value modulo $998244353$ 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 $\frac yx$, then $x$ is not divisible by $998244353$. Here, there is exactly one $0\leq z\lt998244353$ such that $y\equiv xz\pmod{998244353}$. Print this $z$.

Constraints

  • All inputs are integers.
  • $1 \le N \le 3 \times 10^5$
  • $0 \le A_i < 998244353$

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\dots$ $A_N$

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, $x=0$.
  • Roll the die once, and it shows $1$. Since $0<1$, pay him $A_1 = 3$ yen and let $x=1$.
  • Roll the die once, and it shows $3$. Since $1<3$, pay him $A_3 = 6$ yen and let $x=3$.
  • Roll the die once, and it shows $1$. Since $3 \ge 1$, terminate the process.

In this case, his salary for this month is $9$ yen.

It can be calculated that the expected value of his salary this month is $\frac{49}{9}$ yen, whose representation modulo $998244353$ is $776412280$.


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

 

解题思路

  现在一遇到概率期望题就不会做......

  如果直接按照期望的公式来求,则需要把所有合法方案的权重以及对应的概率求出来,很明显这是很困难的。考虑贡献法,由于期望具有线性性,因此可以考虑每个 $a_i$ 对期望的贡献是多少,那么答案就是 $\sum\limits_{i=1}^{n}{a_i \cdot p_i}$,其中 $p_i$ 为获得能获得 $a_i$ 的概率。

  那么在哪些情况下能获得 $a_i$ 呢?分情况讨论:

  • 如果第 $1$ 个数就是 $a_i$,则对应的概率为 $\frac{1}{n}$。
  • 如果第 $2$ 个数是 $a_i$,那么前面的 $1$ 个数只能从 $[1, i-1]$ 中选,则对应的概率为 $\frac{C_{i-1}^{1}}{n} \cdot \frac{1}{n}$。
  • 如果第 $3$ 个数是 $a_i$,那么前面的 $2$ 个数只能从 $[1, i-1]$ 中选,并且要升序排序,则对应的概率为 $\frac{C_{i-1}^{2}}{n^2} \cdot \frac{1}{n}$。
  • $\ldots$
  • 如果第 $k$ 个数是 $a_i$,那么前面的 $k-1$ 个数只能从 $[1, i-1]$ 中选,并且要升序排序,则对应的概率为 $\frac{C_{i-1}^{k-1}}{n^{k-1}} \cdot \frac{1}{n}$。
  • $\ldots$
  • 如果第 $i$ 个数是 $a_i$,那么前面的 $i-1$ 个数只能从 $[1, i-1]$ 中选,并且要升序排序,则对应的概率为 $\frac{C_{i-1}^{i-1}}{n^{i-1}} \cdot \frac{1}{n}$。

  因此 $p_i = \frac{1}{n} \cdot \sum\limits_{j=1}^{i}{\frac{C_{i-1}^{j-1}}{n^{j-1}}} = \frac{1}{n} \cdot \sum\limits_{j=0}^{i-1}{\frac{C_{i-1}^{j}}{n^{j}}}$。根据二项式定理有 $\frac{1}{n} \cdot \sum\limits_{j=0}^{i-1}{\frac{C_{i-1}^{j}}{n^{j}}} = \frac{1}{n} \cdot \left( 1 + \frac{1}{n} \right)^{i-1}$,因此所求的期望值就是 $\frac{1}{n} \cdot \sum\limits_{i=1}^{n}{a_i \cdot \left( 1 + \frac{1}{n} \right)^{i-1}}$。

  AC 代码如下,时间复杂度为 $O(n)$:

#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

标签:Salary,AtCoder,frac,int,个数,Revenge,cdot,998244353
From: https://www.cnblogs.com/onlyblues/p/18002315

相关文章

  • AtCoder Beginner Contest 330 ABCDE
    AtCoderBeginnerContest330ABCDEA-CountingPasses签到题,不多说。//AConemoretimes//nndbk#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintmod=1e9+7;constintN=2e5+10;intmain(){ios::sync_with_stdi......
  • AtCoder Beginner Contest 336
    ABC336总结AtCoderBeginnerContest336A-LongLoong翻译给定一个数\(n\),请输出一个由一个L、\(n\)个o、一个n和一个g组成的字符串(区分大小写)。分析按题意模拟即可。code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1......
  • AtCoder Beginner Contest 337 D
    AtCoderBeginnerContest337DD-CheatingGomokuNarabe题意:\(W\)列的矩阵,矩阵由o、x、.三种字符组成。你可以进行若干次操作(可以不做),每次操作可以把矩阵中的一个.改成o。请问最少经过多少次操作后,能在矩阵中找到位于同一行或同一列的连续\(K\)个o。思路:这题......
  • AtCoder Regular Contest 170
    A贪心。首先判无解。如果一个位置要变成A,那么它右边必须要有个B。如果一个位置要变成B,那么它右边必须要有个A。然后算答案,首先有一个明显的上限:\[\sum_{i=1}^{n}[s_i\neqt_i]\]假设我们有\(i\)位置要变成A,\(j\)位置要变成B,且\(i<j\),那么可以减少一次操作,扫一遍......
  • AtCoder Beginner Contest 338
    ABC338总结A-Capitalized?翻译给你一个由大写和小写英文字母组成的非空字符串\(S\)。请判断是否满足以下条件:\(S\)的第一个字符是大写字母,其他所有字符都是小写字母。如果满足,输出Yes,否则输出No。分析按题目说的判断即可。code#include<bits/stdc++.h>usingn......
  • AtCoder Beginner Contest 337
    ABC337总结A.Scoreboard翻译Takahashi队和Aoki队进行了\(N\)场比赛。在\(i\)/th比赛\((1\leqi\leqN)\)中,Takahashi队得了\(X_i\)分,Aoki队得了\(Y_i\)分。在\(N\)场比赛中总得分较高的队伍获胜。输出获胜者。如果两队总得分相同,则输出Draw。分析将得分分别加起来......
  • AtCoder Beginner Contest 338 c题二分思路
    观察题目可知,会有一个最大的x(两个菜的最大制作数),大于这个x就不能做任何一盘菜,小于这个x那么一定可以做出来,这样分析就是显而易见的递归。实现递归的check函数,那么我们就可以把两个菜的总制作数传进去。那么什么时候true什么时候false呢,就是判断每种材料的制作量有没有超过原材料......
  • AtCoder Beginner Contest 338
    A-Capitalized?#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongusingvi=vector<int>;usingi32=int32_t;usingpii=pair<int,int>;constintinf=1e9,INF=1e18;i32main(){strings;cin>>......
  • AtCoder Beginner Contest 338
    基本情况A忘记大小写敏感卡了20分钟,BC秒了,E用树状数组草过去了,D错了25个点,似乎是交界没有判断好。B-FrequencyB-Frequency(atcoder.jp)这题还可以更优雅点。intmain(){strings;cin>>s;map<char,int>cnt;for(inti=0;i<s.size();i++......
  • AtCoder Beginner Contest 338
    基本情况:A和B直接秒了,C题没写出来大致是思路的问题,下面就讲一下C自己的思路和题解C-LeftoverRecipes题目概述,先输入一个数字代表有多少中配料,然后依次输入A菜每种配料所需的量,然后输入B菜每种配料所需的量,最后输出最多可以做多少盘菜样例:280030010010020010输出为......