A - Pairing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

4 個のボールがあり、i 個目のボールの色は A_i です。

同じ色のボールを 2 つ選び両方捨てるという操作を最大何回行えるか求めてください。

制約

  • A_1,A_2,A_3,A_4 はそれぞれ 1 以上 4 以下の整数

入力

入力は以下の形式で標準入力から与えられる。

A_1 A_2 A_3 A_4

出力

操作回数の最大値を整数として出力せよ。


入力例 1

2 1 2 1

出力例 1

2

1 個目のボールと 3 個目のボールはどちらも色が 2 なので、1 個目のボールと 3 個目のボールを共に捨てる操作を行えます。

次に、2 個目のボールと 4 個目のボールはどちらも色が 1 なので、2 個目のボールと 4 個目のボールを共に捨てる操作を行えます。

合計で 2 回操作を行えます。


入力例 2

4 4 4 1

出力例 2

1

入力例 3

1 2 3 4

出力例 3

0

操作を一度も行えない場合もあります。

Score : 100 points

Problem Statement

There are four balls, and the color of the i-th ball is A_i.

Find the maximum number of times you can perform this operation: choose two balls of the same color and discard both.

Constraints

  • Each of A_1, A_2, A_3, A_4 is an integer between 1 and 4, inclusive.

Input

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

A_1 A_2 A_3 A_4

Output

Print the maximum number of times the operation can be performed as an integer.


Sample Input 1

2 1 2 1

Sample Output 1

2

The first and third balls both have color 2, so you can perform the operation to discard the first and third balls together.

Next, the second and fourth balls both have color 1, so you can perform the operation to discard the second and fourth balls together.

Hence, you can perform a total of two operations.


Sample Input 2

4 4 4 1

Sample Output 2

1

Sample Input 3

1 2 3 4

Sample Output 3

0

There are cases where you cannot perform the operation even once.

B - First Contest of the Year

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

ある星では 1 年が D 日あり、7 日ごとにコンテストが開催されています。コンテストが日をまたいで開催されることはありません。

ある年の最初のコンテストは 1 年のうち F 日目に開催されました。その次の年の最初のコンテストは 1 年のうち何日目に開催されますか?

なお、この問題の制約のもとでは、次の年にコンテストが 1 回以上開催されることが保証されます。

制約

  • 10 \leq D \leq 366
  • 1 \leq F \leq 7
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

D F

出力

答えを出力せよ。


入力例 1

365 4

出力例 1

3

ある年の最初のコンテストの開催が 1 年のうち 4 日目だったとき、次の年の最初のコンテストの開催は 1 年のうち 3 日目になります。


入力例 2

10 5

出力例 2

2

Score : 100 points

Problem Statement

On a certain planet, a year has D days, and contests are held every 7 days. Contests never span across days.

The first contest of a certain year was held on the F-th day of the year. On what day of the next year will the first contest of the next year be held?

The constraints of this problem guarantee that at least one contest will be held in the next year.

Constraints

  • 10 \leq D \leq 366
  • 1 \leq F \leq 7
  • All input values are integers.

Input

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

D F

Output

Print the integer N such that the next contest is held on the N-th day of the year.


Sample Input 1

365 4

Sample Output 1

3

If the first contest of a certain year was held on the 4th day of the year, the first contest of the next year will be held on the 3rd day of the year.


Sample Input 2

10 5

Sample Output 2

2
C - qwerty

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

1 以上 26 以下の整数からなる長さ 26 の数列 P=(P_1,P_2, \ldots ,P_{26}) が与えられます。ここで、P の要素は相異なることが保証されます。

以下の条件を満たす長さ 26 の文字列 S を出力してください。

  • 任意の i\, (1 \leq i \leq 26) について、Si 文字目は辞書順で小さい方から P_i 番目の英小文字である。

制約

  • 1 \leq P_i \leq 26
  • P_i \neq P_j (i \neq j)
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

P_1 P_2 \ldots P_{26}

出力

文字列 S を出力せよ。


入力例 1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

出力例 1

abcdefghijklmnopqrstuvwxyz

入力例 2

2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

出力例 2

bacdefghijklmnopqrstuvwxyz

入力例 3

5 11 12 16 25 17 18 1 7 10 4 23 20 3 2 24 26 19 14 9 6 22 8 13 15 21

出力例 3

eklpyqragjdwtcbxzsnifvhmou

Score : 200 points

Problem Statement

You are given a sequence of 26 integers P=(P_1,P_2, \ldots ,P_{26}) consisting of integers from 1 through 26. It is guaranteed that all elements in P are distinct.

Print a string S of length 26 that satisfies the following condition.

  • For every i (1 \leq i \leq 26), the i-th character of S is the lowercase English letter that comes P_i-th in alphabetical order.

Constraints

  • 1 \leq P_i \leq 26
  • P_i \neq P_j (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

P_1 P_2 \ldots P_{26}

Output

Print the string S.


Sample Input 1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Sample Output 1

abcdefghijklmnopqrstuvwxyz

Sample Input 2

2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Sample Output 2

bacdefghijklmnopqrstuvwxyz

Sample Input 3

5 11 12 16 25 17 18 1 7 10 4 23 20 3 2 24 26 19 14 9 6 22 8 13 15 21

Sample Output 3

eklpyqragjdwtcbxzsnifvhmou
D - At Most 3 (Judge ver.)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

おもり 1, おもり 2, \dots, おもり NN 個のおもりがあります。おもり i の重さは A_i です。
以下の条件を満たす正整数 n良い整数 と呼びます。

  • \bf{3} 個以下 の異なるおもりを自由に選んで、選んだおもりの重さの和を n にすることができる。

W 以下の正整数のうち、良い整数は何個ありますか?

制約

  • 1 \leq N \leq 300
  • 1 \leq W \leq 10^6
  • 1 \leq A_i \leq 10^6
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N W
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

2 10
1 3

出力例 1

3

おもり 1 のみを選ぶと重さの和は 1 になります。よって 1 は良い整数です。
おもり 2 のみを選ぶと重さの和は 3 になります。よって 3 は良い整数です。
おもり 1 とおもり 2 を選ぶと重さの和は 4 になります。よって 4 は良い整数です。
これら以外に良い整数は存在しません。また、1,3,4 のいずれも W 以下の整数です。よって答えは 3 個になります。


入力例 2

2 1
2 3

出力例 2

0

W 以下の良い整数は存在しません。


入力例 3

4 12
3 3 3 3

出力例 3

3

良い整数は 3,6,93 個です。
たとえばおもり 1, おもり 2, おもり 3 を選ぶと重さの和は 9 になるので、9 は良い整数です。
12 は良い整数 ではない ことに注意してください。


入力例 4

7 251
202 20 5 1 4 2 100

出力例 4

48

Score : 200 points

Problem Statement

There are N weights called Weight 1, Weight 2, \dots, Weight N. Weight i has a mass of A_i.
Let us say a positive integer n is a good integer if the following condition is satisfied:

  • We can choose at most 3 different weights so that they have a total mass of n.

How many positive integers less than or equal to W are good integers?

Constraints

  • 1 \leq N \leq 300
  • 1 \leq W \leq 10^6
  • 1 \leq A_i \leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N W
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

2 10
1 3

Sample Output 1

3

If we choose only Weight 1, it has a total mass of 1, so 1 is a good integer.
If we choose only Weight 2, it has a total mass of 3, so 3 is a good integer.
If we choose Weights 1 and 2, they have a total mass of 4, so 4 is a good integer.
No other integer is a good integer. Also, all of 1, 3, and 4 are integers less than or equal to W. Therefore, the answer is 3.


Sample Input 2

2 1
2 3

Sample Output 2

0

There are no good integers less than or equal to W.


Sample Input 3

4 12
3 3 3 3

Sample Output 3

3

There are 3 good integers: 3, 6, and 9.
For example, if we choose Weights 1, 2, and 3, they have a total mass of 9, so 9 is a good integer.
Note that 12 is not a good integer.


Sample Input 4

7 251
202 20 5 1 4 2 100

Sample Output 4

48
E - Max MEX

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の非負整数列 A が与えられます。
A から K 要素を選んで順序を保ったまま抜き出した列を B としたとき、 MEX(B) としてありえる最大値を求めてください。

但し、数列 X に対して MEX(X) は以下の条件を満たす唯一の非負整数 m として定義されます。

  • 0 \le i < m を満たす全ての整数 iX に含まれる。
  • mX に含まれない。

制約

  • 入力は全て整数
  • 1 \le K \le N \le 3 \times 10^5
  • 0 \le A_i \le 10^9

入力

入力は以下の形式で標準入力から与えられる。

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

7 3
2 0 2 3 2 1 9

出力例 1

3

この入力では A=(2,0,2,3,2,1,9) であり、ここから K=3 要素を選んで抜き出して数列 B を得ます。例えば、

  • 1,2,3 要素目を抜き出した時、 MEX(B)=MEX(2,0,2)=1
  • 3,4,6 要素目を抜き出した時、 MEX(B)=MEX(2,3,1)=0
  • 2,6,7 要素目を抜き出した時、 MEX(B)=MEX(0,1,9)=2
  • 2,3,6 要素目を抜き出した時、 MEX(B)=MEX(0,2,1)=3

のようになります。
達成可能な MEX の最大値は 3 です。

Score : 300 points

Problem Statement

You are given a length-N sequence of non-negative integers.
When B is a sequence obtained by choosing K elements from A and concatenating them without changing the order, find the maximum possible value of MEX(B).

Here, for a sequence X, we define MEX(X) as the unique non-negative integer m that satisfies the following conditions:

  • Every integer i such that 0 \le i < m is contained in X.
  • m is not contained in X.

Constraints

  • All values in the input are integers.
  • 1 \le K \le N \le 3 \times 10^5
  • 0 \le A_i \le 10^9

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

7 3
2 0 2 3 2 1 9

Sample Output 1

3

In this input, A=(2,0,2,3,2,1,9), and you obtain the sequence B by picking K=3 elements. For example,

  • If the 1-st, 2-nd, and 3-rd elements are chosen, MEX(B)=MEX(2,0,2)=1.
  • If the 3-rd, 4-th, and 6-th elements are chosen, MEX(B)=MEX(2,3,1)=0.
  • If the 2-nd, 6-th, and 7-th elements are chosen, MEX(B)=MEX(0,1,9)=2.
  • If the 2-nd, 3-rd, and 6-th elements are chosen, MEX(B)=MEX(0,2,1)=3.

The maximum possible MEX is 3.

F - Bib

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1 から N の番号がついた N 人の人がいます。

i は数 Q_i が書かれたゼッケンを着けており、人 P_i を見つめています。

i が書かれたゼッケンを着けている人が見つめている人の着けているゼッケンにかかれている数を、i=1,2,\ldots,N のそれぞれについて求めてください。

制約

  • 2 \leq N \leq 3\times 10^5
  • 1 \leq P_i \leq N
  • P_i は相異なる
  • 1 \leq Q_i \leq N
  • Q_i は相異なる
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
P_1 P_2 \dots P_N
Q_1 Q_2 \dots Q_N

出力

i が書かれたゼッケンを着けている人が見つめている人の着けているゼッケンにかかれている数を S_i とする。
S_1,S_2,\ldots,S_N をこの順に空白区切りで出力せよ。


入力例 1

4
4 3 2 1
2 3 1 4

出力例 1

3 4 1 2

31 が書かれたゼッケンを着けており、人 3 が見つめている人 23 が書かれたゼッケンを着けています。 よって i=1 に対する答えは 3 になります。

図


入力例 2

10
2 6 4 3 7 8 9 10 1 5
1 4 8 2 10 5 7 3 9 6

出力例 2

4 8 6 5 3 10 9 2 1 7

Score : 300 points

Problem Statement

There are N people numbered from 1 to N.

Person i is wearing a bib with the number Q_i and is staring at person P_i.

For each i = 1,2,\ldots,N, find the number written on the bib of the person that the person wearing the bib with number i is staring at.

Constraints

  • 2 \leq N \leq 3\times 10^5
  • 1 \leq P_i \leq N
  • The values of P_i are distinct.
  • 1 \leq Q_i \leq N
  • The values of Q_i are distinct.
  • All input values are integers.

Input

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

N
P_1 P_2 \dots P_N
Q_1 Q_2 \dots Q_N

Output

Let S_i be the number written on the bib of the person that the person wearing the bib with number i is staring at.
Print S_1, S_2, \ldots, S_N in this order, separated by a single space.


Sample Input 1

4
4 3 2 1
2 3 1 4

Sample Output 1

3 4 1 2

Person 3 is wearing the bib with the number 1, and the person that person 3 is staring at, person 2, is wearing the bib with the number 3. Thus, the answer for i = 1 is 3.

Figure


Sample Input 2

10
2 6 4 3 7 8 9 10 1 5
1 4 8 2 10 5 7 3 9 6

Sample Output 2

4 8 6 5 3 10 9 2 1 7
G - Palindromic Number

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

非負整数 X10 進表記(先行ゼロ無し)で表した文字列が回文である時、X を回文数と呼びます。
例えば 363, 12344321, 0 はいずれも回文数です。

小さい方から N 番目の回文数を求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • N は整数

入力

入力は以下の形式で標準入力から与えられる。

N

出力

小さい方から N 番目の回文数を出力せよ。


入力例 1

46

出力例 1

363

小さい方から 46 番目の回文数は 363 です。


入力例 2

1

出力例 2

0

入力例 3

1000000000000000000

出力例 3

90000000000000000000000000000000009

Score : 350 points

Problem Statement

A non-negative integer X is called a palindrome number if its decimal representation (without leading zeros) is a palindrome.
For example, 363, 12344321, and 0 are all palindrome numbers.

Find the N-th smallest palindrome number.

Constraints

  • 1 \leq N \leq 10^{18}
  • N is an integer.

Input

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

N

Output

Print the N-th smallest palindrome number.


Sample Input 1

46

Sample Output 1

363

The 46th smallest palindrome number is 363.


Sample Input 2

1

Sample Output 2

0

Sample Input 3

1000000000000000000

Sample Output 3

90000000000000000000000000000000009
H - Karuta

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

英小文字からなる文字列が N 個与えられます。i \, (i = 1, 2, \dots, N) 番目のものを S_i と表します。

二つの文字列 x, y に対し、以下の条件を全て満たす最大の整数 n\mathrm{LCP}(x, y) と表します。

  • x, y の長さはいずれも n 以上
  • 1 以上 n 以下の全ての整数 i に対し、xi 文字目と yi 文字目が等しい

全ての i = 1, 2, \dots, N に対し、以下の値を求めてください。

  • \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j)

制約

  • 2 \leq N \leq 5 \times 10^5
  • N は整数
  • S_i は英小文字からなる長さ 1 以上の文字列 (i = 1, 2, \dots, N)
  • S_i の長さの総和は 5 \times 10^5 以下

入力

入力は以下の形式で標準入力から与えられる。

N
S_1
S_2
\vdots
S_N

出力

N 行出力せよ。i \, (i = 1, 2, \dots, N) 行目には、\displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j) を出力せよ。


入力例 1

3
abc
abb
aac

出力例 1

2
2
1

\mathrm{LCP}(S_1, S_2) = 2, \mathrm{LCP}(S_1, S_3) = 1, \mathrm{LCP}(S_2, S_3) = 1 です。


入力例 2

11
abracadabra
bracadabra
racadabra
acadabra
cadabra
adabra
dabra
abra
bra
ra
a

出力例 2

4
3
2
1
0
1
0
4
3
2
1

Score : 500 points

Problem Statement

You are given N strings consisting of lowercase English letters. Let S_i be the i-th (i = 1, 2, \dots, N) of them.

For two strings x and y, \mathrm{LCP}(x, y) is defined to be the maximum integer n that satisfies all of the following conditions:

  • The lengths of x and y are both at least n.
  • For all integers i between 1 and n, inclusive, the i-th character of x and that of y are equal.

Find the following value for all i = 1, 2, \dots, N:

  • \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j)

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • N is an integer.
  • S_i is a string of length at least 1 consisting of lowercase English letters (i = 1, 2, \dots, N).
  • The sum of lengths of S_i is at most 5 \times 10^5.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print N lines. The i-th (i = 1, 2, \dots, N) line should contain \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j).


Sample Input 1

3
abc
abb
aac

Sample Output 1

2
2
1

\mathrm{LCP}(S_1, S_2) = 2, \mathrm{LCP}(S_1, S_3) = 1, and \mathrm{LCP}(S_2, S_3) = 1.


Sample Input 2

11
abracadabra
bracadabra
racadabra
acadabra
cadabra
adabra
dabra
abra
bra
ra
a

Sample Output 2

4
3
2
1
0
1
0
4
3
2
1
I - |LIS| = 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

以下の条件を全て満たす数列の個数を、998244353 で割った余りを求めてください。

  • 数列の長さが N
  • 数列の各項は 1 以上 M 以下の整数
  • 最長増加部分列の長さがちょうど 3

注記

数列の部分列とは、数列から 0 個以上の要素を取り除いた後、残りの要素を元の順序で連結して得られる数列のことをいいます。
例えば、(10,30)(10,20,30) の部分列ですが、(20,10)(10,20,30) の部分列ではありません。

数列の最長増加部分列とは、数列の狭義単調増加な部分列のうち、長さが最大のもののことをいいます。

制約

  • 3 \leq N \leq 1000
  • 3 \leq M \leq 10
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N M

出力

答えを出力せよ。


入力例 1

4 5

出力例 1

135

例えば (3,4,1,5) は条件を満たす数列です。
一方 (4,4,1,5) は最長増加部分列の長さが 2 なので条件を満たしません。


入力例 2

3 4

出力例 2

4

入力例 3

111 3

出力例 3

144980434

998244353 で割った余りを求めてください。

Score : 500 points

Problem Statement

Find the number of sequences that satisfy all of the conditions below, modulo 998244353.

  • The length is N.
  • Each of the elements is an integer between 1 and M (inclusive).
  • Its longest increasing subsequence has the length of exactly 3.

Notes

A subsequence of a sequence is the result of removing zero or more elements from it and concatenating the remaining elements without changing the order. For example, (10,30) is a subsequence of (10,20,30), while (20,10) is not a subsequence of (10,20,30).

A longest increasing subsequence of a sequence is its strictly increasing subsequence with the greatest length.

Constraints

  • 3 \leq N \leq 1000
  • 3 \leq M \leq 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.


Sample Input 1

4 5

Sample Output 1

135

One sequence that satisfies the conditions is (3,4,1,5).
On the other hand, (4,4,1,5) do not, because its longest increasing subsequence has the length of 2.


Sample Input 2

3 4

Sample Output 2

4

Sample Input 3

111 3

Sample Output 3

144980434

Find the count modulo 998244353.