A - Not coprime

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 個の 2 以上 50 以下の整数 X_1, X_2, \cdots, X_N が与えられます.全ての i = 1, 2, \cdots, N について次の条件を満たす正の整数 Y のうち,最小のものを求めてください.

  • X_iY は互いに素でない

制約

  • 1 \leq N \leq 49
  • 2 \leq X_i \leq 50
  • X_i \neq X_j (i \neq j)
  • 入力は全て整数

入力

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

N
X_1 X_2 \ldots X_N

出力

条件を満たす最小の正の整数を出力せよ.


入力例 1

2
4 3

出力例 1

6

4 と互いに素でないためには偶数である必要があり,3 と互いに素でないためには 3 の倍数である必要があります.


入力例 2

1
47

出力例 2

47

入力例 3

7
3 4 6 7 8 9 10

出力例 3

42

Score : 300 points

Problem Statement

Given are N integers between 2 and 50 (inclusive): X_1, X_2, \cdots, X_N. Find the minimum positive integer Y that satisfies the following for every i = 1, 2, \cdots, N:

  • X_i and Y are not coprime.

Constraints

  • 1 \leq N \leq 49
  • 2 \leq X_i \leq 50
  • X_i \neq X_j (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 \ldots X_N

Output

Print the minimum positive integer that satisfies the condition.


Sample Input 1

2
4 3

Sample Output 1

6

Being not coprime with 4 requires being even, and being not coprime with 3 requires being a multiple of 3.


Sample Input 2

1
47

Sample Output 2

47

Sample Input 3

7
3 4 6 7 8 9 10

Sample Output 3

42
B - Special Subsets

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1 以上 N 以下の整数すべてから成る集合を S とします.

fS から S への関数であり,f(1), f(2), \cdots, f(N) の値が f_1, f_2, \cdots, f_N として与えられます.

S の空でない部分集合 T であって,次の両方の条件を満たすものの個数を 998244353 で割った余りを求めてください.

  • 全ての a \in T について f(a) \in T である.

  • 全ての a, b \in T について a \neq b ならば f(a) \neq f(b) である.

制約

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

入力

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

N
f_1 f_2 \ldots f_N

出力

S の空でない部分集合であって,両方の条件を満たすものの個数を 998244353 で割った余りを出力せよ.


入力例 1

2
2 1

出力例 1

1

f(1) = 2, f(2) = 1 です.f(1) \neq f(2) であるため条件の 2 つ目は常に満たしますが,1 つ目の条件より 1, 2 は同時に T に入っている必要があります.


入力例 2

2
1 1

出力例 2

1

f(1) = f(2) = 1 です.1 つ目の条件のため 1T に属する必要があり,さらに 2 つ目の条件により 2T に属することはできません.


入力例 3

3
1 2 3

出力例 3

7

f(1) = 1, f(2) = 2, f(3) = 3 です.1 つ目の条件も 2 つ目の条件も常に満たされるため,S の空でない部分集合全てが条件を満たします.

Score : 400 points

Problem Statement

Let S be a set composed of all integers from 1 through N.

f is a function from S to S. You are given the values f(1), f(2), \cdots, f(N) as f_1, f_2, \cdots, f_N.

Find the number, modulo 998244353, of non-empty subsets T of S satisfying both of the following conditions:

  • For every a \in T, f(a) \in T.

  • For every a, b \in T, f(a) \neq f(b) if a \neq b.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq f_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
f_1 f_2 \ldots f_N

Output

Print the number of non-empty subsets of S satisfying both of the conditions, modulo 998244353.


Sample Input 1

2
2 1

Sample Output 1

1

We have f(1) = 2, f(2) = 1. Since f(1) \neq f(2), the second condition is always satisfied, but the first condition requires T to contain 1 and 2 simultaneously.


Sample Input 2

2
1 1

Sample Output 2

1

We have f(1) = f(2) = 1. The first condition requires T to contain 1, and the second condition forbids T to contain 2.


Sample Input 3

3
1 2 3

Sample Output 3

7

We have f(1) = 1, f(2) = 2, f(3) = 3. Both of the conditions are always satisfied, so all non-empty subsets of T count.

C - Sequence Scores

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 以上 M 以下の整数から成る長さ N の数列 A に対して, f(A) を以下のように定義します.

  • 長さ N の数列 X があり,初め全ての要素は 0 である.f(A) を,次の操作を繰り返して XA に等しくするための最小の操作回数とする.
    • 1 \leq l \leq r \leq N1 \leq v \leq M を指定する.l \leq i \leq r に対して X_i\max(X_i, v) で置き換える.

A として考えられる数列は M^N 通りあります.これら全ての数列に対する f(A) の和を 998244353 で割った余りを求めてください.

制約

  • 1 \leq N, M \leq 5000
  • 入力は全て整数

入力

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

N M

出力

全ての数列に対する f(A) の和を 998244353 で割った余りを出力せよ.


入力例 1

2 3

出力例 1

15

3 ^ 2 = 9 通りの数列と,それに対する f の値は以下の通りです.

  • A = (1, 1) のとき,(l = 1, r = 2, v = 1) として 1 回の操作で可能なので,f(A) = 1 です.
  • A = (1, 2) のとき,(l = 1, r = 2, v = 1) , (l = 2, r = 2, v = 2) として 2 回の操作で可能なので,f(A) = 2 です.
  • A = (1, 3) のとき,(l = 1, r = 2, v = 1) , (l = 2, r = 2, v = 3) として 2 回の操作で可能なので,f(A) = 2 です.
  • A = (2, 1) のとき,(l = 1, r = 2, v = 1) , (l = 1, r = 1, v = 2) として 2 回の操作で可能なので,f(A) = 2 です.
  • A = (2, 2) のとき,(l = 1, r = 2, v = 2) として 1 回の操作で可能なので,f(A) = 1 です.
  • A = (2, 3) のとき,(l = 1, r = 2, v = 2) , (l = 2, r = 2, v = 3) として 2 回の操作で可能なので,f(A) = 2 です.
  • A = (3, 1) のとき,(l = 1, r = 2, v = 1) , (l = 1, r = 1, v = 3) として 2 回の操作で可能なので,f(A) = 2 です.
  • A = (3, 2) のとき,(l = 1, r = 2, v = 2) , (l = 1, r = 1, v = 3) として 2 回の操作で可能なので,f(A) = 2 です.
  • A = (3, 3) のとき,(l = 1, r = 2, v = 3) として 1 回の操作で可能なので,f(A) = 1 です.

これらの和は 3 \times 1 + 6 \times 2 = 15 です.


入力例 2

3 2

出力例 2

15

入力例 3

34 56

出力例 3

649717324

Score : 600 points

Problem Statement

For a sequence A of length N consisting of integers between 1 and M (inclusive), let us define f(A) as follows:

  • We have a sequence X of length N, where every element is initially 0. f(A) is the minimum number of operations required to make X equal A by repeating the following operation:
    • Specify 1 \leq l \leq r \leq N and 1 \leq v \leq M, then replace X_i with \max(X_i, v) for each l \leq i \leq r.

Find the sum, modulo 998244353, of f(A) over all M^N sequences that can be A.

Constraints

  • 1 \leq N, M \leq 5000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the sum of f(A) over all sequences A, modulo 998244353.


Sample Input 1

2 3

Sample Output 1

15

The 3 ^ 2 = 9 sequences and the value of f for those are as follows:

  • For A = (1, 1), we can make X equal it with one operation with (l = 1, r = 2, v = 1), so f(A) = 1.
  • For A = (1, 2), we can make X equal it with two operations with (l = 1, r = 2, v = 1) and (l = 2, r = 2, v = 2), so f(A) = 2.
  • For A = (1, 3), we can make X equal it with two operations with (l = 1, r = 2, v = 1) and (l = 2, r = 2, v = 3), so f(A) = 2.
  • For A = (2, 1), we can make X equal it with two operations with (l = 1, r = 2, v = 1) and (l = 1, r = 1, v = 2), so f(A) = 2.
  • For A = (2, 2), we can make X equal it with one operation with (l = 1, r = 2, v = 2), so f(A) = 1.
  • For A = (2, 3), we can make X equal it with two operations with (l = 1, r = 2, v = 2) , (l = 2, r = 2, v = 3), so f(A) = 2.
  • For A = (3, 1), we can make X equal it with two operations with (l = 1, r = 2, v = 1) and (l = 1, r = 1, v = 3) so f(A) = 2.
  • For A = (3, 2), we can make X equal it with two operations with (l = 1, r = 2, v = 2) and (l = 1, r = 1, v = 3), so f(A) = 2.
  • For A = (3, 3), we can make X equal it with one operation with (l = 1, r = 2, v = 3), so f(A) = 1.

The sum of these values is 3 \times 1 + 6 \times 2 = 15.


Sample Input 2

3 2

Sample Output 2

15

Sample Input 3

34 56

Sample Output 3

649717324
D - Moving Pieces on Line

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

X = 10^{100} として,各整数 -X \leq i \leq X に対応する頂点があり,-X \leq i \leq X-1 について頂点 i, i + 1 を結ぶ無向辺 (以降,辺 \{ i, i + 1 \} と呼ぶ) があるグラフがあります.

このグラフの辺は初めすべて赤く塗られています.また,N 個 のコマがあり,i 個目のコマは頂点 a_i に置かれています.

maroon 君は次の操作を行うことができます.

  • コマを 1 つ選ぶ. このコマが頂点 i にあるとき,コマを頂点 i-1 または頂点 i+1 に動かし,通った辺を,現在の色が赤なら青,青なら赤に塗り替える.

操作の過程で,同じ頂点に複数のコマが存在しても構いません.

maroon 君はこれから上記の操作を 0 回以上繰り返して,辺の色の組合せを目的の状態にしたいと思っています.目的の状態は 偶数 K と,K 個の整数 t_1 < t_2 < \cdots < t_K で表され,i < t_1 について辺 \{ i, i + 1 \} は赤,t_1 \leq i < t_2 について辺 \{ i, i + 1 \} は青,\cdots, t_K \leq i について辺 \{ i, i + 1 \} は赤 という状態です.より正確には,各奇数 j = 1, 3, \cdots, K-1 に対して,t_j \leq i < t_{j+1} を満たす i について辺 \{ i, i + 1 \} は青で,それ以外の辺はすべて赤です.

maroon 君が辺の色の組合せを目的の状態にするために必要な操作回数の最小値を求めてください.また,そのような操作が不可能であるなら -1 を出力してください.

制約

  • 1 \leq N \leq 5000
  • 2 \leq K \leq 5000
  • K は偶数
  • |a_i| \leq 10^9
  • |t_i| \leq 10^9
  • t_i < t_{i+1}
  • 入力は全て整数

入力

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

N K
a_1 a_2 \cdots a_N
t_1 t_2 \cdots t_K

出力

maroon 君が辺の色の組合せを目的の状態にするために必要な操作回数の最小値を出力せよ.また,そのような操作が不可能であるなら -1 を出力せよ.


入力例 1

2 2
2 -1
-2 2

出力例 1

4

例えば以下のように 4 回の操作で目的の状態にでき,4 本の辺の色を変える必要があるのでこれが最適です.

これは初めの状態です.便宜上 -3 より左と 3 より右の辺は省いています.

0

-1 にあるコマを -2 に動かすと次の状態になります.

1

2 にあるコマを 1 に動かすと次の状態になります.

2

1 にあるコマを 0 に動かすと次の状態になります.

3

0 にあるコマを -1 に動かすと次の状態になり,これが目的の状態です.

last


入力例 2

2 2
2 2
5 8

出力例 2

9

初めから同じ頂点に複数のコマがある場合もあります.


入力例 3

3 4
1 3 5
0 2 4 6

出力例 3

-1

入力例 4

4 4
3 4 5 6
3 4 5 6

出力例 4

2

Score : 600 points

Problem Statement

Let X = 10^{100}. We have a graph with a vertex for each integer -X \leq i \leq X, and an undirected edge connecting Vertex i and Vertex i + 1 (we will call it Edge \{ i, i + 1 \}) for each -X \leq i \leq X-1.

All edges in this graph are initially painted red. Additionally, we have N pieces, and the i-th of them is on Vertex a_i.

Maroon can repeat the following operation:

  • Choose a piece. If that piece is on Vertex i, move it to Vertex i-1 or Vertex i + 1. Then, if the traversed edge is now painted red, repaint it blue, and vice versa.

During the process, there may be multiple pieces on the same vertex.

Maroon wants to do this operation zero or more times to make the edges painted in his desired combination of colors. The desired combination of colors is represented by an even number K and K integers t_1 < t_2 < \cdots < t_K: it means that Edge \{ i, i + 1 \} is red for i < t_1, blue for t_1 \leq i < t_2, \cdots, red for t_K \leq i. More formally, for each odd number j = 1, 3, \cdots, K-1, Edge \{ i, i + 1 \} is blue for each i such that t_j \leq i < t_{j+1}; all other edges are red.

Find the minimum number of operations required to make the edges painted in the desired combination of colors. If it is impossible to do so, print -1.

Constraints

  • 1 \leq N \leq 5000
  • 2 \leq K \leq 5000
  • K is even.
  • |a_i| \leq 10^9
  • |t_i| \leq 10^9
  • t_i < t_{i+1}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \cdots a_N
t_1 t_2 \cdots t_K

Output

Print the minimum number of operations required to make the edges painted in the desired combination of colors. If it is impossible to do so, print -1.


Sample Input 1

2 2
2 -1
-2 2

Sample Output 1

4

As shown below, we can achieve the desired state in four operations, which is optimal since we need to repaint four edges.

The following is the initial situation. For convenience, we have omitted edges to the left of -3 and the right of 3.

0

We move the piece on -1 to -2 to get the following:

1

Then, we move the piece on 2 to 1 to get the following:

2

Then, we move the piece on 1 to 0 to get the following:

3

Then, we move the piece on 0 to -1 to get the following, which is the desired state:

last


Sample Input 2

2 2
2 2
5 8

Sample Output 2

9

There can be multiple pieces on the same vertex already in the beginning.


Sample Input 3

3 4
1 3 5
0 2 4 6

Sample Output 3

-1

Sample Input 4

4 4
3 4 5 6
3 4 5 6

Sample Output 4

2
E - Paper Cutting 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

H \times W のマス目に区切られた長方形の紙があり,このうちちょうど 2 マスが黒く,残りの部分は白く塗られています.マス目の i 行目,j 列目にあるマスを (i, j) で表すと,黒く塗られているのはマス (h_1, w_1) とマス (h_2, w_2) です.

maroon 君はこれから以下の手順で紙を切断する操作を繰り返します.

  • 現在の紙のマス目が h \times w の時,紙の辺に平行でマスの境界を通るような直線には,(h - 1) 本の横線と (w - 1) 本の縦線がある.この中から 1 本を一様ランダムに選んで,その直線に沿って紙を 2 枚に切断する.このとき,
    • 2 つの黒いマスが同じ紙に存在するとき,もう片方の紙を捨て,操作を続ける
    • そうでなければ,操作を終了する

maroon 君が操作を終了するまでに紙を切断する回数の期待値を {\bmod} 998244353 で求めてください.

注記

求める期待値は必ず有理数になることが証明できます.またこの問題の制約のもとでは,その値を既約分数 \frac{P}{Q} で表した時,Q \not \equiv 0 \pmod{998244353} となることも証明できます.よって,R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります.この R を答えてください.

制約

  • 1 \leq H, W \leq 10^5
  • H \times W \geq 2
  • 1 \leq h_1, h_2 \leq H
  • 1 \leq w_1, w_2 \leq W
  • (h_1, w_1) \neq (h_2, w_2)
  • 入力は全て整数

入力

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

H W
h_1 w_1 h_2 w_2

出力

maroon 君が操作を終了するまでに紙を切断する回数の期待値を {\bmod} 998244353 で出力せよ.


入力例 1

2 3
2 2 1 1

出力例 1

332748119

まず,1 回目の切断で確率 2/3 で操作が終了します.残りの 1/3 については,次の切断で操作が終了します.

よって,紙を切断する回数の期待値は,1 \times 2/3 + 2 \times 1/3 = 4/3 です.


入力例 2

1 5
1 2 1 3

出力例 2

332748120

入力例 3

2 1
2 1 1 1

出力例 3

1

操作は 1 回の切断で必ず終了します.


入力例 4

10 10
3 4 5 6

出力例 4

831078040

Score : 700 points

Problem Statement

We have a rectangular piece of paper divided into H \times W squares, where two of those squares are painted black and the rest are painted white. If we let (i, j) denote the square at the i-th row and j-th column, the squares painted black are (h_1, w_1) and (h_2, w_2).

Maroon will repeat the following operation to cut the piece of paper:

  • Assume that we have h \times w squares remaining. There are (h - 1) horizontal lines and (w - 1) vertical lines that are parallel to the edges of the piece and pass the borders of the squares. He chooses one of these lines uniformly at random and cuts the piece into two along that line. Then,
    • if the two black squares are on the same piece, he throws away the other piece and continues the process;
    • otherwise, he ends the process.

Find the expected value of the number of times Maroon cuts a piece of paper until he ends the process, modulo 998244353.

Notes

We can prove that the expected value in question is always a rational number. Additionally, under the constraints of this problem, we can also prove that if we express that value as an irreducible fraction \frac{P}{Q}, we have Q \not \equiv 0 \pmod{998244353}. Thus, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Answer this R.

Constraints

  • 1 \leq H, W \leq 10^5
  • H \times W \geq 2
  • 1 \leq h_1, h_2 \leq H
  • 1 \leq w_1, w_2 \leq W
  • (h_1, w_1) \neq (h_2, w_2)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
h_1 w_1 h_2 w_2

Print

Find the expected value of the number of times Maroon cuts a piece of paper until he ends the process, modulo 998244353.


Sample Input 1

2 3
2 2 1 1

Sample Output 1

332748119

In the first cut, the process will end with probability 2/3. For the remaining case with probability 1/3, the process will end in the second cut.

Thus, the expected number of cuts is 1 \times 2/3 + 2 \times 1/3 = 4/3.


Sample Input 2

1 5
1 2 1 3

Sample Output 2

332748120

Sample Input 3

2 1
2 1 1 1

Sample Output 3

1

The process will always end in the first cut.


Sample Input 4

10 10
3 4 5 6

Sample Output 4

831078040
F - Permutation Division

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

1, 2, \cdots, N の順列 P が与えられます.

あなたは好きなように P をちょうど K 個の非空な連続部分列に分割することができます.

maroon 君はあなたの分割した連続部分列を並び替え,連結して,新しく順列 Q を作ります.maroon 君は Q を辞書順で最大にしようとします.

あなたは Q が辞書順で最小になるように P を連続部分列に分割したいです.そのときの Q を求めてください.

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq P_i \leq N
  • P_i \neq P_j (i \neq j)
  • 入力は全て整数

入力

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

N K
P_1 P_2 \cdots P_N

出力

あなたが P を最適に分割した場合の Q を出力せよ.


入力例 1

3 2
1 2 3

出力例 1

2 3 1

P の分割方法としては,(1, 2),(3) または (1), (2, 3) が考えられます.

前者であれば maroon 君は (3), (1, 2) と連続部分列を並び替えて Q = (3, 1, 2) を得ます.

後者であれば maroon 君は (2, 3), (1) と連続部分列を並び替えて Q = (2, 3, 1) を得ます.

よってあなたは後者のように分割するべきです.


入力例 2

4 3
4 3 1 2

出力例 2

4 3 1 2

入力例 3

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

出力例 3

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

Score : 900 points

Problem Statement

You are given a permutation P of 1, 2, \cdots, N.

You can divide P into exactly K non-empty contiguous subsequences as you like.

Maroon will rearrange those subsequences you make and concatenate them to make a new permutation Q. Here, he will lexicographically maximize Q.

You want to divide P in a way that lexicographically minimizes Q. Find Q in that case.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq P_i \leq N
  • 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:

N K
P_1 P_2 \cdots P_N

Output

Print Q when you optimally divide P.


Sample Input 1

3 2
1 2 3

Sample Output 1

2 3 1

You have two ways to divide P: (1, 2),(3) and (1), (2, 3).

In the former case, Maroon will rearrange them in the order (3), (1, 2) to get Q = (3, 1, 2).

In the latter case, Maroon will rearrange them in the order (2, 3), (1) to get Q = (2, 3, 1).

Thus, you should choose the latter.


Sample Input 2

4 3
4 3 1 2

Sample Output 2

4 3 1 2

Sample Input 3

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

Sample Output 3

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