実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
AtCoder 国の暦では、1 年は 1 月から M 月までの M ヶ月からなり、どの月も 1 日から D 日までの D 日からなります。
AtCoder 国の暦で y 年 m 月 d 日の翌日は何年何月何日であるか求めてください。
制約
- 1000 \leq y \leq 9000
- 1 \leq m \leq M \leq 99
- 1 \leq d \leq D \leq 99
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
M D y m d
出力
AtCoder 国の暦で y 年 m 月 d 日の翌日が y' 年 m' 月 d' 日であるとき、y',m',d' をこの順に空白区切りで出力せよ。
入力例 1
12 30 2023 12 30
出力例 1
2024 1 1
AtCoder 国の暦では 1 年は 12 ヶ月であり、どの月も 30 日からなります。 よって、2023 年 12 月 30 日の翌日は 2024 年 1 月 1 日になります。
入力例 2
36 72 6789 23 45
出力例 2
6789 23 46
AtCoder 国の暦では 1 年は 36 ヶ月であり、どの月も 72 日からなります。 よって、6789 年 23 月 45 日の翌日は 6789 年 23 月 46 日になります。
入力例 3
12 30 2012 6 20
出力例 3
2012 6 21
Score : 100 points
Problem Statement
In the calendar of AtCoder Kingdom, a year consists of M months from month 1 to month M, and each month consists of D days from day 1 to day D.
What day follows year y, month m, day d in this calendar?
Constraints
- 1000 \leq y \leq 9000
- 1 \leq m \leq M \leq 99
- 1 \leq d \leq D \leq 99
- All input values are integers.
Input
The input is given from Standard Input in the following format:
M D y m d
Output
If the day following year y, month m, day d in the calendar of AtCoder Kingdom is year y', month m', day d', print y', m', and d' in this order, separated by spaces.
Sample Input 1
12 30 2023 12 30
Sample Output 1
2024 1 1
In the calendar of the kingdom, a year consists of 12 months, and each month consists of 30 days. Thus, the day following year 2023, month 12, day 30 is year 2024, month 1, day 1.
Sample Input 2
36 72 6789 23 45
Sample Output 2
6789 23 46
In the calendar of the kingdom, one year consists of 36 months, and each month consists of 72 days. Thus, the day following year 6789, month 23, day 45 is year 6789, month 23, day 46.
Sample Input 3
12 30 2012 6 20
Sample Output 3
2012 6 21
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
スーパーマーケットで卵のパックが売られています。
卵 6 個入りのパックは S 円、卵 8 個入りのパックは M 円、卵 12 個入りのパックは L 円です。
どのパックも何パックでも購入できるとき、N 個以上の卵を買うために必要な最小の金額を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq S,M,L \leq 10^4
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N S M L
出力
答えを出力せよ。
入力例 1
16 120 150 200
出力例 1
300
8 個入りのパックを 2 個買うのが最適です。
入力例 2
10 100 50 10
出力例 2
10
12 個入りのパックを 1 個買うのが最適です。
入力例 3
99 600 800 1200
出力例 3
10000
8 個入りのパックと 12 個入りのパックを 5 個ずつ買うのが最適です。
Score : 200 points
Problem Statement
A supermarket sells egg packs.
A pack of 6 eggs costs S yen, a pack of 8 eggs costs M yen, and a pack of 12 eggs costs L yen.
When you can buy any number of each pack, find the minimum amount of money required to purchase at least N eggs.
Constraints
- 1 \leq N \leq 100
- 1 \leq S,M,L \leq 10^4
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N S M L
Output
Print the answer.
Sample Input 1
16 120 150 200
Sample Output 1
300
It is optimal to buy two 8-egg packs.
Sample Input 2
10 100 50 10
Sample Output 2
10
It is optimal to buy one 12-egg pack.
Sample Input 3
99 600 800 1200
Sample Output 3
10000
It is optimal to buy five 8-egg packs and five 12-egg packs.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。
i=1,\ldots,N のそれぞれについて次の問題を解いてください。
問題:A の要素のうち A_i より大きな要素全ての和を求めよ。
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 10^6
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
各 1\leq k\leq N について、i=k に対する問題の答えを B_k とする。B_1,\ldots,B_N をこの順に空白区切りで出力せよ。
入力例 1
5 1 4 1 4 2
出力例 1
10 0 10 0 8
- i=1 のとき A_1=1 より大きな要素の和は 4+4+2=10
- i=2 のとき A_2=4 より大きな要素の和は 0
- i=3 のとき A_3=1 より大きな要素の和は 4+4+2=10
- i=4 のとき A_4=4 より大きな要素の和は 0
- i=5 のとき A_5=2 より大きな要素の和は 4+4=8
入力例 2
10 31 42 59 26 53 58 97 93 23 54
出力例 2
456 414 190 487 361 249 0 97 513 307
入力例 3
50 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Score : 300 points
Problem Statement
You are given a sequence A=(A_1,\ldots,A_N) of length N.
For each i=1,\ldots,N, solve the following problem.
Problem: Find the sum of all elements in A that are greater than A_i.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq 10^6
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
For each 1\leq k\leq N, let B_k be the answer to the problem when i=k. Print B_1,\ldots,B_N in this order, separated by spaces.
Sample Input 1
5 1 4 1 4 2
Sample Output 1
10 0 10 0 8
- For i=1, the sum of elements greater than A_1=1 is 4+4+2=10.
- For i=2, the sum of elements greater than A_2=4 is 0.
- For i=3, the sum of elements greater than A_3=1 is 4+4+2=10.
- For i=4, the sum of elements greater than A_4=4 is 0.
- For i=5, the sum of elements greater than A_5=2 is 4+4=8.
Sample Input 2
10 31 42 59 26 53 58 97 93 23 54
Sample Output 2
456 414 190 487 361 249 0 97 513 307
Sample Input 3
50 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
縦横 10^9 マスのグリッドがあります。上から i + 1 行目、左から j + 1 列目 (0 \leq i, j \lt 10^9) にあるマスを (i, j) と呼びます。(通常と異なる index の割り当て方に注意してください。)
各マスは黒マスか白マスのいずれかです。マス (i, j) の色は文字 P[i \bmod N][j \bmod N] によって表されて、B
ならばマス (i, j) は黒マス、W
ならば白マスです。ここで a \bmod b は a を b で割った余りを意味します。
Q 個のクエリが与えられるので順に処理してください。
各クエリでは 4 つの整数 A, B, C, D が与えられるので、(A, B) を左上隅、(C, D) を右下隅とする長方形領域に含まれる黒マスの個数を求めてください。
制約
- 1 \leq N \leq 1000
- P[i][j] は
W
またはB
- 1 \leq Q \leq 2 \times 10^5
- 0 \leq A \leq C \lt 10^9
- 0 \leq B \leq D \lt 10^9
- N, Q, A, B, C, D は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \text{query}_i は i 番目に処理するクエリである。
N Q P[0][0]P[0][1]\dots P[0][N-1] P[1][0]P[1][1]\dots P[1][N-1] \vdots P[N-1][0]P[N-1][1]\dots P[N-1][N-1] \text{query}_1 \text{query}_2 \vdots \text{query}_Q
各クエリは以下の形式で与えられる。
A B C D
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
3 2 WWB BBW WBW 1 2 3 4 0 3 4 5
出力例 1
4 7
グリッドの左上部分を図示すると次の図のようになります。
1 番目のクエリについて、(1, 2) を左上隅、(3, 4) を右下隅とする長方形領域は図の赤い枠線に囲まれた部分で、領域に含まれる黒マスの個数は 4 個です。
2 番目のクエリについて、(0, 3) を左上隅、(4, 5) を右下隅とする長方形領域は図の青い枠線に囲まれた部分で、領域に含まれる黒マスの個数は 7 個です。
入力例 2
10 5 BBBWWWBBBW WWWWWBBBWB BBBWBBWBBB BBBWWBWWWW WWWWBWBWBW WBBWBWBBBB WWBBBWWBWB WBWBWWBBBB WBWBWBBWWW WWWBWWBWWB 5 21 21 93 35 35 70 43 55 72 61 84 36 33 46 95 0 0 999999999 999999999
出力例 2
621 167 44 344 500000000000000000
Score : 450 points
Problem Statement
There is a grid with 10^9 by 10^9 squares. Let (i, j) denote the square at the (i + 1)-th row from the top and the (j + 1)-th column from the left (0 \leq i, j \lt 10^9). (Note the unusual index assignment.)
Each square is black or white. The color of the square (i, j) is represented by a character P[i \bmod N][j \bmod N], where B
means black, and W
means white. Here, a \bmod b denotes the remainder when a is divided by b.
Answer Q queries.
Each query gives you four integers A, B, C, D and asks you to find the number of black squares contained in the rectangular area with (A, B) as the top-left corner and (C, D) as the bottom-right corner.
Constraints
- 1 \leq N \leq 1000
- P[i][j] is
W
orB
. - 1 \leq Q \leq 2 \times 10^5
- 0 \leq A \leq C \lt 10^9
- 0 \leq B \leq D \lt 10^9
- N, Q, A, B, C, D are all integers.
Input
The input is given from Standard Input in the following format. Here, \text{query}_i is the i-th query to be processed.
N Q P[0][0]P[0][1]\dots P[0][N-1] P[1][0]P[1][1]\dots P[1][N-1] \vdots P[N-1][0]P[N-1][1]\dots P[N-1][N-1] \text{query}_1 \text{query}_2 \vdots \text{query}_Q
Each query is given in the following format:
A B C D
Output
Follow the instructions in the problem statement and print the answers to the queries, separated by newlines.
Sample Input 1
3 2 WWB BBW WBW 1 2 3 4 0 3 4 5
Sample Output 1
4 7
The figure below illustrates the upper left part of the grid.
For the first query, the rectangular area with (1, 2) as the top-left corner and (3, 4) as the bottom-right corner, surrounded by the red frame in the figure, contains four black squares.
For the second query, the rectangular area with (0, 3) as the top-left corner and (4, 5) as the bottom-right corner, surrounded by the blue frame in the figure, contains seven black squares.
Sample Input 2
10 5 BBBWWWBBBW WWWWWBBBWB BBBWBBWBBB BBBWWBWWWW WWWWBWBWBW WBBWBWBBBB WWBBBWWBWB WBWBWWBBBB WBWBWBBWWW WWWBWWBWWB 5 21 21 93 35 35 70 43 55 72 61 84 36 33 46 95 0 0 999999999 999999999
Sample Output 2
621 167 44 344 500000000000000000
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
AtCoder 食堂では主菜と副菜からなる定食が販売されています。
主菜は N 種類あり、順に主菜 1, 主菜 2, \dots, 主菜 N と呼びます。主菜 i の価格は a_i 円です。
副菜は M 種類あり、順に副菜 1, 副菜 2, \dots, 副菜 M と呼びます。副菜 i の価格は b_i 円です。
定食は主菜と副菜を 1 種類ずつ選んで構成されます。定食の価格は選んだ主菜の価格と副菜の価格の和です。
ただし、L 個の相異なる組 (c_1, d_1), \dots, (c_L, d_L) について、主菜 c_i と副菜 d_i からなる定食は食べ合わせが悪いため提供されていません。
つまり、提供されている定食は NM - L 種類あることになります。(提供されている定食が少なくとも 1 種類存在することが制約によって保証されています。)
提供されている定食のうち、最も価格の高い定食の価格を求めてください。
制約
- 1 \leq N, M \leq 10^5
- 0 \leq L \leq \min(10^5, N M - 1)
- 1 \leq a_i, b_i \leq 10^9
- 1 \leq c_i \leq N
- 1 \leq d_j \leq M
- i \neq j ならば (c_i, d_i) \neq (c_j, d_j)
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M L a_1 a_2 \dots a_N b_1 b_2 \dots b_M c_1 d_1 c_2 d_2 \vdots c_L d_L
出力
提供されている定食のうち、最も価格の高い定食が何円であるかを出力せよ。
入力例 1
2 3 3 2 1 10 30 20 1 2 2 1 2 3
出力例 1
31
提供されている定食、及びその価格は次の 3 種類です。
- 主菜 1 と副菜 1 からなる定食。価格は 2 + 10 = 12 円である。
- 主菜 1 と副菜 3 からなる定食。価格は 2 + 20 = 22 円である。
- 主菜 2 と副菜 2 からなる定食。価格は 1 + 30 = 31 円である。
この中で最も高い定食は 3 番目の定食です。よって 31 を出力してください。
入力例 2
2 1 0 1000000000 1 1000000000
出力例 2
2000000000
入力例 3
10 10 10 47718 21994 74148 76721 98917 73766 29598 59035 69293 29127 7017 46004 16086 62644 74928 57404 32168 45794 19493 71590 1 3 2 6 4 5 5 4 5 5 5 6 5 7 5 8 5 10 7 3
出力例 3
149076
Score : 450 points
Problem Statement
AtCoder cafeteria sells meals consisting of a main dish and a side dish.
There are N types of main dishes, called main dish 1, main dish 2, \dots, main dish N. Main dish i costs a_i yen.
There are M types of side dishes, called side dish 1, side dish 2, \dots, side dish M. Side dish i costs b_i yen.
A set meal is composed by choosing one main dish and one side dish. The price of a set meal is the sum of the prices of the chosen main dish and side dish.
However, for L distinct pairs (c_1, d_1), \dots, (c_L, d_L), the set meal consisting of main dish c_i and side dish d_i is not offered because they do not go well together.
That is, NM - L set meals are offered. (The constraints guarantee that at least one set meal is offered.)
Find the price of the most expensive set meal offered.
Constraints
- 1 \leq N, M \leq 10^5
- 0 \leq L \leq \min(10^5, NM - 1)
- 1 \leq a_i, b_i \leq 10^9
- 1 \leq c_i \leq N
- 1 \leq d_j \leq M
- (c_i, d_i) \neq (c_j, d_j) if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M L a_1 a_2 \dots a_N b_1 b_2 \dots b_M c_1 d_1 c_2 d_2 \vdots c_L d_L
Output
Print the price, in yen, of the most expensive set meal offered.
Sample Input 1
2 3 3 2 1 10 30 20 1 2 2 1 2 3
Sample Output 1
31
They offer three set meals, listed below, along with their prices:
- A set meal consisting of main dish 1 and side dish 1, at a price of 2 + 10 = 12 yen.
- A set meal consisting of main dish 1 and side dish 3, at a price of 2 + 20 = 22 yen.
- A set meal consisting of main dish 2 and side dish 2, at a price of 1 + 30 = 31 yen.
Among them, the most expensive is the third one. Thus, print 31.
Sample Input 2
2 1 0 1000000000 1 1000000000
Sample Output 2
2000000000
Sample Input 3
10 10 10 47718 21994 74148 76721 98917 73766 29598 59035 69293 29127 7017 46004 16086 62644 74928 57404 32168 45794 19493 71590 1 3 2 6 4 5 5 4 5 5 5 6 5 7 5 8 5 10 7 3
Sample Output 3
149076
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
以下で説明されるクエリを与えられる順に Q 個処理してください。
クエリは次の 2 種類のいずれかです。
1 x c
: S の x 文字目を英小文字 c に変更する。2 L R
: S の L 文字目から R 文字目までからなる部分文字列が回文であるならばYes
を、そうでないならばNo
を出力する。
制約
- 1 \leq N \leq 10^6
- 1 \leq Q \leq 10^5
- S は英小文字からなる長さ N の文字列
- 1 \leq x \leq N
- c は英小文字
- 1 \leq L \leq R \leq N
- N, Q, x, L, R は整数
入力
入力は以下の形式で標準入力から与えられる。ここで \text{query}_i は i 番目に処理するクエリである。
N Q S \text{query}_1 \text{query}_2 \vdots \text{query}_Q
各クエリは以下のいずれかの形式で与えられる。
1 x c
2 L R
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
7 8 abcbacb 2 1 5 2 4 7 2 2 2 1 5 c 2 1 5 2 4 7 1 4 c 2 3 6
出力例 1
Yes No Yes No Yes Yes
はじめ、S = abcbacb
です。
1 番目のクエリについて、S の 1 文字目から 5 文字目までからなる文字列は abcba
で、これは回文です。よって Yes
を出力します。
2 番目のクエリについて、S の 4 文字目から 7 文字目までからなる文字列は bacb
で、これは回文ではありません。よって No
を出力します。
3 番目のクエリについて、S の 2 文字目から 2 文字目までからなる文字列は b
で、これは回文です。よって Yes
を出力します。
4 番目のクエリについて、S の 5 文字目を c
に変更します。S は abcbccb
になります。
5 番目のクエリについて、S の 1 文字目から 5 文字目までからなる文字列は abcbc
で、これは回文ではありません。よって No
を出力します。
6 番目のクエリについて、S の 4 文字目から 7 文字目までからなる文字列は bccb
で、これは回文です。よって Yes
を出力します。
7 番目のクエリについて、S の 4 文字目を c
に変更します。S は abccccb
になります。
8 番目のクエリについて、S の 3 文字目から 6 文字目までからなる文字列は cccc
で、これは回文です。よって Yes
を出力します。
Score : 525 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
Process Q queries described below in the order they are given.
There are two types of queries:
1 x c
: Change the x-th character of S to the lowercase English letter c.2 L R
: If the substring formed by the L-th through R-th characters of S is a palindrome, printYes
; otherwise, printNo
.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq Q \leq 10^5
- S is a string of length N consisting of lowercase English letters.
- 1 \leq x \leq N
- c is a lowercase English letter.
- 1 \leq L \leq R \leq N
- N, Q, x, L, R are integers.
Input
The input is given from Standard Input in the following format. Here, \text{query}_i is the i-th query to be processed.
N Q S \text{query}_1 \text{query}_2 \vdots \text{query}_Q
Each query is given in one of the following formats:
1 x c
2 L R
Output
Follow the instructions in the problem statement and print the answers to the queries, separated by newlines.
Sample Input 1
7 8 abcbacb 2 1 5 2 4 7 2 2 2 1 5 c 2 1 5 2 4 7 1 4 c 2 3 6
Sample Output 1
Yes No Yes No Yes Yes
Initially, S = abcbacb
.
For the first query, the string formed by the 1-st through 5-th characters of S is abcba
, which is a palindrome. Thus, print Yes
.
For the second query, the string formed by the 4-th through 7-th character of S is bacb
, which is not a palindrome. Thus, print No
.
For the third query, the string formed by the 2-nd through 2-nd character of S is b
, which is a palindrome. Thus, output Yes
.
For the fourth query, change the 5-th character of S to c
. S becomes abcbccb
.
For the fifth query, the string formed by the 1-st through 5-th character of S is abcbc
, which is not a palindrome. Thus, output No
.
For the sixth query, the string formed by the 4-th through 7-th character of S is bccb
, which is a palindrome. Thus, output Yes
.
For the seventh query, change the 4-th character of S to c
. S becomes abccccb
.
For the eighth query, the string formed by the 3-rd through 6-th character of cccc
, which is a palindrome. Thus, output Yes
.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 625 点
問題文
箱の中に N 枚のカードが入っています。各カードには 1 以上 M 以下の整数が 1 つ書かれており、各 i=1,\ldots,M について、i が書かれたカードは C_i 枚あります。
あなたは何も書かれていないノートを持った状態から、次の操作を繰り返します。
- 箱の中からランダムにカードを 1 枚取り出す。取り出したカードに書かれている整数をノートに書き、カードを箱の中に戻す。
ノートに 1 以上 M 以下の整数が全て 1 個以上書かれている状態になるまでの操作回数の期待値を \bmod 998244353 で求めてください。
期待値 {}\bmod{998244353} の定義
この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac yx で表したときに x が 998244353 で割り切れないことが保証されます。 このとき、y\equiv xz\pmod{998244353} を満たす 0\leq z\lt998244353 がただ一つ存在するので、z を出力してください。制約
- 1 \leq M \leq N \leq 2\times 10^5
- 1 \leq C_i
- \sum_{i=1}^{M}C_i=N
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M C_1 \ldots C_M
出力
答えを出力せよ。
入力例 1
2 2 1 1
出力例 1
3
一連の操作は、例えば次のように進行します。
- 箱から取り出したカードに 1 が書かれている。ノートには 1 が 1 個が書かれている状態になる。
- 箱から取り出したカードに 1 が書かれている。ノートには 1 が 2 個が書かれている状態になる。
- 箱から取り出したカードに 2 が書かれている。ノートには 1 が 2 個、2 が 1 個書かれている状態になる。
求める期待値は 2\times\frac{1}{2}+3\times\frac{1}{4}+4\times\frac{1}{8}+\ldots=3 です。
入力例 2
5 2 4 1
出力例 2
748683270
求める期待値は \frac{21}{4} であり、\bmod 998244353 で 748683270 となります。
入力例 3
50 50 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 3
244742906
求める期待値は \frac{13943237577224054960759}{61980890084919934128} です。
入力例 4
74070 15 1 2 3 11 22 33 111 222 333 1111 2222 3333 11111 22222 33333
出力例 4
918012973
Score : 625 points
Problem Statement
There are N cards in a box. Each card has an integer written on it, which is between 1 and M, inclusive. For each i=1,\ldots,M, there are C_i cards with the number i written on them.
Starting with an empty notebook, you repeat the following operation:
- Randomly draw one card from the box. Write the integer on the card in the notebook, then return the card to the box.
Find the expected value, modulo 998244353, of the number of times the operation is performed until the notebook has all integers from 1 to M written at least once.
How to find an expected value modulo 998244353
The expected value sought in this problem can be proven to always be a rational number. Furthermore, the constraints of this problem guarantee that when the expected value is expressed as an irreducible fraction \frac yx, the denominator x is not divisible by 998244353. Then, there is a unique 0\leq z\lt998244353 such that y\equiv xz\pmod{998244353}. Print this z.Constraints
- 1 \leq M \leq N \leq 2\times 10^5
- 1 \leq C_i
- \sum_{i=1}^{M}C_i=N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M C_1 \ldots C_M
Output
Print the answer.
Sample Input 1
2 2 1 1
Sample Output 1
3
The series of operations may proceed as follows:
- You draw a card with 1 written on it. The notebook now has one 1 written in it.
- You again draw a card with 1 written on it. The notebook now has two 1s written in it.
- You draw a card with 2 written on it. The notebook now has two 1s and one 2 written in it.
The sought expected value is 2\times\frac{1}{2}+3\times\frac{1}{4}+4\times\frac{1}{8}+\ldots=3.
Sample Input 2
5 2 4 1
Sample Output 2
748683270
The expected value is \frac{21}{4}, whose representation modulo 998244353 is 748683270.
Sample Input 3
50 50 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 3
244742906
The expected value is \frac{13943237577224054960759}{61980890084919934128}.
Sample Input 4
74070 15 1 2 3 11 22 33 111 222 333 1111 2222 3333 11111 22222 33333
Sample Output 4
918012973