C - Mex

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

長さ N の整数からなる数列 A=(A_1,\ldots,A_N) が与えられます。

A_1,\ldots,A_N に含まれない最小の非負整数を求めてください。

制約

  • 1 \leq N \leq 2000
  • 0 \leq A_i \leq 2000
  • 入力は全て整数である

入力

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

N
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

8
0 3 2 6 2 1 0 0

出力例 1

4

非負整数は 0,1,2,3,4,\ldots と続きます。
0,1,2,3A に含まれ、4A に含まれないので、答えは 4 です。


入力例 2

3
2000 2000 2000

出力例 2

0

Score : 200 points

Problem Statement

You are given a sequence of length N consisting of integers: A=(A_1,\ldots,A_N).

Find the smallest non-negative integer not in (A_1,\ldots,A_N).

Constraints

  • 1 \leq N \leq 2000
  • 0 \leq A_i \leq 2000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

8
0 3 2 6 2 1 0 0

Sample Output 1

4

The non-negative integers are 0,1,2,3,4,\ldots.
We have 0,1,2,3 in A, but not 4, so the answer is 4.


Sample Input 2

3
2000 2000 2000

Sample Output 2

0
D - Measure

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

正整数 N が与えられるので、下記で定まる長さ (N+1) の文字列 s_0s_1\ldots s_N を出力してください。

i = 0, 1, 2, \ldots, N について、

  • 1 以上 9 以下の N の約数 j であって、iN/j の倍数であるものが存在するとき、そのような j のうち最小のものに対応する数字を s_i とする。(よって、この場合 s_i12\ldots9 のいずれかである。)
  • そのような j が存在しないとき、s_i- とする。

制約

  • 1 \leq N \leq 1000
  • 入力はすべて整数

入力

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

N

出力

答えを出力せよ。


入力例 1

12

出力例 1

1-643-2-346-1

以下で、いくつかの i について s_i の決め方を説明します。

  • i = 0 について、1 以上 9 以下の N の約数 j であって iN/j の倍数であるものは、j = 1, 2, 3, 4, 65 個です。そのうち最小のものは 1 であるので、s_0 = 1 です。

  • i = 4 について、1 以上 9 以下の N の約数 j であって iN/j の倍数であるものは、j = 3, 62 個です。そのうち最小のものは 3 であるので、s_4 = 3 です。

  • i = 11 について、1 以上 9 以下の N の約数 j であって iN/j の倍数であるものは存在しないので、s_{11} = - です。


入力例 2

7

出力例 2

17777771

入力例 3

1

出力例 3

11

Score : 200 points

Problem Statement

You are given a positive integer N. Print a string of length (N+1), s_0s_1\ldots s_N, defined as follows.

For each i = 0, 1, 2, \ldots, N,

  • if there is a divisor j of N that is between 1 and 9, inclusive, and i is a multiple of N/j, then s_i is the digit corresponding to the smallest such j (s_i will thus be one of 1, 2, ..., 9);
  • if no such j exists, then s_i is -.

Constraints

  • 1 \leq N \leq 1000
  • All input values are integers.

Input

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

N

Output

Print the answer.


Sample Input 1

12

Sample Output 1

1-643-2-346-1

We will explain how to determine s_i for some i.

  • For i = 0, the divisors j of N between 1 and 9 such that i is a multiple of N/j are 1, 2, 3, 4, 6. The smallest of these is 1, so s_0 = 1.

  • For i = 4, the divisors j of N between 1 and 9 such that i is a multiple of N/j are 3, 6. The smallest of these is 3, so s_4 = 3.

  • For i = 11, there are no divisors j of N between 1 and 9 such that i is a multiple of N/j, so s_{11} = -.


Sample Input 2

7

Sample Output 2

17777771

Sample Input 3

1

Sample Output 3

11
E - Inverse of Permutation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1,2,\dots,N1 回ずつ現れる長さ N の数列を「長さ N の順列」と呼びます。
長さ N の順列 P = (p_1, p_2,\dots,p_N) が与えられるので、以下の条件を満たす長さ N の順列 Q = (q_1,\dots,q_N) を出力してください。

  • 全ての i (1 \leq i \leq N) に対して Qp_i 番目の要素が i である。

ただし、条件を満たす Q は必ずただ 1 つ存在することが証明できます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • (p_1,p_2,\dots,p_N) は長さ N の順列である。
  • 入力は全て整数である。

入力

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

N
p_1 p_2 \dots p_N

出力

数列 Q を空白区切りで 1 行で出力せよ。

q_1 q_2 \dots q_N

入力例 1

3
2 3 1

出力例 1

3 1 2

以下に説明する通り、 Q=(3,1,2) は条件を満たす順列です。

  • i = 1 のとき p_i = 2, q_2 = 1
  • i = 2 のとき p_i = 3, q_3 = 2
  • i = 3 のとき p_i = 1, q_1 = 3

入力例 2

3
1 2 3

出力例 2

1 2 3

全ての i (1 \leq i \leq N) に対して p_i = i が成り立つときは P = Q になります。


入力例 3

5
5 3 2 4 1

出力例 3

5 3 2 4 1

Score : 300 points

Problem Statement

We will call a sequence of length N where each of 1,2,\dots,N occurs once as a permutation of length N.
Given a permutation of length N, P = (p_1, p_2,\dots,p_N), print a permutation of length N, Q = (q_1,\dots,q_N), that satisfies the following condition.

  • For every i (1 \leq i \leq N), the p_i-th element of Q is i.

It can be proved that there exists a unique Q that satisfies the condition.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • (p_1,p_2,\dots,p_N) is a permutation of length N (defined in Problem Statement).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \dots p_N

Output

Print the sequence Q in one line, with spaces in between.

q_1 q_2 \dots q_N

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

The permutation Q=(3,1,2) satisfies the condition, as follows.

  • For i = 1, we have p_i = 2, q_2 = 1.
  • For i = 2, we have p_i = 3, q_3 = 2.
  • For i = 3, we have p_i = 1, q_1 = 3.

Sample Input 2

3
1 2 3

Sample Output 2

1 2 3

If p_i = i for every i (1 \leq i \leq N), we will have P = Q.


Sample Input 3

5
5 3 2 4 1

Sample Output 3

5 3 2 4 1
F - Dice Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の整数からなる数列 A=(A_1,\ldots,A_N) であって、以下の条件を全て満たすものは何通りありますか?

  • 1\le A_i \le M (1 \le i \le N)

  • \displaystyle\sum _{i=1}^N A_i \leq K

ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割った余りを求めてください。

制約

  • 1 \leq N, M \leq 50
  • N \leq K \leq NM
  • 入力は全て整数

入力

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

N M K

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

2 3 4

出力例 1

6

条件を満たす数列は以下の 6 つです。

  • (1,1)
  • (1,2)
  • (1,3)
  • (2,1)
  • (2,2)
  • (3,1)

入力例 2

31 41 592

出力例 2

798416518

答えを 998244353 で割った余りを出力してください。

Score : 300 points

Problem Statement

How many integer sequences of length N, A=(A_1, \ldots, A_N), satisfy all of the conditions below?

  • 1\le A_i \le M (1 \le i \le N)

  • \displaystyle\sum _{i=1}^N A_i \leq K

Since the count can get enormous, find it modulo 998244353.

Constraints

  • 1 \leq N, M \leq 50
  • N \leq K \leq NM
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the answer.


Sample Input 1

2 3 4

Sample Output 1

6

The following six sequences satisfy the conditions.

  • (1,1)
  • (1,2)
  • (1,3)
  • (2,1)
  • (2,2)
  • (3,1)

Sample Input 2

31 41 592

Sample Output 2

798416518

Be sure to print the count modulo 998244353.

G - Island Tour

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

AtCoder 諸島は N 個の島からなり、これらの島々は N 本の橋によって結ばれています。 島には 1 から N までの番号が付けられていて、i\ (1\leq i\leq N-1) 本目の橋は島 i と島 i+1 を、N 本目の橋は島 N と島 1 を双方向に結んでいます。 橋を渡る以外に島の間を行き来する方法は存在しません。

AtCoder 諸島では、島 X_1 から始めて島 X_2,X_3,\dots,X_M を順に訪れるツアーが定期的に催行されています。 移動の過程で訪れる島とは別の島を経由することもあり、ツアー中に橋を通る回数の合計がツアーの長さと定義されます。

厳密には、ツアーとは以下の条件を全て満たす l+1 個の島の列 a_0,a_1,\dots,a_l のことであり、その長さl として定義されます。

  • 全ての j\ (0\leq j\leq l-1) について、島 a_j と島 a_{j+1} は橋で直接結ばれている
  • ある 0 = y_1 < y_2 < \dots < y_M = l が存在して、全ての k\ (1\leq k\leq M) について a_{y_k} = X_k

財政難に苦しむ AtCoder 諸島では、維持費削減のため橋を 1 本封鎖することになりました。 封鎖する橋をうまく選んだとき、ツアーの長さの最小値がいくつになるか求めてください。

制約

  • 3\leq N \leq 2\times 10^5
  • 2\leq M \leq 2\times 10^5
  • 1\leq X_k\leq N
  • X_k\neq X_{k+1}\ (1\leq k\leq M-1)
  • 入力は全て整数

入力

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

N M
X_1 X_2 \dots X_M

出力

答えを整数として出力せよ。


入力例 1

3 3
1 3 2

出力例 1

2
  • 1 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2)=(1,3,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 2 のツアーが催行できます。これより短いツアーは存在しません。
  • 2 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2,a_3)=(1,3,1,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 3 のツアーが催行できます。これより短いツアーは存在しません。
  • 3 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2,a_3)=(1,2,3,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 3 のツアーが催行できます。これより短いツアーは存在しません。

よって、封鎖する橋をうまく選んだときのツアーの長さの最小値は 2 です。

以下の図は左から順に橋 1,2,3 を封鎖した場合を表し、数字の書かれた丸が島、丸同士を結ぶ線が橋、青い矢印が最短のツアーの経路を表します。


入力例 2

4 5
2 4 2 4 2

出力例 2

8

X_1,X_2,\dots,X_M の中に同じ島が複数回現れることもあります。


入力例 3

163054 10
62874 19143 77750 111403 29327 56303 6659 18896 64175 26369

出力例 3

390009

Score: 425 points

Problem Statement

The AtCoder Archipelago consists of N islands connected by N bridges. The islands are numbered from 1 to N, and the i-th bridge (1\leq i\leq N-1) connects islands i and i+1 bidirectionally, while the N-th bridge connects islands N and 1 bidirectionally. There is no way to travel between islands other than crossing the bridges.

On the islands, a tour that starts from island X_1 and visits islands X_2, X_3, \dots, X_M in order is regularly conducted. The tour may pass through islands other than those being visited, and the total number of times bridges are crossed during the tour is defined as the length of the tour.

More precisely, a tour is a sequence of l+1 islands a_0, a_1, \dots, a_l that satisfies all the following conditions, and its length is defined as l:

  • For all j\ (0\leq j\leq l-1), islands a_j and a_{j+1} are directly connected by a bridge.
  • There are some 0 = y_1 < y_2 < \dots < y_M = l such that for all k\ (1\leq k\leq M), a_{y_k} = X_k.

Due to financial difficulties, the islands will close one bridge to reduce maintenance costs. Determine the minimum possible length of the tour when the bridge to be closed is chosen optimally.

Constraints

  • 3\leq N \leq 2\times 10^5
  • 2\leq M \leq 2\times 10^5
  • 1\leq X_k\leq N
  • X_k\neq X_{k+1}\ (1\leq k\leq M-1)
  • All input values are integers.

Input

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

N M
X_1 X_2 \dots X_M

Output

Print the answer as an integer.


Sample Input 1

3 3
1 3 2

Sample Output 1

2
  • If the first bridge is closed: By taking the sequence of islands (a_0, a_1, a_2) = (1, 3, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 2 can be conducted. There is no shorter tour.
  • If the second bridge is closed: By taking the sequence of islands (a_0, a_1, a_2, a_3) = (1, 3, 1, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 3 can be conducted. There is no shorter tour.
  • If the third bridge is closed: By taking the sequence of islands (a_0, a_1, a_2, a_3) = (1, 2, 3, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 3 can be conducted. There is no shorter tour.

Therefore, the minimum possible length of the tour when the bridge to be closed is chosen optimally is 2.

The following figure shows, from left to right, the cases when bridges 1, 2, 3 are closed, respectively. The circles with numbers represent islands, the lines connecting the circles represent bridges, and the blue arrows represent the shortest tour routes.


Sample Input 2

4 5
2 4 2 4 2

Sample Output 2

8

The same island may appear multiple times in X_1, X_2, \dots, X_M.


Sample Input 3

163054 10
62874 19143 77750 111403 29327 56303 6659 18896 64175 26369

Sample Output 3

390009