実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
数字からなる文字列 S が与えられます。
S から 2 以外の文字を削除し、残った文字を順序を保って結合した文字列を求めてください。
制約
- S は数字からなる長さ 1 以上 100 以下の文字列
- S は
2を 1 つ以上含む
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
20250222
出力例 1
22222
20250222 から 0, 5, 0 を削除し、残った文字を順序を保って結合することで文字列 22222 が得られます。
入力例 2
2
出力例 2
2
入力例 3
22222000111222222
出力例 3
22222222222
Score : 100 points
Problem Statement
You are given a string S consisting of digits.
Remove all characters from S except for 2, and then concatenate the remaining characters in their original order to form a new string.
Constraints
- S is a string consisting of digits with length between 1 and 100, inclusive.
- S contains at least one
2.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
20250222
Sample Output 1
22222
By removing 0, 5, and 0 from 20250222 and then concatenating the remaining characters in their original order, the string 22222 is obtained.
Sample Input 2
2
Sample Output 2
2
Sample Input 3
22222000111222222
Sample Output 3
22222222222
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
長さ N の整数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。
A から偶数だけすべて取り出し、もとの順番を保って出力してください。
制約
- 1\leq N\leq 100
- 1\leq A _ i\leq 100\ (1\leq i\leq N)
- A には 1 つ以上偶数が含まれる
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
A から偶数を取り出した列を、空白区切りで 1 行に出力せよ。
入力例 1
5 1 2 3 5 6
出力例 1
2 6
A=(1,2,3,5,6) です。 このうち偶数なのは A _ 2=2,A _ 5=6 の 2 つなので、2 と 6 をこの順に空白区切りで出力してください。
入力例 2
5 2 2 2 3 3
出力例 2
2 2 2
A の中には同じ要素がある場合もあります。
入力例 3
10 22 3 17 8 30 15 12 14 11 17
出力例 3
22 8 30 12 14
Score : 100 points
Problem Statement
You are given a sequence of N integers: A=(A _ 1,A _ 2,\ldots,A _ N).
Print all even numbers in A without changing the order.
Constraints
- 1\leq N\leq 100
- 1\leq A _ i\leq 100\ (1\leq i\leq N)
- A contains at least one even number.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N
Output
Print a line containing the sequence of all even numbers in A, with spaces in between.
Sample Input 1
5 1 2 3 5 6
Sample Output 1
2 6
We have A=(1,2,3,5,6). Among them are two even numbers, A _ 2=2 and A _ 5=6, so print 2 and 6 in this order, with a space in between.
Sample Input 2
5 2 2 2 3 3
Sample Output 2
2 2 2
A may contain equal elements.
Sample Input 3
10 22 3 17 8 30 15 12 14 11 17
Sample Output 3
22 8 30 12 14
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
1 から N の番号がついた N 頭の馬が競争をしました。
全ての馬は同時にスタートし、 i 番の馬はスタートからゴールまで T_i 秒かかりました。
1,2,3 着の馬の番号を求めてください。なお、 T_i は相異なることが保証されます。
制約
- 3\leq N \leq 32
- 1\leq T_i \leq 200
- T_i は相異なる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N T_1 \dots T_N
出力
1,2,3 着の馬の番号をそれぞれ空白区切りでこの順に出力せよ。
入力例 1
4 100 110 105 95
出力例 1
4 1 3
4,1,3,2 番の順にゴールしました。1,2,3 着の番号である 4,1,3 をこの順に空白区切りで出力してください。
入力例 2
8 72 74 69 70 73 75 71 77
出力例 2
3 4 7
Score : 200 points
Problem Statement
N horses numbered 1 to N had a race.
All horses started simultaneously, and horse i took T_i seconds from the start to the goal.
Find the numbers of the horses that finished in 1st, 2nd, and 3rd places. It is guaranteed that all T_i are distinct.
Constraints
- 3\leq N \leq 32
- 1\leq T_i \leq 200
- All T_i are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T_1 \dots T_N
Output
Output the numbers of the horses that finished in 1st, 2nd, and 3rd places, in this order, separated by spaces.
Sample Input 1
4 100 110 105 95
Sample Output 1
4 1 3
The horses finished in the order 4, 1, 3, 2. Output the numbers for 1st, 2nd, and 3rd places, which are 4, 1, 3, in this order, separated by spaces.
Sample Input 2
8 72 74 69 70 73 75 71 77
Sample Output 2
3 4 7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
整数 B が与えられます。
A^A = B であるような正の整数 A が存在するならばその値を、存在しないならば -1 を出力してください。
制約
- 1 \leq B \leq 10^{18}
- B は整数
入力
入力は以下の形式で標準入力から与えられる。
B
出力
A^A = B であるような正の整数 A が存在するならばその値を、存在しないならば -1 を出力せよ。
A^A = B を満たす正の整数 A が複数ある場合は、どれを出力しても正解とみなされる。
入力例 1
27
出力例 1
3
3^3 = 27 なので 3 を出力します。
入力例 2
100
出力例 2
-1
A^A = B を満たす A は存在しません。
入力例 3
10000000000
出力例 3
10
Score : 200 points
Problem Statement
You are given an integer B.
If there exists a positive integer A such that A^A = B, print its value; otherwise, output -1.
Constraints
- 1 \leq B \leq 10^{18}
- B is an integer.
Input
The input is given from Standard Input in the following format:
B
Output
If there exists a positive integer A such that A^A = B, print its value; otherwise, print -1.
If there are multiple positive integers A such that A^A = B, any of them will be accepted.
Sample Input 1
27
Sample Output 1
3
3^3 = 27, so print 3.
Sample Input 2
100
Sample Output 2
-1
There is no A such that A^A = B.
Sample Input 3
10000000000
Sample Output 3
10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英大文字からなる長さ 3 の文字列 T が、英小文字からなる文字列 S の 空港コード であるとは、 T が S から次のいずれかの方法により得られることとします。
- S の長さ 3 の(連続とは限らない)部分列をとり、それを英大文字に変換したものを T とする
- S の長さ 2 の(連続とは限らない)部分列をとり、それを英大文字に変換したものの末尾に
Xを追加したものを T とする
文字列 S, T が与えられるので、 T が S の空港コードであるか判定してください。
制約
- S は英小文字からなる長さ 3 以上 10^5 以下の文字列
- T は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
T が S の空港コードであるならば Yes を、そうでないならば No を出力せよ。
入力例 1
narita NRT
出力例 1
Yes
narita の部分列 nrt を英大文字に変換した文字列 NRT は、 narita の空港コードです。
入力例 2
losangeles LAX
出力例 2
Yes
losangeles の部分列 la を英大文字に変換した文字列 LA の末尾に X を追加したもの LAX は、 losangeles の空港コードです。
入力例 3
snuke RNG
出力例 3
No
Score: 300 points
Problem Statement
A string T of length 3 consisting of uppercase English letters is an airport code for a string S of lowercase English letters if and only if T can be derived from S by one of the following methods:
- Take a subsequence of length 3 from S (not necessarily contiguous) and convert it to uppercase letters to form T.
- Take a subsequence of length 2 from S (not necessarily contiguous), convert it to uppercase letters, and append
Xto the end to form T.
Given strings S and T, determine if T is an airport code for S.
Constraints
- S is a string of lowercase English letters with a length between 3 and 10^5, inclusive.
- T is a string of uppercase English letters with a length of 3.
Input
The input is given from Standard Input in the following format:
S T
Output
Print Yes if T is an airport code for S, and No otherwise.
Sample Input 1
narita NRT
Sample Output 1
Yes
The subsequence nrt of narita, when converted to uppercase, forms the string NRT, which is an airport code for narita.
Sample Input 2
losangeles LAX
Sample Output 2
Yes
The subsequence la of losangeles, when converted to uppercase and appended with X, forms the string LAX, which is an airport code for losangeles.
Sample Input 3
snuke RNG
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
ピザ屋で働く高橋くんは、まかないとして美味しいチーズピザを作ることにしました。
今、高橋くんの目の前に N 種類のチーズがあります。
i 種類目のチーズは 1 [g] あたりのおいしさが A_i で、 B_i [g] あります。
ピザのおいしさは、ピザに乗せたチーズのおいしさの総和で決まります。
但し、チーズを使いすぎると怒られてしまうため、乗せたチーズの重さは合計で W [g] 以下である必要があります。
この条件のもとで、可能なピザのおいしさの最大値を求めてください。
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- 1 \le W \le 3 \times 10^8
- 1 \le A_i \le 10^9
- 1 \le B_i \le 1000
入力
入力は以下の形式で標準入力から与えられる。
N W A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを整数として出力せよ。
入力例 1
3 5 3 1 4 2 2 3
出力例 1
15
1 種類目のチーズを 1 [g] 、 2 種類目のチーズを 2 [g] 、 3 種類目のチーズを 2 [g] 乗せるのが最適です。
このとき、ピザのおいしさは 15 となります。
入力例 2
4 100 6 2 1 5 3 9 8 7
出力例 2
100
チーズの重量の総和が W [g] に満たないケースもあります。
入力例 3
10 3141 314944731 649 140276783 228 578012421 809 878510647 519 925326537 943 337666726 611 879137070 306 87808915 39 756059990 244 228622672 291
出力例 3
2357689932073
Score : 300 points
Problem Statement
Takahashi, who works for a pizza restaurant, is making a delicious cheese pizza for staff meals.
There are N kinds of cheese in front of him.
The deliciousness of the i-th kind of cheese is A_i per gram, and B_i grams of this cheese are available.
The deliciousness of the pizza will be the total deliciousness of cheese he puts on top of the pizza.
However, using too much cheese would make his boss angry, so the pizza can have at most W grams of cheese on top of it.
Under this condition, find the maximum possible deliciousness of the pizza.
Constraints
- All values in input are integers.
- 1 \le N \le 3 \times 10^5
- 1 \le W \le 3 \times 10^8
- 1 \le A_i \le 10^9
- 1 \le B_i \le 1000
Input
Input is given from Standard Input in the following format:
N W A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the answer as an integer.
Sample Input 1
3 5 3 1 4 2 2 3
Sample Output 1
15
The optimal choice is to use 1 gram of cheese of the first kind, 2 grams of the second kind, and 2 grams of the third kind.
The pizza will have a deliciousness of 15.
Sample Input 2
4 100 6 2 1 5 3 9 8 7
Sample Output 2
100
There may be less than W grams of cheese in total.
Sample Input 3
10 3141 314944731 649 140276783 228 578012421 809 878510647 519 925326537 943 337666726 611 879137070 306 87808915 39 756059990 244 228622672 291
Sample Output 3
2357689932073
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
この問題は G 問題の部分問題です。
0 と 1 からなる空でない文字列 S が次の条件を満たす時、S を美しい文字列と呼びます。
- (条件) 次の一連の操作を S の長さが 1 になるまで行い、S に残った唯一の文字を
1にすることができる。- 1 \leq i \leq |S| - 1 を満たす整数 i を自由に選ぶ。
- 整数 x を次のように定義する。
- S_i =
0かつ S_{i+1}=0である場合、x = 1 とする。 - S_i =
0かつ S_{i+1}=1である場合、x = 0 とする。 - S_i =
1かつ S_{i+1}=0である場合、x = 0 とする。 - S_i =
1かつ S_{i+1}=1である場合、x = 1 とする。
- S_i =
- S_i と S_{i+1} を取り除き、それらがあった場所に x を数字とみなしたものを 1 個挿入する。
例えば S=10101に対して i=2 を選んで操作を行った場合、操作後の文字列は1001になる。
0 と 1 からなる長さ N の文字列 T があります。
T の部分文字列である美しい文字列の個数を求めてください。ただし、2 つの部分文字列が文字列として同じでも、取り出す位置が異なるならば別々に数えます。
部分文字列とは
S の部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。例えば、
10 は 101 の部分文字列ですが、11 は 101 の部分文字列ではありません。
制約
- 1 \leq N \leq 2 \times 10^5
- N は整数
- T は
0と1からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N T
出力
T の部分文字列である美しい文字列の個数を出力せよ。
入力例 1
3 110
出力例 1
3
T の 1 文字目から 2 文字目までを取り出してできる文字列 11 は美しい文字列です。なぜならば、i=1 として操作を行うと、操作後の文字列は 1 になるからです。
T の部分文字列である美しい文字列は次の 3 個です。
- T の 1 文字目を取り出してできる文字列
1 - T の 2 文字目を取り出してできる文字列
1 - T の 1 文字目から 2 文字目までを取り出してできる文字列
11
入力例 2
4 0000
出力例 2
4
入力例 3
30 011011100101110111100010011010
出力例 3
225
Score : 425 points
Problem Statement
This problem is a subproblem of Problem G.
A non-empty string S consisting of 0 and 1 is called a beautiful string when it satisfies the following condition:
- (Condition) You can perform the following sequence of operations until the length of S becomes 1 and make the only character remaining in S be
1.- Choose any integer i satisfying 1 \leq i \leq |S| - 1.
- Define an integer x as follows:
- If S_i =
0and S_{i+1} =0, let x = 1. - If S_i =
0and S_{i+1} =1, let x = 0. - If S_i =
1and S_{i+1} =0, let x = 0. - If S_i =
1and S_{i+1} =1, let x = 1.
- If S_i =
- Remove S_i and S_{i+1}, and insert the digit corresponding to x in their place.
For example, if S=10101and you choose i=2, the string after the operation is1001.
You are given a string T of length N consisting of 0 and 1.
Find the number of beautiful strings that are substrings of T. Even if two substrings are identical as strings, count them separately if they are taken from different positions.
What are substrings?
A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.For example,
10 is a substring of 101, but 11 is not a substring of 101.
Constraints
- 1 \leq N \leq 2 \times 10^5
- N is an integer.
- T is a string of length N consisting of
0and1.
Input
The input is given from Standard Input in the following format:
N T
Output
Print the number of beautiful strings that are substrings of T.
Sample Input 1
3 110
Sample Output 1
3
The string 11 obtained by taking the 1st through 2nd characters of T is a beautiful string, because if you choose i=1 and perform the operation, the string becomes 1.
The beautiful strings that are substrings of T are the following three strings:
- The string
1obtained by taking the 1st character of T. - The string
1obtained by taking the 2nd character of T. - The string
11obtained by taking the 1st through 2nd characters of T.
Sample Input 2
4 0000
Sample Output 2
4
Sample Input 3
30 011011100101110111100010011010
Sample Output 3
225
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
(1,2,3,\ldots,2^{N}) の順列 P=(P_0,P_1,\ldots,P_{2^{N}-1}) が与えられます。
あなたは次の操作を何回でも(0 回でもよい)行うことができます。
- 0 \leq a \times 2^{b} < (a+1) \times 2^{b} \leq 2^{N} を満たす非負整数 a,b を選び、P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} を反転する。ここで、P_{a \times 2^{b}},P_{a \times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} を反転するとは、P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} を P_{(a+1) \times 2^{b}-1}, P_{(a+1) \times 2^{b}-2},\ldots,P_{a \times 2^{b}} に同時に置き換えることを意味する。
操作を繰り返して得られる P のうち、辞書順最小のものを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1 \leq T \leq 10^{5}
- 1 \leq N \leq 18
- P は (1,2,3,\ldots,2^{N}) の順列
- 各入力ファイルについて、すべてのテストケースの 2^N の総和は 3 \times 10^{5} 以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T
\textrm{case}_i は i 番目のテストケースを表し、以下の形式で与えられる。
N
P_0 P_1 \ldots P_{2^N-1}
出力
T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。
入力例 1
4 1 1 2 2 1 3 4 2 2 2 3 4 1 3 8 3 4 2 1 5 7 6
出力例 1
1 2 1 3 2 4 1 4 2 3 1 5 6 7 2 4 3 8
1 番目のテストケースにおいて、P に対して操作を一回も行わないとき、P=(1,2) となります。これは辞書順で最小の順列です。よって答えは(1,2) です。
2 番目のテストケースにおいて、a=1,b=1 を選んで操作を行うと P=(1,3,2,4) となります。P に対して何回操作を行っても (1,3,2,4) より辞書順で小さい順列を得られません。よって答えは (1,3,2,4) です。
3 番目のテストケースにおいて、以下の順番で操作を行うことで、P=(1,4,2,3) が得られます。
- a=0,b=1 を選んで操作を行う。P=(3,2,4,1) となる。
- a=0,b=2 を選んで操作を行う。P=(1,4,2,3) となる。
P に対して何回操作を行っても (1,4,2,3) より辞書順で小さい順列を得られません。よって答えは (1,4,2,3) です。
Score : 450 points
Problem Statement
You are given a permutation P=(P_0,P_1,\ldots,P_{2^{N}-1}) of (1,2,3,\ldots,2^{N}).
You can perform the following operation any number of times (possibly zero):
- Choose non-negative integers a,b satisfying 0 \leq a \times 2^{b} < (a+1) \times 2^{b} \leq 2^{N}, and reverse P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1}. Here, reversing P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} means simultaneously replacing P_{a \times 2^{b}}, P_{a\times 2^{b}+1},\ldots,P_{(a+1) \times 2^{b}-1} with P_{(a+1) \times 2^{b}-1}, P_{(a+1) \times 2^{b}-2},\ldots,P_{a \times 2^{b}}.
Find the lexicographically smallest permutation P that can be obtained by repeating the operation.
You are given T test cases, so find the answer for each.
Constraints
- 1 \leq T \leq 10^{5}
- 1 \leq N \leq 18
- P is a permutation of (1,2,3,\ldots,2^{N}).
- For each input file, the sum of 2^N over all test cases is at most 3 \times 10^{5}.
- All input values are integers.
Input
The input is given from standard input in the following format:
T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T
\textrm{case}_i represents the i-th test case and is given in the following format:
N
P_0 P_1 \ldots P_{2^N-1}
Output
Output T lines. The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case.
Sample Input 1
4 1 1 2 2 1 3 4 2 2 2 3 4 1 3 8 3 4 2 1 5 7 6
Sample Output 1
1 2 1 3 2 4 1 4 2 3 1 5 6 7 2 4 3 8
In the first test case, when no operation is performed on P, P=(1,2). This is the lexicographically smallest permutation. Thus, the answer is (1,2).
In the second test case, when we perform the operation with a=1,b=1, P becomes (1,3,2,4). No matter how many operations we perform on P, we cannot obtain a permutation lexicographically smaller than (1,3,2,4). Thus, the answer is (1,3,2,4).
In the third test case, by performing operations in the following order, we can obtain P=(1,4,2,3):
- Perform the operation with a=0,b=1. P becomes (3,2,4,1).
- Perform the operation with a=0,b=2. P becomes (1,4,2,3).
No matter how many operations we perform on P, we cannot obtain a permutation lexicographically smaller than (1,4,2,3). Thus, the answer is (1,4,2,3).
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
高橋君のタンスの中には N 色の靴下があり、色 i の靴下は A_i 枚あります。
高橋君ははじめこれらの靴下とは別に色 C の靴下を 1 枚タンスの外に持っており、操作を終える条件を満たすまで以下の操作を繰り返します。
- タンスから靴下を 1 枚一様ランダムに選んで取り出す。その後、タンスの外に持っている 2 枚の靴下が同じ色ならば操作を終える。そうでないならば、片方の靴下を選んでタンスに戻す。ただし、高橋君は常に今後の靴下を取り出す回数の期待値が最小となるようにタンスに戻す靴下を選ぶ。
操作を終えるまでに靴下を取り出す回数の期待値を \bmod\ 998244353 で求めてください。
期待値を \bmod\ 998244353 で求めるとは
この問題の制約のもとで、期待値は存在し、有理数になることが証明できます。 また、その値を既約分数 \frac{P}{Q} (Q > 0) で表したとき、Q \not\equiv 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 期待値を \bmod\ 998244353 で求めるとは、この R の値を求めることを指します。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq C \leq N
- 1 \leq A_i \leq 3000
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N C A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 2 3 1 2
出力例 1
249561092
靴下を取り出す回数の期待値は \frac{15}{4} となります。
入力例 2
8 4 4 1 6 2 5 1 7 3
出力例 2
393623786
Score : 525 points
Problem Statement
In Takahashi's chest of drawers, there are socks of N colors, with A_i socks of color i.
Initially, Takahashi has one sock of color C outside the chest of drawers, separate from these socks, and repeats the following operation until the termination condition is met:
- Uniformly randomly choose and draw 1 sock from the chest of drawers. Then, if the two socks he has outside the chest of drawers are the same color, terminate the operation. Otherwise, choose one of the socks and put it back in the chest of drawers. He always chooses the sock to put back so as to minimize the expected number of future sock draws.
Find the expected number, modulo 998244353, of sock draws until the operation terminates.
Finding the expected value modulo 998244353
Under the constraints of this problem, it can be proved that the expected value exists and is a rational number. It can also be proved that when this value is expressed as an irreducible fraction \frac{P}{Q} (Q > 0), we have Q \not\equiv 0 \pmod{998244353}. Therefore, there uniquely exists an integer R satisfying R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Finding the expected value modulo 998244353 means finding this value of R.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq C \leq N
- 1 \leq A_i \leq 3000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N C A_1 A_2 \ldots A_N
Output
Output the answer.
Sample Input 1
3 2 3 1 2
Sample Output 1
249561092
The expected number of sock draws is \frac{15}{4}.
Sample Input 2
8 4 4 1 6 2 5 1 7 3
Sample Output 2
393623786