题目收集
题目列表题单
返回列表

Stack

CodeforcesP0014· 200ms· 256 MB· ZJU Summer 2026 Contest 1原题链接
dp

创建于 2026/07/16 · 更新于 2026/07/16

题目描述

Given integers nnn, kkk, and a sequence p1…kp_{1 \dots k}p1…k​.

There is a stack, initially empty. Perform the following insertion operation nnn times:

  1. Randomly choose an integer vvv from [1,k][1,k][1,k], where the probability of v=xv=xv=x is px∑i=1kpi\frac{p_x}{\sum_{i=1}^k p_i}∑i=1k​pi​px​​.
  2. If the top of the stack is exactly vvv, pop it, then set v←v+1v \gets v+1v←v+1. Repeat this step until the top of the stack differs from vvv.
  3. Push vvv onto the stack.

Find the expected number of elements in the stack after nnn insertions, modulo M=998244353M=998244353M=998244353.

In other words, the expected value can be written as PQ\frac{P}{Q}QP​ (gcd⁡(P,Q)=1\gcd(P,Q)=1gcd(P,Q)=1). You need to find the unique integer R∈[0,M−1]R \in [0,M-1]R∈[0,M−1] such that R⋅Q≡P(modM)R \cdot Q \equiv P \pmod MR⋅Q≡P(modM).

输入格式

The first line of input contains two integers nnn and kkk. The second line contains kkk integers, namely p1,p2,…,pkp_{1},p_2,\dots,p_kp1​,p2​,…,pk​.

输出格式

The output should contain a single line with one integer: the expected value you computed.

样例输入 #1

3 2
1 2

样例输出 #1

961272342

样例输入 #2

3 3
1 2 1

样例输出 #2

779878403

样例输入 #3

6 10
3293122 304483210 0 920851348 548604332 397404204 78439209 998244352 439820043 291380245

样例输出 #3

168058624