A - World Cup

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

あるスポーツ大会は西暦年を 4 で割った余りが 2 である年の 6 月に開催されます。
現在が西暦 Y 年の 1 月である時、このスポーツ大会が次に開催されるのは西暦何年になるかを求めてください。

制約

  • 2000 \leq Y \leq 3000
  • Y は整数

入力

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

Y

出力

答えを出力せよ。


入力例 1

2022

出力例 1

2022

20224 で割った余りが 2 なので、現在が西暦 2022 年の 1 月である時、次の開催は同年の 6 月です。


入力例 2

2023

出力例 2

2026

入力例 3

3000

出力例 3

3002

Score : 100 points

Problem Statement

A sport event is held in June of every year whose remainder when divided by 4 is 2.
Suppose that it is now January of the year Y. In what year will this sport event be held next time?

Constraints

  • 2000 \leq Y \leq 3000
  • Y is an integer.

Input

Input is given from Standard Input in the following format:

Y

Output

Print the answer.


Sample Input 1

2022

Sample Output 1

2022

The remainder when 2022 is divided by 4 is 2, so if it is now January of 2022, the next games will be held in June of the same year.


Sample Input 2

2023

Sample Output 2

2026

Sample Input 3

3000

Sample Output 3

3002
B - Triangle (Easier)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, \dots, N の番号が付けられており、i \, (1 \leq i \leq M) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。

以下の条件を全て満たす整数 a, b, c の組の総数を求めてください。

  • 1 \leq a \lt b \lt c \leq N
  • 頂点 a と頂点 b を結ぶ辺が存在する。
  • 頂点 b と頂点 c を結ぶ辺が存在する。
  • 頂点 c と頂点 a を結ぶ辺が存在する。

制約

  • 3 \leq N \leq 100
  • 1 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
  • (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
  • 入力は全て整数

入力

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

N M
U_1 V_1
\vdots
U_M V_M

出力

答えを出力せよ。


入力例 1

5 6
1 5
4 5
2 3
1 4
3 5
2 5

出力例 1

2

(a, b, c) = (1, 4, 5), (2, 3, 5) が条件を満たします。


入力例 2

3 1
1 2

出力例 2

0

入力例 3

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

出力例 3

4

Score : 200 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq M) edge connects Vertex U_i and Vertex V_i.

Find the number of tuples of integers a, b, c that satisfy all of the following conditions:

  • 1 \leq a \lt b \lt c \leq N
  • There is an edge connecting Vertex a and Vertex b.
  • There is an edge connecting Vertex b and Vertex c.
  • There is an edge connecting Vertex c and Vertex a.

Constraints

  • 3 \leq N \leq 100
  • 1 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
  • (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
U_1 V_1
\vdots
U_M V_M

Output

Print the answer.


Sample Input 1

5 6
1 5
4 5
2 3
1 4
3 5
2 5

Sample Output 1

2

(a, b, c) = (1, 4, 5), (2, 3, 5) satisfy the conditions.


Sample Input 2

3 1
1 2

Sample Output 2

0

Sample Input 3

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

Sample Output 3

4
C - Min Max Pair

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

1 以上 N 以下の整数からなる長さ N の数列 a = (a_1, \dots, a_N) が与えられます。

以下の条件を全て満たす整数 i, j の組の総数を求めてください。

  • 1 \leq i \lt j \leq N
  • \min(a_i, a_j) = i
  • \max(a_i, a_j) = j

制約

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq a_i \leq N \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

4
1 3 2 4

出力例 1

2

(i, j) = (1, 4), (2, 3) が条件を満たします。


入力例 2

10
5 8 2 2 1 6 7 2 9 10

出力例 2

8

Score : 300 points

Problem Statement

You are given a sequence a = (a_1, \dots, a_N) of length N consisting of integers between 1 and N.

Find the number of pairs of integers i, j that satisfy all of the following conditions:

  • 1 \leq i \lt j \leq N
  • \min(a_i, a_j) = i
  • \max(a_i, a_j) = j

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq a_i \leq N \, (1 \leq i \leq N)
  • 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

4
1 3 2 4

Sample Output 1

2

(i, j) = (1, 4), (2, 3) satisfy the conditions.


Sample Input 2

10
5 8 2 2 1 6 7 2 9 10

Sample Output 2

8
D - I Hate Non-integer Number

Time Limit: 2.5 sec / Memory Limit: 1024 MB

配点 : 400

問題文

項数が N の正整数列 A=(a_1,\ldots,a_N) が与えられます。
A の項を 1 個以上選ぶ方法は 2^N-1 通りありますが、そのうち選んだ項の平均値が整数であるものが何通りかを 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

3
2 6 2

出力例 1

6

A の項を選ぶ方法それぞれに対する平均値は以下のようになります。

  • a_1 のみを選んだ場合、平均値は \frac{a_1}{1}=\frac{2}{1} = 2 であり、整数である。

  • a_2 のみを選んだ場合、平均値は \frac{a_2}{1}=\frac{6}{1} = 6 であり、整数である。

  • a_3 のみを選んだ場合、平均値は \frac{a_3}{1}=\frac{2}{1} = 2 であり、整数である。

  • a_1a_2 を選んだ場合、平均値は \frac{a_1+a_2}{2}=\frac{2+6}{2} = 4 であり、整数である。

  • a_1a_3 を選んだ場合、平均値は \frac{a_1+a_3}{2}=\frac{2+2}{2} = 2 であり、整数である。

  • a_2a_3 を選んだ場合、平均値は \frac{a_2+a_3}{2}=\frac{6+2}{2} = 4 であり、整数である。

  • a_1a_2a_3 を選んだ場合、平均値は \frac{a_1+a_2+a_3}{3}=\frac{2+6+2}{3} = \frac{10}{3} であり、整数ではない。

以上より、6 通りの選び方が条件を満たします。


入力例 2

5
5 5 5 5 5

出力例 2

31

どのように A の項を 1 個以上選んでも平均値が 5 になります。

Score : 400 points

Problem Statement

You are given a sequence of positive integers A=(a_1,\ldots,a_N) of length N.
There are (2^N-1) ways to choose one or more terms of A. How many of them have an integer-valued average? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 10^9
  • 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

3
2 6 2

Sample Output 1

6

For each way to choose terms of A, the average is obtained as follows:

  • If just a_1 is chosen, the average is \frac{a_1}{1}=\frac{2}{1} = 2, which is an integer.

  • If just a_2 is chosen, the average is \frac{a_2}{1}=\frac{6}{1} = 6, which is an integer.

  • If just a_3 is chosen, the average is \frac{a_3}{1}=\frac{2}{1} = 2, which is an integer.

  • If a_1 and a_2 are chosen, the average is \frac{a_1+a_2}{2}=\frac{2+6}{2} = 4, which is an integer.

  • If a_1 and a_3 are chosen, the average is \frac{a_1+a_3}{2}=\frac{2+2}{2} = 2, which is an integer.

  • If a_2 and a_3 are chosen, the average is \frac{a_2+a_3}{2}=\frac{6+2}{2} = 4, which is an integer.

  • If a_1, a_2, and a_3 are chosen, the average is \frac{a_1+a_2+a_3}{3}=\frac{2+6+2}{3} = \frac{10}{3}, which is not an integer.

Therefore, 6 ways satisfy the condition.


Sample Input 2

5
5 5 5 5 5

Sample Output 2

31

Regardless of the choice of one or more terms of A, the average equals 5.

E - Red and Blue Graph

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の単純無向グラフが与えられます。頂点は 1, \dots, N と番号付けられ、i \, (1 \leq i \leq M) 番目の辺は頂点 U_i, V_i を結んでいます。

それぞれの頂点を赤または青で塗る方法は全部で 2^N 通りあります。これらのうち、以下の条件を全て満たすものの総数を 998244353 で割った余りを求めてください。

  • 赤く塗られた頂点がちょうど K 個ある
  • 異なる色で塗られた頂点を結ぶ辺の本数は偶数である

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
  • (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
  • 入力は全て整数

入力

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

N M K
U_1 V_1
\vdots
U_M V_M

出力

答えを出力せよ。


入力例 1

4 4 2
1 2
1 3
2 3
3 4

出力例 1

2

以下の 2 通りが条件を満たします。

  • 頂点 1, 2 を赤く、頂点 3, 4 を青く塗る。
  • 頂点 3, 4 を赤く、頂点 1, 2 を青く塗る。

上記の塗り方について、異なる色で塗られた頂点を結ぶ辺は 2 番目の辺と 3 番目の辺です。


入力例 2

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

出力例 2

64

Score : 500 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq M) edge connects Vertices U_i and V_i.

There are 2^N ways to paint each vertex red or blue. Find the number, modulo 998244353, of such ways that satisfy all of the following conditions:

  • There are exactly K vertices painted red.
  • There is an even number of edges connecting vertices painted in different colors.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
  • (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
U_1 V_1
\vdots
U_M V_M

Output

Print the answer.


Sample Input 1

4 4 2
1 2
1 3
2 3
3 4

Sample Output 1

2

The following two ways satisfy the conditions.

  • Paint Vertices 1 and 2 red and Vertices 3 and 4 blue.
  • Paint Vertices 3 and 4 red and Vertices 1 and 2 blue.

In either of the ways above, the 2-nd and 3-rd edges connect vertices painted in different colors.


Sample Input 2

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

Sample Output 2

64
F - Erase and Rotate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1,2,\ldots,N がちょうど 1 回ずつ現れる数列 P = (p_1,p_2,\ldots,p_N) が与えられます。
あなたは以下の操作のうち 1 つを選んで行うことを 0 回以上 K 回以下繰り返せます。

  • P の項を 1 つ選び、削除する。
  • P の末尾の項を先頭に移動させる。

操作後の P として考えられるもののうち辞書順で最小のものを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq N-1
  • 1 \leq p_i \leq N
  • (p_1,p_2,\ldots,p_N) には 1,2,\ldots,N がちょうど 1 回ずつ現れる。
  • 入力はすべて整数

入力

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

N K
p_1 p_2 \ldots p_N

出力

操作後の P として考えられるもののうち辞書順で最小のものを空白区切りで出力せよ。


入力例 1

5 3
4 5 2 3 1

出力例 1

1 2 3

以下のように操作をすると P(1,2,3) になります。

  • 先頭の項を削除する。これによって P(5,2,3,1) になる。
  • 末尾の項を先頭に移動させる。これによって P(1,5,2,3) になる。
  • 先頭から 2 番目の項を削除する。これによって P(1,2,3) になる。

また、辞書順で (1,2,3) より小さい数列は操作後の P として考えられません。よってこれが答えです。


入力例 2

3 0
3 2 1

出力例 2

3 2 1

操作を 1 回も行えない場合があります。


入力例 3

15 10
12 10 7 2 8 11 9 1 6 14 3 15 13 5 4

出力例 3

1 3 4 7 2 8 11 9

Score : 500 points

Problem Statement

You are given a sequence P = (p_1,p_2,\ldots,p_N) that contains 1,2,\ldots,N exactly once each.
You may perform the following operations between 0 and K times in total in any order:

  • Choose one term of P and remove it.
  • Move the last term of P to the head.

Find the lexicographically smallest P that can be obtained as a result of the operations.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq N-1
  • 1 \leq p_i \leq N
  • (p_1,p_2,\ldots,p_N) contains 1,2,\ldots,N exactly once each.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 p_2 \ldots p_N

Output

Print the lexicographically smallest P that can be obtained as a result of the operations, separated by spaces.


Sample Input 1

5 3
4 5 2 3 1

Sample Output 1

1 2 3

The following operations make P equal (1,2,3).

  • Removing the first term makes P equal (5,2,3,1).
  • Moving the last term to the head makes P equal (1,5,2,3).
  • Removing the second term makes P equal (1,2,3).

There is no way to obtain P lexicographically smaller than (1,2,3), so this is the answer.


Sample Input 2

3 0
3 2 1

Sample Output 2

3 2 1

You may be unable to perform operations.


Sample Input 3

15 10
12 10 7 2 8 11 9 1 6 14 3 15 13 5 4

Sample Output 3

1 3 4 7 2 8 11 9
G - LIS with Stack

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

空の列 X と空のスタック S があります。また、項数が N の整数列 A=(a_1,\ldots,a_N) が与えられます。
高橋君は i=1,\ldots,N の順に、以下の 2 つの操作のうち一方を行います。

  • A の先頭の整数 a_iS の先頭に移動させる。
  • A の先頭の整数 a_i を捨てる。

また、高橋君は S が空でない任意のタイミングで次の操作をすることが出来ます。

  • S の先頭の整数を X の末尾に移動させる。

最終的な X に対し、スコアを以下のように定めます。

  • X が広義単調増加な場合、すなわち X=(x_1,\ldots,x_{|X|}) とした時に任意の整数 i(1 \leq i \lt |X|) に対して x_i \leq x_{i+1} が成り立つ場合、スコアは |X| である。(|X|X の項数)
  • X が広義単調増加でない場合、スコアは 0 である。

スコアの最大値を求めてください。

制約

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

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

7
1 2 3 4 1 2 3

出力例 1

5

以下のように操作をすると最終的な X(1,1,2,3,4) となり、スコアが 5 になります。

  • a_1=1S の先頭に移動させる。
  • S の先頭の 1X の末尾に移動させる。
  • a_2=2S の先頭に移動させる。
  • a_3=3 を捨てる。
  • a_4=4S の先頭に移動させる。
  • a_5=1S の先頭に移動させる。
  • S の先頭の 1X の末尾に移動させる。
  • a_6=2S の先頭に移動させる。
  • S の先頭の 2X の末尾に移動させる。
  • a_7=3S の先頭に移動させる。
  • S の先頭の 3X の末尾に移動させる。
  • S の先頭の 4X の末尾に移動させる。

また、スコアを 6 以上にすることは出来ません。よってスコアの最大値は 5 です。


入力例 2

10
1 1 1 1 1 1 1 1 1 1

出力例 2

10

Score : 600 points

Problem Statement

There is an empty sequence X and an empty stack S. Also, you are given an integer sequence A=(a_1,\ldots,a_N) of length N.
For each i=1,\ldots,N in this order, Takahashi will do one of the following operations:

  • Move the integer a_i onto the top of S.
  • Discard the integer a_i from A.

Additionally, Takahashi may do the following operation whenever S is not empty:

  • Move the integer at the top of S to the tail of X.

The score of the final X is defined as follows.

  • If X is non-decreasing, i.e. if x_i \leq x_{i+1} holds for all integer i(1 \leq i \lt |X|), where X=(x_1,\ldots,x_{|X|}), then the score is |X| (where |X| denotes the number of terms in X).
  • If X is not non-decreasing, then the score is 0.

Find the maximum possible score.

Constraints

  • 1 \leq N \leq 50
  • 1 \leq a_i \leq 50
  • 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

7
1 2 3 4 1 2 3

Sample Output 1

5

The following operations make the final X equal (1,1,2,3,4), for a score of 5.

  • Move a_1=1 onto the top of S.
  • Move 1 at the top of S to the tail of X.
  • Move a_2=2 onto the top of S.
  • Discard a_3=3.
  • Move a_4=4 onto the top of S.
  • Move a_5=1 onto the top of S.
  • Move 1 at the top of S to the tail of X.
  • Move a_6=2 onto the top of S.
  • Move 2 at the top of S to the tail of X.
  • Move a_7=3 onto the top of S.
  • Move 3 at the top of S to the tail of X.
  • Move 4 at the top of S to the tail of X.

We cannot make the score 6 or greater, so the maximum possible score is 5.


Sample Input 2

10
1 1 1 1 1 1 1 1 1 1

Sample Output 2

10
Ex - Max Limited Sequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の整数列 A = (A_1, \dots, A_N) であって、以下の条件を全て満たすものの総数を 998244353 で割った余りを求めてください。

  • 1 \leq i \leq N を満たす全ての i について、0 \leq A_i \leq M
  • 1 \leq j \leq Q を満たす全ての j について、A_{L_j}, \dots, A_{R_j} の最大値は X_j である。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \lt 998244353
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N \, (1 \leq i \leq Q)
  • 1 \leq X_i \leq M \, (1 \leq i \leq Q)
  • 入力は全て整数

入力

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

N M Q
L_1 R_1 X_1
\vdots
L_Q R_Q X_Q

出力

答えを出力せよ。


入力例 1

3 3 2
1 2 2
2 3 3

出力例 1

5

A = (0, 2, 3), (1, 2, 3), (2, 0, 3), (2, 1, 3), (2, 2, 3) が条件を満たします。


入力例 2

1 1 1
1 1 1

出力例 2

1

入力例 3

6 40000000 3
1 4 30000000
2 6 20000000
3 5 10000000

出力例 3

135282163

Score : 600 points

Problem Statement

Find the number, modulo 998244353, of integer sequences A = (A_1, \dots, A_N) of length N that satisfy all of the following conditions:

  • 0 \leq A_i \leq M for all i such that 1 \leq i \leq N.
  • The maximum value of A_{L_j}, \dots, A_{R_j} is X_j for all j such that 1 \leq j \leq Q.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \lt 998244353
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N \, (1 \leq i \leq Q)
  • 1 \leq X_i \leq M \, (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
L_1 R_1 X_1
\vdots
L_Q R_Q X_Q

Output

Print the answer.


Sample Input 1

3 3 2
1 2 2
2 3 3

Sample Output 1

5

A = (0, 2, 3), (1, 2, 3), (2, 0, 3), (2, 1, 3), (2, 2, 3) satisfy the conditions.


Sample Input 2

1 1 1
1 1 1

Sample Output 2

1

Sample Input 3

6 40000000 3
1 4 30000000
2 6 20000000
3 5 10000000

Sample Output 3

135282163