実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 : 100 点
問題文
鉄道の AtCoder 線には N 個の駅があり、それぞれ 1, 2, \ldots, N の番号が付けられています。
AtCoder 線では、駅 1 を始発駅として駅 2, 3, \ldots, N の順に各駅に停車する上り列車および、駅 N を始発駅として駅 N - 1, N - 2, \ldots, 1 の順に各駅に停車する下り列車が運行されています。
高橋君は AtCoder 線の上り列車あるいは下り列車の一方のみを使うことで駅 X から駅 Y まで移動しようとしています。
この移動の間に高橋君が乗っている電車が駅 Z に停車することがあるか判定してください。
制約
- 3 \leq N \leq 100
- 1 \leq X, Y, Z \leq N
- X, Y, Z は相異なる
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y Z
出力
駅 X から駅 Y への移動の間に高橋君が乗っている電車が駅 Z に停車することがあるならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
7 6 1 3
出力例 1
Yes
駅 6 から駅 1 に移動するためには下り列車に乗車します。
駅 6 を出発し、駅 5, 4, 3, 2, 1 の順に停車するため移動の間に電車が駅 3 に停車することがあり、Yes
を出力します。
入力例 2
10 3 2 9
出力例 2
No
入力例 3
100 23 67 45
出力例 3
Yes
Score: 100 points
Problem Statement
The AtCoder railway line has N stations, numbered 1, 2, \ldots, N.
On this line, there are inbound trains that start at station 1 and stop at the stations 2, 3, \ldots, N in order, and outbound trains that start at station N and stop at the stations N - 1, N - 2, \ldots, 1 in order.
Takahashi is about to travel from station X to station Y using only one of the inbound and outbound trains.
Determine whether the train stops at station Z during this travel.
Constraints
- 3 \leq N \leq 100
- 1 \leq X, Y, Z \leq N
- X, Y, and Z are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
If the train stops at station Z during the travel from station X to station Y, print Yes
; otherwise, print No
.
Sample Input 1
7 6 1 3
Sample Output 1
Yes
To travel from station 6 to station 1, Takahashi will take an outbound train.
After departing from station 6, the train stops at stations 5, 4, 3, 2, 1 in order, which include station 3, so you should print Yes
.
Sample Input 2
10 3 2 9
Sample Output 2
No
Sample Input 3
100 23 67 45
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 : 200 点
問題文
高橋君は英小文字からなる文字列 S をキーボードで入力しようとしました。
高橋君は画面を見ずにキーボードだけを見てタイピングをしていました。
誤って別の英小文字を入力してしまったときにはその直後にバックスペースキーを押しましたが、バックスペースキーが壊れていたため誤って入力された文字は消去されず、実際に入力された文字列は文字列 T となりました。
また、英小文字以外のキーを誤って押してしまうことはありませんでした。
T のうち高橋君が誤って入力した文字でないものを正しく入力された文字であると定めます。
正しく入力された文字が T の何文字目であるか答えてください。
制約
- S, T は長さ 1 以上 2 \times 10^5 以下の英小文字からなる文字列
- T は問題文中の手続きにより得られる文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S の長さを |S| として、正しく入力された文字が A_1, A_2, \ldots, A_{|S|} 文字目であるとき A_1, A_2, \ldots, A_{|S|} の値をこの順に空白区切りで出力せよ。
ただし、出力は昇順になるようにせよ。すなわち、各 1 \leq i \leq |S| - 1 に対して A_i < A_{i + 1} を満たすようにせよ。
入力例 1
abc axbxyc
出力例 1
1 3 6
高橋君のタイピングの一連の流れは以下のようになります。
a
を入力する。b
を入力しようとするが、誤ってx
を入力してしまう。- バックスペースキーを押すが、文字の削除は行われない。
b
を入力する。c
を入力しようとするが、誤ってx
を入力してしまう。- バックスペースキーを押すが、文字の削除は行われない。
c
を入力しようとするが、誤ってy
を入力してしまう。- バックスペースキーを押すが、文字の削除は行われない。
c
を入力する。
正しく入力された文字は 1, 3, 6 文字目です。
入力例 2
aaaa bbbbaaaa
出力例 2
5 6 7 8
入力例 3
atcoder atcoder
出力例 3
1 2 3 4 5 6 7
高橋君が誤った文字を入力することはありませんでした。
Score: 200 points
Problem Statement
Takahashi tried to type a string S consisting of lowercase English letters using a keyboard.
He was typing while looking only at the keyboard, not the screen.
Whenever he mistakenly typed a different lowercase English letter, he immediately pressed the backspace key. However, the backspace key was broken, so the mistakenly typed letter was not deleted, and the actual string typed was T.
He did not mistakenly press any keys other than those for lowercase English letters.
The characters in T that were not mistakenly typed are called correctly typed characters.
Determine the positions in T of the correctly typed characters.
Constraints
- S and T are strings of lowercase English letters with lengths between 1 and 2 \times 10^5, inclusive.
- T is a string obtained by the procedure described in the problem statement.
Input
The input is given from Standard Input in the following format:
S T
Output
Let |S| be the length of S. If the correctly typed characters are the A_1-th, A_2-th, \ldots, A_{|S|}-th characters of T, print the values of A_1, A_2, \ldots, A_{|S|} in this order, separated by spaces.
Ensure that the output is in ascending order. That is, A_i < A_{i + 1} should hold for each 1 \leq i \leq |S| - 1.
Sample Input 1
abc axbxyc
Sample Output 1
1 3 6
The sequence of Takahashi's typing is as follows:
- Type
a
. - Try to type
b
but mistakenly typex
. - Press the backspace key, but the character is not deleted.
- Type
b
. - Try to type
c
but mistakenly typex
. - Press the backspace key, but the character is not deleted.
- Try to type
c
but mistakenly typey
. - Press the backspace key, but the character is not deleted.
- Type
c
.
The correctly typed characters are the first, third, and sixth characters.
Sample Input 2
aaaa bbbbaaaa
Sample Output 2
5 6 7 8
Sample Input 3
atcoder atcoder
Sample Output 3
1 2 3 4 5 6 7
Takahashi did not mistakenly type any characters.
実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 : 300 点
問題文
N 人の巨人がいます。巨人にはそれぞれ 1, 2, \ldots, N の名前がついており、巨人 i が地面に立ったとき、肩の高さは A_i、頭の高さは B_i となります。
あなたは (1, 2, \ldots, N) を並べ替えて得られる数列 (P_1, P_2, \ldots, P_N) を選び、以下の規則に従って N 人の巨人を積み上げることができます。
-
まず地面に巨人 P_1 を立たせる。巨人 P_1 の肩は地面を基準として A_{P_1}、頭は地面を基準として B_{P_1} の高さとなる。
-
i = 1, 2, \ldots, N - 1 の順に巨人 P_i の肩の上に巨人 P_{i + 1} を立たせる。巨人 P_i の肩が地面を基準として高さ t のとき、巨人 P_{i + 1} の肩は地面を基準として t + A_{P_{i + 1}}、頭は地面を基準として t + B_{P_{i + 1}} の高さとなる。
一番上に立っている巨人、すなわち巨人 P_N の地面を基準とした頭の高さとして実現できる最大値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを出力せよ。
入力例 1
3 4 10 5 8 2 9
出力例 1
18
(P_1, P_2, P_3) = (2, 1, 3) とすると、地面を基準として巨人 2 は肩の高さが 5、頭の高さが 8、巨人 1 は肩の高さが 9、頭の高さが 15、巨人 3 は肩の高さが 11、頭の高さが 18 となります。
一番上に立っている巨人の頭の高さが地面を基準として 18 より大きくなることはないため 18 を出力します。
入力例 2
5 1 1 1 1 1 1 1 1 1 1
出力例 2
5
入力例 3
10 690830957 868532399 741145463 930111470 612846445 948344128 540375785 925723427 723092548 925021315 928915367 973970164 563314352 832796216 562681294 868338948 923012648 954764623 691107436 891127278
出力例 3
7362669937
Score: 300 points
Problem Statement
There are N giants, named 1 to N. When giant i stands on the ground, their shoulder height is A_i, and their head height is B_i.
You can choose a permutation (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) and stack the N giants according to the following rules:
-
First, place giant P_1 on the ground. The giant P_1's shoulder will be at a height of A_{P_1} from the ground, and their head will be at a height of B_{P_1} from the ground.
-
For i = 1, 2, \ldots, N - 1 in order, place giant P_{i + 1} on the shoulders of giant P_i. If giant P_i's shoulders are at a height of t from the ground, then giant P_{i + 1}'s shoulders will be at a height of t + A_{P_{i + 1}} from the ground, and their head will be at a height of t + B_{P_{i + 1}} from the ground.
Find the maximum possible height of the head of the topmost giant P_N from the ground.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq B_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the answer.
Sample Input 1
3 4 10 5 8 2 9
Sample Output 1
18
If (P_1, P_2, P_3) = (2, 1, 3), then measuring from the ground, giant 2 has a shoulder height of 5 and a head height of 8, giant 1 has a shoulder height of 9 and a head height of 15, and giant 3 has a shoulder height of 11 and a head height of 18.
The head height of the topmost giant from the ground cannot be greater than 18, so print 18.
Sample Input 2
5 1 1 1 1 1 1 1 1 1 1
Sample Output 2
5
Sample Input 3
10 690830957 868532399 741145463 930111470 612846445 948344128 540375785 925723427 723092548 925021315 928915367 973970164 563314352 832796216 562681294 868338948 923012648 954764623 691107436 891127278
Sample Output 3
7362669937
実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 : 425 点
問題文
(1,2,\dots,N) を並び替えて得られる数列 P=(P_1,P_2,\dots,P_N) が与えられます。
長さ K の正整数列 (i_1,i_2,\dots,i_K) であって、以下の条件を共に満たすものを良い添字列と呼びます。
- 1\leq i_1 < i_2 < \dots < i_K \leq N
- (P_{i_1},P_{i_2},\dots,P_{i_K}) はある連続する K 個の整数を並び替えることで得られる。
厳密には、ある整数 a が存在して、\lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace。
全ての良い添字列における i_K-i_1 の最小値を求めてください。 なお、本問題の制約下では良い添字列が必ず 1 つ以上存在することが示せます。
制約
- 1\leq K \leq N \leq 2\times 10^5
- 1\leq P_i\leq N
- i\neq j ならば P_i\neq P_j
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K P_1 P_2 \dots P_N
出力
全ての良い添字列における i_K-i_1 の最小値を出力せよ。
入力例 1
4 2 2 3 1 4
出力例 1
1
良い添字列は (1,2),(1,3),(2,4) の 3 つです。 例えば (i_1,i_2)=(1,3) は、 1\leq i_1 < i_2 \leq N かつ (P_{i_1},P_{i_2})=(2,1) が連続する 2 つの整数 1,2 の並び替えなので良い添字列です。
これらの良い添字列のうち i_K-i_1 の値が最小となるのは (1,2) で、そのときの値は 2-1=1 です。
入力例 2
4 1 2 3 1 4
出力例 2
0
どの良い添字列においても i_K-i_1=i_1-i_1=0 です。
入力例 3
10 5 10 1 6 8 7 2 5 9 3 4
出力例 3
5
Score: 425 points
Problem Statement
You are given a permutation P = (P_1, P_2, \dots, P_N) of (1, 2, \dots, N).
A length-K sequence of indices (i_1, i_2, \dots, i_K) is called a good index sequence if it satisfies both of the following conditions:
- 1 \leq i_1 < i_2 < \dots < i_K \leq N.
- The subsequence (P_{i_1}, P_{i_2}, \dots, P_{i_K}) can be obtained by rearranging some consecutive K integers.
Formally, there exists an integer a such that \lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace.
Find the minimum value of i_K - i_1 among all good index sequences. It can be shown that at least one good index sequence exists under the constraints of this problem.
Constraints
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq N
- P_i \neq P_j if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K P_1 P_2 \dots P_N
Output
Print the minimum value of i_K - i_1 among all good index sequences.
Sample Input 1
4 2 2 3 1 4
Sample Output 1
1
The good index sequences are (1,2),(1,3),(2,4). For example, (i_1, i_2) = (1,3) is a good index sequence because 1 \leq i_1 < i_2 \leq N and (P_{i_1}, P_{i_2}) = (2,1) is a rearrangement of two consecutive integers 1, 2.
Among these good index sequences, the smallest value of i_K - i_1 is for (1,2), which is 2-1=1.
Sample Input 2
4 1 2 3 1 4
Sample Output 2
0
i_K - i_1 = i_1 - i_1 = 0 in all good index sequences.
Sample Input 3
10 5 10 1 6 8 7 2 5 9 3 4
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 : 450 点
問題文
N 頂点からなる重み付き無向グラフ G があります。 G の各頂点には 1 から N までの番号が付けられています。 最初、G には辺が 1 本も存在しません。
今から、M 回の操作を行うことによって G に辺を追加していきます。 i\ (1\leq i\leq M) 回目の操作は以下の通りです。
- K_i 頂点からなる頂点の部分集合 S_i=\lbrace A_{i,1},A_{i,2},\dots,A_{i,K_i}\rbrace が与えられる。 u,v\in S_i かつ u<v を満たす全ての u,v について、頂点 u と頂点 v の間に重み C_i の辺を追加する。
M 回の操作を全て行ったとき G が連結になるか判定し、連結になるならば G の最小全域木に含まれる辺の重みの総和を求めてください。
制約
- 2\leq N \leq 2\times 10^5
- 1\leq M \leq 2\times 10^5
- 2\leq K_i \leq N
- \displaystyle \sum_{i=1}^{M} K_i \leq 4\times 10^5
- 1\leq A_{i,1} < A_{i,2} < \dots < A_{i,K_i}\leq N
- 1\leq C_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K_1 C_1 A_{1,1} A_{1,2} \dots A_{1,K_1} K_2 C_2 A_{2,1} A_{2,2} \dots A_{2,K_2} \vdots K_M C_M A_{M,1} A_{M,2} \dots A_{M,K_M}
出力
M 回の操作を全て行ったとき G が連結にならないならば -1
を、連結になるならば G の最小全域木に含まれる辺の重みの総和を出力せよ。
入力例 1
4 3 3 3 1 2 3 2 2 1 2 3 4 1 3 4
出力例 1
9
左の図は M 回の操作を全て行ったあとの G を、右の図はその最小全域木の 1 つを表しています(辺の横に書かれた数はその辺の重みを示します)。
最小全域木に含まれる辺の重みの総和は 3+2+4=9 です。
入力例 2
3 2 2 1 1 2 2 1 1 2
出力例 2
-1
M 回の操作を全て行っても G は連結になりません。
入力例 3
10 5 6 158260522 1 3 6 8 9 10 10 877914575 1 2 3 4 5 6 7 8 9 10 4 602436426 2 6 7 9 6 24979445 2 3 4 5 8 10 4 861648772 2 4 8 9
出力例 3
1202115217
Score: 450 points
Problem Statement
You are given a weighted undirected graph G with N vertices, numbered 1 to N. Initially, G has no edges.
You will perform M operations to add edges to G. The i-th operation (1 \leq i \leq M) is as follows:
- You are given a subset of vertices S_i=\lbrace A_{i,1},A_{i,2},\dots,A_{i,K_i}\rbrace consisting of K_i vertices. For every pair u, v such that u, v \in S_i and u < v, add an edge between vertices u and v with weight C_i.
After performing all M operations, determine whether G is connected. If it is, find the total weight of the edges in a minimum spanning tree of G.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 2 \leq K_i \leq N
- \sum_{i=1}^{M} K_i \leq 4 \times 10^5
- 1 \leq A_{i,1} < A_{i,2} < \dots < A_{i,K_i} \leq N
- 1 \leq C_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K_1 C_1 A_{1,1} A_{1,2} \dots A_{1,K_1} K_2 C_2 A_{2,1} A_{2,2} \dots A_{2,K_2} \vdots K_M C_M A_{M,1} A_{M,2} \dots A_{M,K_M}
Output
If G is not connected after all M operations, print -1
. If G is connected, print the total weight of the edges in a minimum spanning tree of G.
Sample Input 1
4 3 3 3 1 2 3 2 2 1 2 3 4 1 3 4
Sample Output 1
9
The left diagram shows G after all M operations, and the right diagram shows a minimum spanning tree of G (the numbers next to the edges indicate their weights).
The total weight of the edges in the minimum spanning tree is 3 + 2 + 4 = 9.
Sample Input 2
3 2 2 1 1 2 2 1 1 2
Sample Output 2
-1
G is not connected even after all M operations.
Sample Input 3
10 5 6 158260522 1 3 6 8 9 10 10 877914575 1 2 3 4 5 6 7 8 9 10 4 602436426 2 6 7 9 6 24979445 2 3 4 5 8 10 4 861648772 2 4 8 9
Sample Output 3
1202115217
実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 : 525 点
問題文
N 人の人がおり、人にはそれぞれ 1, 2, \ldots, N の番号が付けられています。
N 人が競争を行い、順位が付きました。この順位に対して以下の情報が与えられています。
- それぞれの人に対して付けられた順位は相異なる
- 各 1 \leq i \leq M について人 A_i の順位を x、人 B_i の順位を y とすると、x - y = C_i である
ただし、この問題では与えられた情報に矛盾しないような順位付けが 1 つ以上存在するような入力のみが与えられます。
N 個のクエリの答えを求めてください。i 番目のクエリの答えは以下により定まる整数です。
- 人 i の順位が一意に定まるならば、その値を答えとする。そうでない場合、答えは -1 である。
制約
- 2 \leq N \leq 16
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq A_i, B_i \leq N
- 1 \leq C_i \leq N - 1
- (A_i, B_i) \neq (A_j, B_j) (i \neq j)
- 与えられた情報に矛盾しない順位付けが 1 つ以上存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
1, 2, \ldots ,N 番目のクエリに対する答えをこの順に空白区切りで出力せよ。
入力例 1
5 2 2 3 3 5 4 3
出力例 1
3 -1 -1 -1 -1
人 i の順位を X_i とおくと、(X_1, X_2, X_3, X_4, X_5) は (3, 4, 1, 2, 5), (3, 5, 2, 1, 4) のいずれかです。
したがって、1 番目のクエリに対する答えは 3、2, 3, 4, 5 番目のクエリに対する答えは -1 となります。
入力例 2
3 0
出力例 2
-1 -1 -1
入力例 3
8 5 6 7 3 8 1 7 4 5 1 7 2 1 6 2 4
出力例 3
1 -1 -1 -1 -1 -1 -1 8
Score: 525 points
Problem Statement
There are N people, numbered 1 to N.
A competition was held among these N people, and they were ranked accordingly. The following information is given about their ranks:
- Each person has a unique rank.
- For each 1 \leq i \leq M, if person A_i is ranked x-th and person B_i is ranked y-th, then x - y = C_i.
The given input guarantees that there is at least one possible ranking that does not contradict the given information.
Answer N queries. The answer to the i-th query is an integer determined as follows:
- If the rank of person i can be uniquely determined, return that rank. Otherwise, return -1.
Constraints
- 2 \leq N \leq 16
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq A_i, B_i \leq N
- 1 \leq C_i \leq N - 1
- (A_i, B_i) \neq (A_j, B_j) (i \neq j)
- There is at least one possible ranking that does not contradict the given information.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
Print the answers to the 1-st, 2-nd, \ldots, N-th queries in this order, separated by spaces.
Sample Input 1
5 2 2 3 3 5 4 3
Sample Output 1
3 -1 -1 -1 -1
Let X_i be the rank of person i. Then, (X_1, X_2, X_3, X_4, X_5) could be (3, 4, 1, 2, 5) or (3, 5, 2, 1, 4).
Therefore, the answer to the 1-st query is 3, and the answers to the 2-nd, 3-rd, 4-th, and 5-th queries are -1.
Sample Input 2
3 0
Sample Output 2
-1 -1 -1
Sample Input 3
8 5 6 7 3 8 1 7 4 5 1 7 2 1 6 2 4
Sample Output 3
1 -1 -1 -1 -1 -1 -1 8
実行時間制限: 3 sec / メモリ制限: 1024 MB
配点 : 600 点
問題文
高橋君のタンスの中には様々な色の靴下が入っています。 靴下の色は 1 以上 N 以下の整数として表され、色 i の靴下は A_i\ (\geq 2) 枚入っています。
高橋君は、以下の操作を行うことで今日履く靴下を選ぼうとしています。
- 今までに取り出した靴下の中で同じ色の靴下の 2 枚組が作れるようになるまで、タンスの中からランダムに等確率で 1 枚の靴下を取り出すことを繰り返す。 なお、一度取り出した靴下はタンスの中には戻さない。
高橋君がタンスから靴下を取り出す回数の期待値を \text{mod } 998244353 で求めてください。
期待値を \text{mod } 998244353 で求めるとは
求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、期待値を既約分数 \frac{y}{x} で表したときに x が 998244353 で割り切れないことが保証されます。 このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まるので、この z を求めてください。制約
- 1\leq N \leq 3\times 10^5
- 2\leq A_i \leq 3000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
2 2 2
出力例 1
665496238
例えば、以下のように操作を行うことが考えられます。
- タンスの中から色 1 の靴下を 1 枚取り出す。タンスの中には色 1 の靴下が 1 枚と色 2 の靴下が 2 枚残っている。
- タンスの中から色 2 の靴下を 1 枚取り出す。タンスの中には色 1,2 の靴下が 1 枚ずつ残っている。
- タンスの中から色 1 の靴下を 1 枚取り出す。今までに取り出した靴下は色 1 の靴下が 2 枚と色 2 の靴下が 1 枚であり、この中で色 1 の靴下の 2 枚組が作れるので操作を終了する。
この例の場合、高橋君がタンスから靴下を取り出す回数は 3 回です。
高橋君がタンスから靴下を取り出す回数は \frac{2}{3} の確率で 3 回、\frac{1}{3} の確率で 2 回なので、求める期待値は 3\times \frac{2}{3} + 2\times \frac{1}{3} = \frac{8}{3} \equiv 665496238 \pmod {998244353} です。
入力例 2
1 352
出力例 2
2
入力例 3
6 1796 905 2768 253 2713 1448
出力例 3
887165507
Score: 600 points
Problem Statement
Takahashi has various colors of socks in his chest of drawers. The colors of the socks are represented by integers from 1 to N, and there are A_i\ (\geq 2) socks of color i.
He is about to choose which socks to wear today by performing the following operation:
- Continue to randomly draw one sock at a time from the chest, with equal probability, until he can make a pair of socks of the same color from those he has already drawn. Once a sock is drawn, it will not be returned to the chest.
Find the expected value, modulo 998244353, of the number of times he has to draw a sock from the chest.
What does it mean to find the expected value modulo 998244353?
It can be proved that the sought expected value is always rational. Furthermore, the constraints of this problem guarantee that if the expected value is expressed as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353. Here, there exists a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Find this z.Constraints
- 1\leq N \leq 3\times 10^5
- 2\leq A_i \leq 3000
- All input values are integers.
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
2 2 2
Sample Output 1
665496238
For example, the operation could be performed as follows:
- Draw a sock of color 1 from the chest. There remains one sock of color 1 and two socks of color 2 in the chest.
- Draw a sock of color 2 from the chest. There remains one sock each of colors 1 and 2 in the chest.
- Draw a sock of color 1 from the chest. The socks drawn so far are two of color 1 and one of color 2, allowing a pair of color 1 socks to be made, thus ending the operation.
In this example, Takahashi draws a sock from the chest three times.
The expected number of times Takahashi draws a sock from the chest is 3 with probability \frac{2}{3} and 2 with probability \frac{1}{3}, so the expected value is 3\times \frac{2}{3} + 2\times \frac{1}{3} = \frac{8}{3} \equiv 665496238 \pmod {998244353}.
Sample Input 2
1 352
Sample Output 2
2
Sample Input 3
6 1796 905 2768 253 2713 1448
Sample Output 3
887165507