A - 202<s>3</s>

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英小文字と数字からなる文字列 S が入力されます。
S2023 で終わることが保証されます。
S の最後の文字を 4 に変更し、変更後の文字列を出力してください。

制約

  • S は英小文字と数字からなる長さ 4 以上 100 以下の文字列
  • S2023 で終わる。

入力

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

S

出力

答えを出力せよ。


入力例 1

hello2023

出力例 1

hello2024

hello2023 の最後の文字を 4 に変更すると、 hello2024 となります。


入力例 2

worldtourfinals2023

出力例 2

worldtourfinals2024

入力例 3

2023

出力例 3

2024

S2023 で終わることが保証されていますが、 S2023 そのものである場合もあります。


入力例 4

20232023

出力例 4

20232024

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English letters and digits.
S is guaranteed to end with 2023.
Change the last character of S to 4 and print the modified string.

Constraints

  • S is a string of length between 4 and 100, inclusive, consisting of lowercase English letters and digits.
  • S ends with 2023.

Input

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

S

Output

Print the answer.


Sample Input 1

hello2023

Sample Output 1

hello2024

Changing the last character of hello2023 to 4 yields hello2024.


Sample Input 2

worldtourfinals2023

Sample Output 2

worldtourfinals2024

Sample Input 3

2023

Sample Output 3

2024

S is guaranteed to end with 2023, possibly being 2023 itself.


Sample Input 4

20232023

Sample Output 4

20232024
B - Tetrahedral Number

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 150

問題文

整数 N が与えられます。

非負整数の組 (x,y,z) であって x+y+z\leq N を満たすものを辞書順で小さい方から順に全て出力してください。

非負整数の組の辞書順とは?

非負整数の組 (x,y,z)(x',y',z') より辞書順で小さいとは、下記のいずれかが成り立つことを言います。

  • x < x' である
  • x=x' かつ y< y' である
  • x=x' かつ y=y' かつ z< z' である

制約

  • 0 \leq N \leq 21
  • N は整数である

入力

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

N

出力

非負整数の組 (x,y,z) であって x+y+z\leq N を満たすものを、1 行に 1 組ずつ x,y,z を空白区切りで、辞書順で小さい方から順に全て出力せよ。


入力例 1

3

出力例 1

0 0 0
0 0 1
0 0 2
0 0 3
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 3 0
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 2 0
2 0 0
2 0 1
2 1 0
3 0 0

入力例 2

4

出力例 2

0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
0 1 0
0 1 1
0 1 2
0 1 3
0 2 0
0 2 1
0 2 2
0 3 0
0 3 1
0 4 0
1 0 0
1 0 1
1 0 2
1 0 3
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 3 0
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 2 0
3 0 0
3 0 1
3 1 0
4 0 0

Score : 150 points

Problem Statement

You are given an integer N.

Print all triples of non-negative integers (x,y,z) such that x+y+z\leq N in ascending lexicographical order.

What is lexicographical order for non-negative integer triples?

A triple of non-negative integers (x,y,z) is said to be lexicographically smaller than (x',y',z') if and only if one of the following holds:

  • x < x';
  • x=x' and y< y';
  • x=x' and y=y' and z< z'.

Constraints

  • 0 \leq N \leq 21
  • N is an integer.

Input

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

N

Output

Print all triples of non-negative integers (x,y,z) such that x+y+z\leq N in ascending lexicographical order, with x,y,z separated by spaces, one triple per line.


Sample Input 1

3

Sample Output 1

0 0 0
0 0 1
0 0 2
0 0 3
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 3 0
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 2 0
2 0 0
2 0 1
2 1 0
3 0 0

Sample Input 2

4

Sample Output 2

0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
0 1 0
0 1 1
0 1 2
0 1 3
0 2 0
0 2 1
0 2 2
0 3 0
0 3 1
0 4 0
1 0 0
1 0 1
1 0 2
1 0 3
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 3 0
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 2 0
3 0 0
3 0 1
3 1 0
4 0 0
C - Loong Tracking

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

高橋君は座標平面上で龍を操作するゲームを作成しました。

龍は 1 から N までの番号がついた N 個のパーツからなり、パーツ 1 と呼びます。

最初パーツ i は座標 (i,0) にあります。以下のクエリを Q 個処理してください。

  • 1 C: 頭を方向 C1 移動させる。ここで、CR, L, U, D のいずれかであり、それぞれ x 軸正方向、x 軸負方向、y 軸正方向、y 軸負方向を意味する。頭以外の全てのパーツは前のパーツに追従するように動く。すなわち、パーツ i\ \ (2\leq i \leq N) は移動前にパーツ i-1 があった座標に移動する。
  • 2 p: パーツ p のある座標を求める。

制約

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 2\times 10^5
  • 1 種類目のクエリにおいて、CR, L, U, D のいずれか
  • 2 種類目のクエリにおいて、1\leq p \leq N
  • 入力に含まれる数値は全て整数

入力

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

N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

各クエリは以下の 2 種類のいずれかの形式である。

1 C
2 p

出力

2 種類目のクエリの回数を q として q 行出力せよ。
i 行目には、i 番目のそのようなクエリに対する答えの座標を (x,y) としたとき、x,y を空白区切りで出力せよ。


入力例 1

5 9
2 3
1 U
2 3
1 R
1 D
2 3
1 L
2 1
2 5

出力例 1

3 0
2 0
1 1
1 0
1 0

2 種類目のクエリを処理する各タイミングにおいて、パーツの位置は次のようになっています。

図

複数のパーツが同じ座標に存在しうることに注意してください。

Score : 300 points

Problem Statement

Takahashi has created a game where the player controls a dragon on a coordinate plane.

The dragon consists of N parts numbered 1 to N, with part 1 being called the head.

Initially, part i is located at the coordinates (i,0). Process Q queries as follows.

  • 1 C: Move the head by 1 in direction C. Here, C is one of R, L, U, and D, which represent the positive x-direction, negative x-direction, positive y-direction, and negative y-direction, respectively. Each part other than the head moves to follow the part in front of it. That is, part i (2\leq i \leq N) moves to the coordinates where part i-1 was before the move.
  • 2 p: Find the coordinates of part p.

Constraints

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 2\times 10^5
  • For the first type of query, C is one of R, L, U, and D.
  • For the second type of query, 1\leq p \leq N.
  • All numerical input values are integers.

Input

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

N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Each query is in one of the following two formats:

1 C
2 p

Output

Print q lines, where q is the number of queries of the second type.
The i-th line should contain x and y separated by a space, where (x,y) are the answer to the i-th such query.


Sample Input 1

5 9
2 3
1 U
2 3
1 R
1 D
2 3
1 L
2 1
2 5

Sample Output 1

3 0
2 0
1 1
1 0
1 0

At each time when processing the second type of query, the parts are at the following positions:

Figure

Note that multiple parts may exist at the same coordinates.

D - Loong and Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 350

問題文

N 行 横 N 列のマスからなるグリッドがあります。ここで、N45 以下の奇数です。

上から i 行目、左から j 列目のマスをマス (i,j) と表します。

このグリッドに、以下の条件を満たすように高橋君と 1 から N^2-1 までの番号がついた N^2-1 個のパーツからなる 1 匹の龍を配置します。

  • 高橋君はグリッドの中央、すなわちマス (\frac{N+1}{2},\frac{N+1}{2}) に配置しなければならない。
  • 高橋君がいるマスを除く各マスには龍のパーツをちょうど 1 つ配置しなければならない。
  • 2 \leq x \leq N^2-1 を満たす全ての整数 x について、龍のパーツ x はパーツ x-1 があるマスと辺で隣接するマスに配置しなければならない。
    • マス (i,j) とマス (k,l) が辺で隣接するとは、|i-k|+|j-l|=1 であることを意味します。

条件を満たす配置方法を 1 つ出力してください。なお、条件を満たす配置は必ず存在します。

制約

  • 3 \leq N \leq 45
  • N は奇数である

入力

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

N

出力

N 行出力せよ。
X_{i,j} を、マス (i,j) に高橋君を配置するとき T、パーツ x を配置するとき x とし、i 行目には X_{i,1},\ldots,X_{i,N} を空白区切りで出力せよ。


入力例 1

5

出力例 1

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

この他、以下の出力も条件をすべて満たすため正解となります。

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

一方、以下の出力はそれぞれ不正解となります。

高橋君が中央にいない。

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

パーツ 23 とパーツ 24 のあるマスが辺で隣接していない。

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

Score : 350 points

Problem Statement

There is a grid with N rows and N columns, where N is an odd number at most 45.

Let (i,j) denote the cell at the i-th row from the top and j-th column from the left.

In this grid, you will place Takahashi and a dragon consisting of N^2-1 parts numbered 1 to N^2-1 in such a way that satisfies the following conditions:

  • Takahashi must be placed at the center of the grid, that is, in cell (\frac{N+1}{2},\frac{N+1}{2}).
  • Except for the cell where Takahashi is, exactly one dragon part must be placed in each cell.
  • For every integer x satisfying 2 \leq x \leq N^2-1, the dragon part x must be placed in a cell adjacent by an edge to the cell containing part x-1.
    • Cells (i,j) and (k,l) are said to be adjacent by an edge if and only if |i-k|+|j-l|=1.

Print one way to arrange the parts to satisfy the conditions. It is guaranteed that there is at least one arrangement that satisfies the conditions.

Constraints

  • 3 \leq N \leq 45
  • N is odd.

Input

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

N

Output

Print N lines.
The i-th line should contain X_{i,1},\ldots,X_{i,N} separated by spaces, where X_{i,j} is T when placing Takahashi in cell (i,j) and x when placing part x there.


Sample Input 1

5

Sample Output 1

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

The following output also satisfies all the conditions and is correct.

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

On the other hand, the following outputs are incorrect for the reasons given.

Takahashi is not at the center.

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

The cells containing parts 23 and 24 are not adjacent by an edge.

1 2 3 4 5
10 9 8 7 6
11 12 24 22 23
14 13 T 21 20
15 16 17 18 19
E - Non-Decreasing Colorful Path

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 525

問題文

N 頂点 M 辺の連結な無向グラフがあり、 i 番目の辺は頂点 U_i と頂点 V_i を双方向に結びます。
また、全ての頂点に整数が書いてあり、頂点 v には整数 A_v が書かれています。

頂点 1 から頂点 N への単純なパス ( 同じ頂点を複数回通らないパス ) に対して、以下のように得点を定めます。

  • パス上の頂点に書かれた整数を通った順に並べた数列 を S とする。
  • S が広義単調増加になっていない場合、そのパスの得点は 0 である。
  • そうでない場合、 S に含まれる整数の種類数が得点となる。

頂点 1 から頂点 N への全ての単純なパスのうち、最も得点が高いものを求めてその得点を出力してください。

S が広義単調増加であるとは? 長さ l の数列 S=(S_1,S_2,\dots,S_l) が広義単調増加であるとは、 全ての整数 1 \le i < l について S_i \le S_{i+1} を満たすことを言います。

制約

  • 入力は全て整数
  • 2 \le N \le 2 \times 10^5
  • N-1 \le M \le 2 \times 10^5
  • 1 \le A_i \le 2 \times 10^5
  • グラフは連結である
  • 1 \le U_i < V_i \le N
  • i \neq j なら (U_i,V_i) \neq (U_j,V_j)

入力

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

N M
A_1 A_2 \dots A_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

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


入力例 1

5 6
10 20 30 40 50
1 2
1 3
2 5
3 4
3 5
4 5

出力例 1

4

1 \rightarrow 3 \rightarrow 4 \rightarrow 5 というパスについて S=(10,30,40,50) となり、このパスの得点は 4 で、これが最大です。


入力例 2

4 5
1 10 11 4
1 2
1 3
2 3
2 4
3 4

出力例 2

0

頂点 1 から頂点 N への単純パスであって、 S が広義単調増加となるものはありません。この場合、最大の得点は 0 です。


入力例 3

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

出力例 3

5

Score : 525 points

Problem Statement

There is a connected undirected graph with N vertices and M edges, where the i-th edge connects vertex U_i and vertex V_i bidirectionally.
Each vertex has an integer written on it, with integer A_v written on vertex v.

For a simple path from vertex 1 to vertex N (a path that does not pass through the same vertex multiple times), the score is determined as follows:

  • Let S be the sequence of integers written on the vertices along the path, listed in the order they are visited.
  • If S is not non-decreasing, the score of that path is 0.
  • Otherwise, the score is the number of distinct integers in S.

Find the path from vertex 1 to vertex N with the highest score among all simple paths and print that score.

What does it mean for S to be non-decreasing? A sequence S=(S_1,S_2,\dots,S_l) of length l is said to be non-decreasing if and only if S_i \le S_{i+1} for all integers 1 \le i < l.

Constraints

  • All input values are integers.
  • 2 \le N \le 2 \times 10^5
  • N-1 \le M \le 2 \times 10^5
  • 1 \le A_i \le 2 \times 10^5
  • The graph is connected.
  • 1 \le U_i < V_i \le N
  • (U_i,V_i) \neq (U_j,V_j) if i \neq j.

Input

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

N M
A_1 A_2 \dots A_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print the answer as an integer.


Sample Input 1

5 6
10 20 30 40 50
1 2
1 3
2 5
3 4
3 5
4 5

Sample Output 1

4

The path 1 \rightarrow 3 \rightarrow 4 \rightarrow 5 has S=(10,30,40,50) for a score of 4, which is the maximum.


Sample Input 2

4 5
1 10 11 4
1 2
1 3
2 3
2 4
3 4

Sample Output 2

0

There is no simple path from vertex 1 to vertex N such that S is non-decreasing. In this case, the maximum score is 0.


Sample Input 3

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

Sample Output 3

5
F - Hop Sugoroku

Time Limit: 2.5 sec / Memory Limit: 1024 MB

配点 : 525

問題文

一列に並んだ N 個のマス 1,2,\dots,N と長さ N の数列 A=(A_1,A_2,\dots,A_N) があります。
最初、マス 1 は黒く、他の N-1 個のマスは白く塗られており、 1 つのコマがマス 1 に置かれています。

以下の操作を 0 回以上好きな回数繰り返します。

  • コマがマス i にあるとき、ある正整数 x を決めてコマをマス i + A_i \times x に移動させる。
    • 但し、 i + A_i \times x > N となるような移動はできません。
  • その後、マス i + A_i \times x を黒く塗る。

操作を終えた時点で黒く塗られたマスの集合として考えられるものの数を 998244353 で割った余りを求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 2 \times 10^5

入力

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

N
A_1 A_2 \dots A_N

出力

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


入力例 1

5
1 2 3 1 1

出力例 1

8

黒く塗られたマスの集合として考えられるものは以下の 8 通りです。

  • マス 1
  • マス 1,2
  • マス 1,2,4
  • マス 1,2,4,5
  • マス 1,3
  • マス 1,4
  • マス 1,4,5
  • マス 1,5

入力例 2

1
200000

出力例 2

1

入力例 3

40
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

出力例 3

721419738

998244353 で割った余りを求めることに注意してください。

Score : 525 points

Problem Statement

There is a row of N squares labeled 1,2,\dots,N and a sequence A=(A_1,A_2,\dots,A_N) of length N.
Initially, square 1 is painted black, the other N-1 squares are painted white, and a piece is placed on square 1.

You may repeat the following operation any number of times, possibly zero:

  • When the piece is on square i, choose a positive integer x and move the piece to square i + A_i \times x.
    • Here, you cannot make a move with i + A_i \times x > N.
  • Then, paint the square i + A_i \times x black.

Find the number of possible sets of squares that can be painted black at the end of the operations, modulo 998244353.

Constraints

  • All input values are integers.
  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 2 \times 10^5

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5
1 2 3 1 1

Sample Output 1

8

There are eight possible sets of squares painted black:

  • Square 1
  • Squares 1,2
  • Squares 1,2,4
  • Squares 1,2,4,5
  • Squares 1,3
  • Squares 1,4
  • Squares 1,4,5
  • Squares 1,5

Sample Input 2

1
200000

Sample Output 2

1

Sample Input 3

40
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 3

721419738

Be sure to find the number modulo 998244353.

G - Discrete Logarithm Problems

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 個の整数 A_1,\ldots,A_N と素数 P が与えられます。 次の条件をともに満たす整数の組 (i,j) の個数を求めてください。

  • 1 \leq i,j \leq N
  • ある正整数 k が存在し、A_i^k \equiv A_j \bmod P

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i < P
  • 2 \leq P \leq 10^{13}
  • P は素数
  • 入力は全て整数

入力

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

N P
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3 13
2 3 5

出力例 1

5

(1,1),(1,2),(1,3),(2,2),(3,3)5 組が条件を満たします。

例えば (1,3) については、k=9 とすると A_1^9 = 512 \equiv 5 = A_3 \bmod 13 となります。


入力例 2

5 2
1 1 1 1 1

出力例 2

25

入力例 3

10 9999999999971
141592653589 793238462643 383279502884 197169399375 105820974944 592307816406 286208998628 34825342117 67982148086 513282306647

出力例 3

63

Score : 600 points

Problem Statement

You are given N integers A_1,\ldots,A_N and a prime number P. Find the number of pairs of integers (i,j) that satisfy both of the following conditions:

  • 1 \leq i,j \leq N;
  • There is a positive integer k such that A_i^k \equiv A_j \mod P.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i < P
  • 2 \leq P \leq 10^{13}
  • P is prime.
  • All input values are integers.

Input

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

N P
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

3 13
2 3 5

Sample Output 1

5

Five pairs satisfy the conditions: (1,1),(1,2),(1,3),(2,2),(3,3).

For example, for the pair (1,3), if we take k=9, then A_1^9 = 512 \equiv 5 = A_3 \mod 13.


Sample Input 2

5 2
1 1 1 1 1

Sample Output 2

25

Sample Input 3

10 9999999999971
141592653589 793238462643 383279502884 197169399375 105820974944 592307816406 286208998628 34825342117 67982148086 513282306647

Sample Output 3

63