A - Swapping Game

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

配点 : 600

問題文

(1,2, \dots ,L) の順列が N 個あります。i (1 \le i \le N) 個目の順列は P_i であり、P_ij (1 \le j \le L) 番目の要素は P_{i,j} です。

はじめ、あなたは A = (1,2, \dots ,L) を持っており、所持金は 0 円です。これから、i = 1,2, \dots ,N の順に以下の操作を行います。

  1. まず、A の隣接 2 項の swap を 0 回または 1 回行う。
    • A の隣接 2 項の swap」とは、1 \le j \le L-1 なる j を選び、A_jA_{j+1} を入れ替える操作を指します。
  2. その後、A = P_i となっていれば C_i 円を獲得する。

最終的なあなたの所持金としてありえる金額の最大値を求めて下さい。

制約

  • 1 \leq N \leq 3 \times 10^4
  • 1 \leq L \leq 9
  • 0 \leq C_i \leq 10^4
  • P_i(1,2, \dots, L) の順列
  • 入力はすべて整数

入力

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

N L
C_1 P_{1,1} P_{1,2} \dots P_{1,L}
C_2 P_{2,1} P_{2,2} \dots P_{2,L}
\vdots
C_N P_{N,1} P_{N,2} \dots P_{N,L}

出力

答えを出力せよ。


入力例 1

4 3
200 2 1 3
400 3 1 2
300 2 3 1
100 3 2 1

出力例 1

600

以下のように操作を行うことで所持金を 600 円にすることが出来ます。

  • i=1 : A_1,A_2 を swap し、A=(2,1,3) とする。A=P_1 であるため、200 円を獲得する。
  • i=2 : A_2,A_3 を swap し、A=(2,3,1) とする。A=P_2 でない。
  • i=3 : swap を行わず、A=(2,3,1) のままとする。A=P_3 であるため、300 円を獲得する。
  • i=4 : A_1,A_2 を swap し、A=(3,2,1) とする。A=P_4 であるため、100 円を獲得する。

入力例 2

2 1
0 1
10000 1

出力例 2

10000

入力例 3

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

出力例 3

0

入力例 4

10 5
2615 5 1 3 4 2
4275 1 3 2 5 4
4331 3 1 5 2 4
1457 3 5 1 2 4
2222 1 3 2 4 5
5051 2 4 5 3 1
1082 2 3 1 5 4
1579 4 1 5 2 3
2855 5 1 3 2 4
5848 2 4 3 1 5

出力例 4

12345

Score : 600 points

Problem Statement

There are N permutations of (1,2,\dots,L). The i-th permutation is P_i, and the j-th element of P_i is P_{i,j} (1 \le j \le L).

Initially, you have A = (1,2,\dots,L), and your initial amount of money is 0 yen. From now on, for i = 1,2,\dots,N in this order, you will perform the following operation:

  1. First, perform zero or one swap of two adjacent elements in A.
    • Specifically, “a swap of two adjacent elements in A” means choosing j with 1 \le j \le L-1, and swapping A_j and A_{j+1}.
  2. Then, if A is equal to P_i, you gain C_i yen.

Find the maximum possible amount of money you can have at the end.

Constraints

  • 1 \leq N \leq 3 \times 10^4
  • 1 \leq L \leq 9
  • 0 \leq C_i \leq 10^4
  • Each P_i is a permutation of (1,2,\dots,L).
  • All input values are integers.

Input

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

N L
C_1 P_{1,1} P_{1,2} \dots P_{1,L}
C_2 P_{2,1} P_{2,2} \dots P_{2,L}
\vdots
C_N P_{N,1} P_{N,2} \dots P_{N,L}

Output

Print the answer.


Sample Input 1

4 3
200 2 1 3
400 3 1 2
300 2 3 1
100 3 2 1

Sample Output 1

600

By doing the operations as follows, you can end up with 600 yen.

  • For i=1: swap A_1 and A_2, so A=(2,1,3). We have A=P_1, so you gain 200 yen.
  • For i=2: swap A_2 and A_3, so A=(2,3,1). We have A \neq P_2.
  • For i=3: do not swap, so A=(2,3,1). We have A=P_3, so you gain 300 yen.
  • For i=4: swap A_1 and A_2, so A=(3,2,1). We have A=P_4, so you gain 100 yen.

Sample Input 2

2 1
0 1
10000 1

Sample Output 2

10000

Sample Input 3

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

Sample Output 3

0

Sample Input 4

10 5
2615 5 1 3 4 2
4275 1 3 2 5 4
4331 3 1 5 2 4
1457 3 5 1 2 4
2222 1 3 2 4 5
5051 2 4 5 3 1
1082 2 3 1 5 4
1579 4 1 5 2 3
2855 5 1 3 2 4
5848 2 4 3 1 5

Sample Output 4

12345
B - Hamming Distance is not 1

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

配点 : 800

問題文

非負整数 q,L,R\;(q \in \{ 0,1\}, \; L \leq R) が与えられます。 以下のすべての条件を満たす集合 S について考えます。

  • SL 以上 R 以下の互いに相異なる整数からなる。
  • a \in S, \; b \in S, \; a \neq b のとき、ab2 進表記で 2 桁以上異なる。 より厳密には、\left\lfloor\frac{a}{2^i}\right\rfloor\left\lfloor\frac{b}{2^i}\right\rfloor の偶奇が異なるような非負整数 i2 個以上存在する。
  • 上の 2 条件を満たすもののうち、要素数が最大となる。

q=0 のとき、このような集合 S の要素数を求めてください。
q=1 のとき、このような集合 S を一つ構築してください。

1 つの入力ファイルにつき、T 個のテストケースを解いてください。

制約

  • 1 \leq T \leq 2 \times 10^5
  • 0 \leq q \leq 1
  • 0 \leq L \leq R \leq 10^{18}
  • 全てのテストケースにおける q(R-L) の総和は 5\times 10^6 以下
  • 入力される値は全て整数

入力

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

T
case_1
case_2
\vdots
case_T

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

q L R

出力

答えを合計 T 行で出力せよ。
t 個目のテストケースの答えを t 行目に出力せよ。
各テストケースでは、q=0 ならば条件を満たす集合 S の要素数を出力せよ。
q=1 ならば条件を満たすような集合 S に対して

B_i=\begin{cases} 1 \; (S \text{ に } i \text{ が含まれる }) \\ 0 \; (S \text{ に } i \text{ が含まれない }) \end{cases}

として、以下の形式で出力せよ。

B_LB_{L+1}\dotsB_R

条件を満たす S が複数存在する場合はどれを出力してもよい。


入力例 1

3
1 0 3
0 1 2
1 213 213

出力例 1

1001
2
1

1 つ目のテストケースでは、S=\{0,3\} は条件を満たします。 S=\{1,2\} も条件を満たすため正解となります。
2 つ目のテストケースでは、条件を満たす S\{1,2\} のみで、その要素数は 2 です。

Score : 800 points

Problem Statement

You are given non-negative integers q,L,R\;(q \in \{ 0,1\}, \; L \leq R). Consider a set S that satisfies all of the following conditions.

  • S consists of distinct integers between L and R, inclusive.
  • If a \in S, \; b \in S, \; a \neq b, then a and b differ in at least two digits in binary representation. More formally, there exist at least two non-negative integers i such that \left\lfloor\frac{a}{2^i}\right\rfloor and \left\lfloor\frac{b}{2^i}\right\rfloor have different parities.
  • Among those satisfying the above two conditions, the number of elements is maximum.

If q=0, find the number of elements in such a set S.
If q=1, construct one such set S.

For each input file, solve T test cases.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 0 \leq q \leq 1
  • 0 \leq L \leq R \leq 10^{18}
  • The sum of q(R-L) over all test cases is at most 5\times 10^6.
  • All input values are integers.

Input

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

T
case_1
case_2
\vdots
case_T

Each case is given in the following format:

q L R

Output

Output the answers in a total of T lines.
The t-th line should contain the answer for the t-th test case.
For each test case, if q=0, print the number of elements in a set S that satisfies the conditions.
If q=1, for a set S that satisfies the conditions, let

B_i=\begin{cases} 1 \; (S \text{ contains } i) \\ 0 \; (S \text{ does not contain } i) \end{cases}

and output it in the following format:

B_LB_{L+1}\dotsB_R

If there are multiple sets S that satisfy the conditions, you may print any of them.


Sample Input 1

3
1 0 3
0 1 2
1 213 213

Sample Output 1

1001
2
1

In the first test case, S=\{0,3\} satisfies the conditions. S=\{1,2\} also satisfies the conditions, so it is correct as well.
In the second test case, the only S that satisfies the conditions is \{1,2\}, whose number of elements is 2.

C - Double X

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

配点 : 900

問題文

頂点に 1 から N の番号がついた N 頂点の木が 2 個与えられ、それぞれ T, U と呼びます。Ti 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。Ui 番目の辺は頂点 b_i と頂点 c_i を結ぶ辺です。
また、長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

k = 1, 2, \dots, N に対して次の小問題を解いてください。

次の条件を満たす整数の組 (x_1, x_2, x_3, x_4) は存在しますか?また、存在する場合は条件を満たす組における \sum_{i=1}^4 A_{x_i} の最小値を求めてください。

  • x_1, x_2, x_3, x_4 は全て相異なる。
  • x_1, x_2, x_3, x_4 は全て k でない。
  • 1 \leq i \lt j \leq 4 を満たす全ての整数 (i,j) について、T 上の x_i x_j パスに頂点 k は載っていて、かつ U 上の x_i x_j パスにも頂点 k は載っている。

t 個のテストケースが与えられるので、それぞれについて問題を解いてください。

制約

  • 1 \leq t \leq 10^5
  • 5 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq u_i \lt v_i \leq N
  • 1 \leq b_i \lt c_i \leq N
  • T, U は木
  • 全てのテストケースに対する N の総和は 10^5 以下
  • 入力される値は全て整数

入力

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

t
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_t

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

N
A_1 A_2 \dots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
b_1 c_1
b_2 c_2
\vdots
b_{N-1} c_{N-1}

出力

t 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、小問題の答えを空白区切りで k=1,2,\dots,N の順に 1 行に出力せよ。
小問題では、条件を満たす整数の組 (x_1, x_2, x_3, x_4) が存在しない場合は -1 を、存在する場合はそのような組における \sum_{i=1}^4 A_{x_i} の最小値を出力せよ。


入力例 1

2
5
20 26 1 25 213
1 5
3 5
2 5
4 5
4 5
1 5
2 5
3 5
20
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288
1 2
2 3
1 4
1 5
2 6
3 7
3 8
3 9
2 10
1 11
8 12
6 13
5 14
11 15
13 16
1 17
7 18
9 19
15 20
1 2
2 3
2 4
4 5
2 6
1 7
6 8
3 9
3 10
7 11
7 12
1 13
3 14
3 15
8 16
1 17
3 18
2 19
1 20

出力例 1

-1 -1 -1 -1 72
70664 616 131968 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

1 番目のテストケースについて、k=1,2,3,4 の時は条件を満たす (x_1, x_2, x_3, x_4) は存在しません。また、k=5 の時は (x_1,x_2,x_3,x_4) = (1,2,3,4) が条件を満たします。

Score : 900 points

Problem Statement

You are given two trees T and U, each with N vertices numbered from 1 to N. The i-th edge of T connects vertices u_i and v_i. The i-th edge of U connects vertices b_i and c_i.
You are also given a length-N integer sequence A=(A_1,A_2,\dots,A_N).

For k = 1, 2, \dots, N, solve the following subproblem.

Does there exist a tuple of integers (x_1, x_2, x_3, x_4) that satisfies the following conditions? If so, find the minimum value of \sum_{i=1}^4 A_{x_i} for a tuple satisfying the conditions.

  • x_1, x_2, x_3, x_4 are all distinct.
  • x_1, x_2, x_3, x_4 are all different from k.
  • For all integers (i,j) satisfying 1 \leq i \lt j \leq 4, the x_i x_j path in T contains vertex k, and the x_i x_j path in U also contains vertex k.

You are given t test cases; solve the problem for each of them.

Constraints

  • 1 \leq t \leq 10^5
  • 5 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq u_i \lt v_i \leq N
  • 1 \leq b_i \lt c_i \leq N
  • T and U are trees.
  • The sum of N over all test cases is at most 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format. Here, \mathrm{case}_i means the i-th test case.

t
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_t

For each test case, the input is given in the following format:

N
A_1 A_2 \dots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
b_1 c_1
b_2 c_2
\vdots
b_{N-1} c_{N-1}

Output

Output t lines. The i-th line should contain the answer for the i-th test case.
For each test case, output the answers to the subproblems in the order k=1,2,\dots,N, separated by spaces on one line.
For each subproblem, output -1 if there does not exist a tuple of integers (x_1, x_2, x_3, x_4) satisfying the conditions, or output the minimum value of \sum_{i=1}^4 A_{x_i} for such a tuple if it exists.


Sample Input 1

2
5
20 26 1 25 213
1 5
3 5
2 5
4 5
4 5
1 5
2 5
3 5
20
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288
1 2
2 3
1 4
1 5
2 6
3 7
3 8
3 9
2 10
1 11
8 12
6 13
5 14
11 15
13 16
1 17
7 18
9 19
15 20
1 2
2 3
2 4
4 5
2 6
1 7
6 8
3 9
3 10
7 11
7 12
1 13
3 14
3 15
8 16
1 17
3 18
2 19
1 20

Sample Output 1

-1 -1 -1 -1 72
70664 616 131968 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

For the first test case, when k=1,2,3,4, there does not exist (x_1, x_2, x_3, x_4) satisfying the conditions. When k=5, (x_1,x_2,x_3,x_4) = (1,2,3,4) satisfies the conditions.

D - Minimize Inversion

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

配点 : 1100

問題文

1\leq K\leq N を満たす正整数 N, K が与えられます。

(1, 2, \dots , N) の順列 P = (P_{1}, P_{2}, \dots , P_{N}) に対して、f(P) を以下のように定義します。

  • 以下の条件を満たす長さ N の整数列 A = (A_{1}, A_{2}, \dots, A_{N}) の転倒数としてあり得る最小の値
    • 任意の整数 1\leq i\leq N に対して |A_{i}| = P_{i} が成り立つ

a = 1, 2, \dots, N について、以下の問いに答えてください。

(1, 2, \dots , N) の順列 P = (P_{1}, P_{2}, \dots , P_{N}) であって、P_{K} = a を満たすものは (N - 1)! 通りありますが、その全てに対する f(P) の総和を 998244353 で割ったあまりを求めてください。

制約

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

入力

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

N K

出力

N 行出力せよ。i 行目には a = i のときの答えを出力せよ。


入力例 1

4 2

出力例 1

3
3
6
8

入力例 2

1 1

出力例 2

0

入力例 3

16 7

出力例 3

262718137
262718137
826158422
869037161
443336358
897729892
154945392
124681723
861272045
3176021
900143166
451899023
365015181
740245202
732524873
391198612

Score : 1100 points

Problem Statement

You are given positive integers N and K satisfying 1\leq K\leq N.

For a permutation P = (P_{1}, P_{2}, \dots , P_{N}) of (1, 2, \dots , N), define f(P) as follows:

  • The minimum possible value of the number of inversions of a length-N integer sequence A = (A_{1}, A_{2}, \dots, A_{N}) satisfying the following condition:
    • |A_{i}| = P_{i} for every integer 1\leq i\leq N.

For a = 1, 2, \dots, N, answer the following question:

There are (N - 1)! permutations P = (P_{1}, P_{2}, \dots , P_{N}) of (1, 2, \dots , N) satisfying P_{K} = a. Find the sum, modulo 998244353, of f(P) over all of them.

Constraints

  • 1\leq K\leq N\leq 2\times 10^{5}
  • All input values are integers.

Input

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

N K

Output

Output N lines. The i-th line should contain the answer for a = i.


Sample Input 1

4 2

Sample Output 1

3
3
6
8

Sample Input 2

1 1

Sample Output 2

0

Sample Input 3

16 7

Sample Output 3

262718137
262718137
826158422
869037161
443336358
897729892
154945392
124681723
861272045
3176021
900143166
451899023
365015181
740245202
732524873
391198612