A - Reverse and Count

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

(1, 2, \dots, N) の順列 A = (A_1, A_2, \dots, A_N) が与えられます。
1 \leq L \leq R \leq N を満たす整数の組 (L, R) に対して、AL 番目から R 番目までの要素を反転してできる順列を f(L, R) とします。
ここで、「AL 番目から R 番目までの要素を反転する」とは、A_L, A_{L+1}, \dots, A_{R-1}, A_RA_R, A_{R-1}, \dots, A_{L+1}, A_{L} に同時に置き換えることを言います。

(L, R)1 \leq L \leq R \leq N を満たすように選ぶ方法は \frac{N(N + 1)}{2} 通りあります。
このような (L, R) の組全てに対して順列 f(L, R) をすべて列挙して辞書順にソートしたときに、先頭から K 番目にある順列を求めてください。

数列の辞書順とは?

数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。

  1. |S| \lt |T| かつ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})
  2. ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
    • S_iT_i より(数として)小さい。

制約

  • 1 \leq N \leq 7000
  • 1 \leq K \leq \frac{N(N + 1)}{2}
  • A(1, 2, \dots, N) の順列

入力

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

N K
A_1 A_2 \dots A_N

出力

順列 f(L, R) を列挙して辞書順にソートしたときに、先頭から K 番目にある順列を B =(B_1, B_2, \dots, B_N) とする。
このとき以下の形式で B を出力せよ。

B_1 B_2 \dots B_N

入力例 1

3 5
1 3 2

出力例 1

2 3 1

1 \leq L \leq R \leq N を満たす (L, R) の組全てに対して順列 f(L, R) をすべて列挙すると次のようになります。

  • f(1, 1) = (1, 3, 2)
  • f(1, 2) = (3, 1, 2)
  • f(1, 3) = (2, 3, 1)
  • f(2, 2) = (1, 3, 2)
  • f(2, 3) = (1, 2, 3)
  • f(3, 3) = (1, 3, 2)

これらを辞書順にソートしたときに 5 番目に来る順列は f(1, 3) = (2, 3, 1) です。よってこれを出力します。


入力例 2

5 15
1 2 3 4 5

出力例 2

5 4 3 2 1

答えは f(1, 5) です。


入力例 3

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

出力例 3

9 2 1 6 5 4 10 7 8 3

Score : 400 points

Problem Statement

You are given a permutation A = (A_1, A_2, \dots, A_N) of (1, 2, \dots, N).
For a pair of integers (L, R) such that 1 \leq L \leq R \leq N, let f(L, R) be the permutation obtained by reversing the L-th through R-th elements of A, that is, replacing A_L, A_{L+1}, \dots, A_{R-1}, A_R with A_R, A_{R-1}, \dots, A_{L+1}, A_{L} simultaneously.

There are \frac{N(N + 1)}{2} ways to choose (L, R) such that 1 \leq L \leq R \leq N.
If the permutations f(L, R) for all such pairs (L, R) are listed and sorted in lexicographical order, what is the K-th permutation from the front?

What is lexicographical order on sequences?

A sequence S = (S_1,S_2,\ldots,S_{|S|}) is said to be lexicographically smaller than a sequence T = (T_1,T_2,\ldots,T_{|T|}) if and only if 1. or 2. below holds. Here, |S| and |T| denote the lengths of S and T, respectively.

  1. |S| \lt |T| and (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
  2. There is an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace that satisfies both of the following.
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1}).
    • S_i is smaller than T_i (as a number).

Constraints

  • 1 \leq N \leq 7000
  • 1 \leq K \leq \frac{N(N + 1)}{2}
  • A is a permutation of (1, 2, \dots, N).

Input

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

N K
A_1 A_2 \dots A_N

Output

Let B =(B_1, B_2, \dots, B_N) be the K-th permutation from the front in the list of the permutations f(L, R) sorted in lexicographical order.
Print B in the following format:

B_1 B_2 \dots B_N

Sample Input 1

3 5
1 3 2

Sample Output 1

2 3 1

Here are the permutations f(L, R) for all pairs (L, R) such that 1 \leq L \leq R \leq N.

  • f(1, 1) = (1, 3, 2)
  • f(1, 2) = (3, 1, 2)
  • f(1, 3) = (2, 3, 1)
  • f(2, 2) = (1, 3, 2)
  • f(2, 3) = (1, 2, 3)
  • f(3, 3) = (1, 3, 2)

When these are sorted in lexicographical order, the fifth permutation is f(1, 3) = (2, 3, 1), which should be printed.


Sample Input 2

5 15
1 2 3 4 5

Sample Output 2

5 4 3 2 1

The answer is f(1, 5).


Sample Input 3

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

Sample Output 3

9 2 1 6 5 4 10 7 8 3
B - Triple Pair

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

正整数 N が与えられます。

以下の条件を満たす 3 個の正整数の組 (x,y,z) の個数を 998244353 で割ったあまりを求めてください。

  • xy,yz,zx が全て N 以下である。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 100
  • 1 \le N \le 10^9

入力

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

ここで、\mathrm{case}_i とは、i 番目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N

出力

T 行出力せよ。i(1 \le i \le T) 行目には、i 番目のテストケースに対する答えを出力せよ。


入力例 1

4
1
2
5
998244353

出力例 1

1
4
17
727512986

1 個目のテストケースでは、N=1 です。条件を満たす (x,y,z)(1,1,1)1 個です。

2 個目のテストケースでは、N=2 です。条件を満たす (x,y,z) は、(1,1,1),(2,1,1),(1,2,1),(1,1,2)4 個です。

Score : 500 points

Problem Statement

You are given a positive integer N.

Find the number, modulo 998244353, of triples of positive integers (x,y,z) that satisfy the following condition.

  • All of xy, yz, zx are less than or equal to N.

You have T test cases to solve.

Constraints

  • 1 \le T \le 100
  • 1 \le N \le 10^9

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i represents the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is in the following format:

N

Output

Print T lines. The i-th line (1 \le i \le T) should contain the answer for the i-th test case.


Sample Input 1

4
1
2
5
998244353

Sample Output 1

1
4
17
727512986

In the first test case, N=1. There is one triple (x,y,z) that satisfies the condition: (1,1,1).

In the second test case, N=2. There are four triples (x,y,z) that satisfy the condition: (1,1,1),(2,1,1),(1,2,1),(1,1,2).

C - Power Up

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

正整数からなる N 要素の多重集合 A=\lbrace A_1,A_2,\dots,A_N \rbrace が与えられます。

あなたは、以下の操作を好きな回数 ( 0 回でもよい) 繰り返すことが出来ます。

  • A2 個以上含まれる正整数 x を選ぶ。A から x2 個削除し、Ax+11 個加える。

最終的な A としてあり得るものの個数を 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

4
1 1 2 4

出力例 1

3

最終的な A としてあり得るものは、\lbrace 1,1,2,4 \rbrace,\lbrace 2,2,4 \rbrace,\lbrace 3,4 \rbrace3 個があります。

\lbrace 3,4 \rbrace は以下のようにして作ることが出来ます。

  • x として 1 を選ぶ。A から 12 個削除し、21 個加える。A=\lbrace 2,2,4 \rbrace となる。
  • x として 2 を選ぶ。A から 22 個削除し、31 個加える。A=\lbrace 3,4 \rbrace となる。

入力例 2

5
1 2 3 4 5

出力例 2

1

入力例 3

13
3 1 4 1 5 9 2 6 5 3 5 8 9

出力例 3

66

Score : 500 points

Problem Statement

You are given a multiset of positive integers with N elements: A=\lbrace A_1,A_2,\dots,A_N \rbrace.

You may repeat the following operation any number of times (possibly zero).

  • Choose a positive integer x that occurs at least twice in A. Delete two occurrences of x from A, and add one occurrence of x+1 to A.

Find the number, modulo 998244353, of multisets that A can be in the end.

Constraints

  • 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.


Sample Input 1

4
1 1 2 4

Sample Output 1

3

A can be one of the three multisets \lbrace 1,1,2,4 \rbrace,\lbrace 2,2,4 \rbrace,\lbrace 3,4 \rbrace in the end.

You can make A = \lbrace 3,4 \rbrace as follows.

  • Choose x = 1. Delete two 1s from A and add one 2 to A, making A=\lbrace 2,2,4 \rbrace.
  • Choose x = 2. Delete two 2s from A and add one 3 to A, making A=\lbrace 3,4 \rbrace.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1

Sample Input 3

13
3 1 4 1 5 9 2 6 5 3 5 8 9

Sample Output 3

66
D - Mahjong

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 700

問題文

長さ N かつ総和 M である非負整数列 A=(A_1,A_2,\dots,A_N) のうち、以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。

  • 以下の操作のうちどちらかを選んで行うことを繰り返して、A の全ての要素を 0 にすることが出来る。
    • 1 \le i \le N を満たす整数 i を選び、A_iK 減らす。
    • 1 \le i \le N-K+1 を満たす整数 i を選び、A_i,A_{i+1},\dots,A_{i+K-1}1 ずつ減らす。

制約

  • 1 \le K \le N \le 2000
  • 1 \le M \le 10^{18}

入力

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

N M K

出力

答えを出力せよ。


入力例 1

3 2 2

出力例 1

5

条件を満たす数列は、以下の 5 個です。

  • (1,1,0)
  • (0,1,1)
  • (2,0,0)
  • (0,2,0)
  • (0,0,2)

例えば、A=(0,1,1) の場合は以下のように操作をすることで全ての要素を 0 にすることが出来ます。

  • 2 個目の操作を行う。i として 2 を選ぶ。A_2,A_31 ずつ減らす。A=(0,0,0) となる。

入力例 2

100 998244353 100

出力例 2

0

条件を満たす数列が存在しない場合もあります。


入力例 3

2000 545782618661124208 533

出力例 3

908877889

Score : 700 points

Problem Statement

Find the number, modulo 998244353, of sequences of N non-negative integers A=(A_1,A_2,\dots,A_N) totaling M that satisfy the following condition.

  • It is possible to make all elements of A equal 0 by repeatedly choosing one of the following operations and performing it.
    • Choose an integer i such that 1 \le i \le N and decrease A_i by K.
    • Choose an integer i such that 1 \le i \le N-K+1 and decrease each of A_i,A_{i+1},\dots,A_{i+K-1} by 1.

Constraints

  • 1 \le K \le N \le 2000
  • 1 \le M \le 10^{18}

Input

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

N M K

Output

Print the answer.


Sample Input 1

3 2 2

Sample Output 1

5

The following five sequences satisfy the requirements.

  • (1,1,0)
  • (0,1,1)
  • (2,0,0)
  • (0,2,0)
  • (0,0,2)

For instance, if A=(0,1,1), you can do the following to make all elements of A equal 0.

  • Perform the second operation. Choose i = 2 to decrease each of A_2 and A_3 by 1, making A=(0,0,0).

Sample Input 2

100 998244353 100

Sample Output 2

0

There may be no sequence that satisfies the requirements.


Sample Input 3

2000 545782618661124208 533

Sample Output 3

908877889
E - Make Biconnected

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 800

問題文

N 頂点の無向木 G が与えられます。G の全ての頂点の次数は 3 以下です。
頂点には 1 から N の番号がついています。辺には 1 から N-1 までの番号がついていて、辺 i は頂点 u_i と頂点 v_i を結んでいます。
また、全ての頂点には重みが設定されていて、頂点 i の重みは W_i です。

あなたは G0 本以上の辺を追加します。頂点 i と頂点 j の間に辺を追加すると W_i + W_j のコストがかかります。

次の条件を満たすように辺を追加する方法のうち、コストの総和が最小である方法を 1 つ出力してください。

  • G は二重頂点連結である。つまり、G 内の任意の頂点 v について、G から頂点 v および v に隣接する辺を取り除いても G は連結な状態を保っている。

T 個のテストケースが与えられるので、それぞれについて答えてください。

制約

  • 1 \leq T \leq 2 \times 10^5
  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • 入力で与えられるグラフは木
  • 入力で与えられるグラフにおいて、全ての頂点は次数が 3 以下
  • 1 \leq W_i \leq 10^9
  • W_i は整数
  • 全てのテストケースにおける N の総和は 2 \times 10^5 以下

入力

入力は以下の形式で標準入力から与えられる。ここで、\mathrm{case}_ii 番目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_N

各テストケースは以下の形式で与えられる。

N 
W_1 W_2 \dots W_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

各テストケースについて、以下の形式で答えを出力せよ。ここで、

  • 追加する辺の本数は M 本で、
  • i 本目の追加する辺は頂点 a_i と頂点 b_i を結んでいる

とする。

答えが複数ある場合は、どれを出力しても正答とみなされる。

M 
a_1 b_1
a_2 b_2
\vdots
a_M b_M

入力例 1

2
3
2 3 5
1 2
2 3
7
1 10 100 1000 10000 100000 1000000
1 2
2 3
2 4
3 5
3 6
4 7

出力例 1

1
1 3
2
7 6
1 5

1 番目のテストケースでは、頂点 1 と頂点 3 を結ぶ辺を張ると G が問題文の条件を満たします。
この時、コストは W_1 + W_3 = 2 + 5 = 7 になります。 コストが 7 未満で条件を満たす辺の張り方は存在しないため、これが答えになります。

2 番目のテストケースでは、コストの総和は (W_7 + W_6) + (W_1 + W_5) = 1100000 + 10001 = 1110001 になり、これが最小です。

Score : 800 points

Problem Statement

You are given an undirected tree G with N vertices. The degree of every vertex in G is at most 3.
The vertices are numbered 1 to N. The edges are numbered 1 to N-1, and edge i connects vertex u_i and vertex v_i.
Each vertex has a fixed weight, and the weight of vertex i is W_i.

You will add zero or more edges to G. The cost of adding an edge between vertex i and vertex j is W_i + W_j.

Print one way to add edges to satisfy the following condition for the minimum possible total cost.

  • G is 2-vertex-connected. In other words, for every vertex v in G, removing v and its incident edges from G would not disconnect G.

You have T test cases to solve.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • The given graph is a tree.
  • The degree of every vertex in the given graph is at most 3.
  • 1 \leq W_i \leq 10^9
  • W_i is an integer.
  • The sum of N across the test cases is at most 2 \times 10^5.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i represents the i-th test case:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_N

Each test case is in the following format:

N 
W_1 W_2 \dots W_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

For each test case, print a solution in the following format, where:

  • M is the number of added edges, and
  • the i-th added edge connects vertex a_i and vertex b_i.

If multiple solutions exist, any of them would be accepted.

M 
a_1 b_1
a_2 b_2
\vdots
a_M b_M

Sample Input 1

2
3
2 3 5
1 2
2 3
7
1 10 100 1000 10000 100000 1000000
1 2
2 3
2 4
3 5
3 6
4 7

Sample Output 1

1
1 3
2
7 6
1 5

In the first test case, adding an edge connecting vertex 1 and vertex 3 makes G satisfy the condition in the problem statement.
The cost of this is W_1 + W_3 = 2 + 5 = 7. There is no way to add edges to satisfy the condition for a cost of less than 7, so this is a valid solution.

In the second test case, the solution above has a total cost of (W_7 + W_6) + (W_1 + W_5) = 1100000 + 10001 = 1110001, which is the minimum possible.

F - Count Sorted Arrays

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 900

問題文

整数 NM 個の整数の組 (a_1, b_1), (a_2, b_2), \dots, (a_M, b_M) があります。各 a_i, b_i1 \leq a_i \lt b_i \leq N を満たします。

はじめ、あなたは (1,2,\dots,N) の順列を N! 種類すべて持っています。
あなたは M 回の操作を行います。i 回目の操作は次の通りです。

  • 持っている N! 個の順列すべてに対して次の処理を行う。
    • 順列の a_i 番目の要素と b_i 番目の要素の値を比較して、前者の方が大きければ両者を swap する。

1 \leq i \leq M について、i 回目の操作を終了した時点で持っている順列のうち昇順にソートされている列の個数を S_i とします。
S_1, S_2, \dots, S_M を出力してください。

ただし、入力では (a_i, b_i) の代わりに整数の組 (x_i, y_i) が与えられます。
(a_i, b_i) の値は x_i, y_i, S_{i-1} を用いて次の手順で得ることができます。(便宜上 S_0 = 1 とします。)

  • c_i = ((x_i + S_{i-1}) \bmod N) + 1 とする。
  • d_i = ((y_i + S_{i-1} \times 2) \bmod N) + 1 とする。(ここで c_i \neq d_i が保証される。)
  • a_i = \min(c_i, d_i) とする。
  • b_i = \max(c_i, d_i) とする。

制約

  • 2 \leq N \leq 15
  • 1 \leq M \leq 5 \times 10^5
  • 1 \leq a_i \lt b_i \leq N
  • 0 \leq x_i, y_i \leq N - 1

入力

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

N M
x_1 y_1
x_2 y_2
\vdots
x_M y_M

出力

M 行出力せよ。i 行目には S_i を出力せよ。


入力例 1

2 1
1 1

出力例 1

2

はじめ、持っている順列は (1, 2)(2, 1) です。
(a_1, b_1) = (1, 2) です。1 回目の操作を終了した時点で持っている順列は (1, 2)2 個になります。よって 2 を出力します。


入力例 2

3 4
0 1
2 1
1 1
0 1

出力例 2

2
4
4
6

(a_i, b_i) は順に (1, 2), (2, 3), (1, 3), (1, 2) です。


入力例 3

5 5
4 4
0 4
1 1
2 4
1 2

出力例 3

2
4
4
8
16

(a_i, b_i) は順に (1, 2), (3, 4), (1, 5), (2, 3), (4, 5) です。

Score : 900 points

Problem Statement

There are an integer N and M pairs of integers: (a_1, b_1), (a_2, b_2), \dots, (a_M, b_M). Each pair (a_i, b_i) satisfies 1 \leq a_i \lt b_i \leq N.

Initally, you have all N! permutations of (1,2,\dots,N).
You will perform M operations. The i-th operation is as follows.

  • Do the following for each of your N! permutations.
    • Compare the a_i-th and b_i-th elements. If the former is greater, swap the two elements.

For each 1 \leq i \leq M, let S_i be the number of permutations of yours that are already sorted in ascending order at the end of the i-th operation.
Print S_1, S_2, \dots, S_M.

Here, the input gives you pairs of integers (x_i, y_i) instead of (a_i, b_i).
The values of (a_i, b_i) can be obtained using x_i, y_i, and S_{i-1} as follows. (Let S_0 = 1 for convenience.)

  • Let c_i = ((x_i + S_{i-1}) \bmod N) + 1.
  • Let d_i = ((y_i + S_{i-1} \times 2) \bmod N) + 1. (Here, it is guaranteed that c_i \neq d_i.)
  • Let a_i = \min(c_i, d_i).
  • Let b_i = \max(c_i, d_i).

Constraints

  • 2 \leq N \leq 15
  • 1 \leq M \leq 5 \times 10^5
  • 1 \leq a_i \lt b_i \leq N
  • 0 \leq x_i, y_i \leq N - 1

Input

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

N M
x_1 y_1
x_2 y_2
\vdots
x_M y_M

Output

Print M lines. The i-th line should contain S_i.


Sample Input 1

2 1
1 1

Sample Output 1

2

You start with the permutations (1, 2) and (2, 1).
We have (a_1, b_1) = (1, 2). At the end of the first operation, you have two copies of (1, 2), so you should print 2.


Sample Input 2

3 4
0 1
2 1
1 1
0 1

Sample Output 2

2
4
4
6

(a_i, b_i) in order are (1, 2), (2, 3), (1, 3), (1, 2).


Sample Input 3

5 5
4 4
0 4
1 1
2 4
1 2

Sample Output 3

2
4
4
8
16

(a_i, b_i) in order are (1, 2), (3, 4), (1, 5), (2, 3), (4, 5).