A - Hamming-Distant Arrays

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

配点 : 700

問題文

整数 N が与えられます. 1 以上 N 以下の整数からなる長さ N^2 の整数列 a,b に対し,その距離 d(a,b) を次のように定義します.

  • d(a,b)=a_i \neq b_i を満たす i (1 \leq i \leq N^2) の個数」

今から,1 以上 N 以下の整数からなる長さ N^2 の整数列を N 個作り,それらを x_1,x_2,\cdots,x_N とおきます(順番も区別します). 以下の条件を満たす (x_1,x_2,\cdots,x_N) の個数を 998244353 で割ったあまりを求めてください.

  • 1 以上 N 以下の整数からなる長さ N^2 の整数列 y をどのようにとっても,ある 1 \leq i \leq N が存在し,d(x_i,y) \geq N^2-N を満たす.

制約

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

入力

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

N

出力

答えを出力せよ.


入力例 1

2

出力例 1

80

例えば,(x_1,x_2)=((1,1,2,2),(1,2,1,2)) は条件を満たしません. y=(1,1,1,2) とすると,d(x_1,y)=1,d(x_2,y)=1 となるためです.

一方,(x_1,x_2)=((1,1,1,1),(2,2,2,2)) は条件を満たします.

条件を満たす (x_1,x_2)80 通りあります.


入力例 2

3

出力例 2

597965565

入力例 3

10

出力例 3

241191911

Score : 700 points

Problem Statement

You are given an integer N. For length-N^2 integer sequences a and b consisting of integers between 1 and N (inclusive), we define their distance d(a,b) as follows:

  • d(a,b)= "the number of indices i (1 \leq i \leq N^2) such that a_i \neq b_i."

Now, you will create N length-N^2 integer sequences consisting of integers between 1 and N, and denote them as x_1,x_2,\cdots,x_N (their order also matters). Find the number, modulo 998244353, of instances of (x_1,x_2,\cdots,x_N) satisfying the following condition.

  • For any length-N^2 integer sequence y consisting of integers between 1 and N, there exists some 1 \leq i \leq N such that d(x_i,y) \geq N^2-N.

Constraints

  • 1 \leq N \leq 50
  • All input values are integers.

Input

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

N

Output

Output the answer.


Sample Input 1

2

Sample Output 1

80

For example, (x_1,x_2)=((1,1,2,2),(1,2,1,2)) does not satisfy the condition, because if y=(1,1,1,2), then d(x_1,y)=1,d(x_2,y)=1.

On the other hand, (x_1,x_2)=((1,1,1,1),(2,2,2,2)) satisfies the condition.

There are 80 instances of (x_1,x_2) that satisfy the condition.


Sample Input 2

3

Sample Output 2

597965565

Sample Input 3

10

Sample Output 3

241191911
B - Split and Reverse

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

配点 : 1100

問題文

0,1 からなる長さ N の整数列 X=(X_1,X_2,\cdots,X_N) が与えられます.

あなたは以下の操作を 0 回以上行うことができます.

  • X の各要素を A または B のグループに分類する. そして各グループに対して,そこに含まれる要素の順番を反転させる. より厳密に言えば,グループに含まれる要素が X_{i_1},X_{i_2},\cdots,X_{i_s} (i_1<i_2<\cdots<i_s) であるとき,X_{i_j} の値を X_{i_{s+1-j}} で置き換えるという操作をすべての 1 \leq j \leq s に対して同時に行う.

あなたの目標は X を広義単調増加にすることです. 目標を達成するために必要な最小の操作回数と,その操作方法を求めてください. なお,この問題の制約下で常に解が存在することが証明できます.

1 つの入力につき,T ケースを解いてください.

制約

  • 1 \leq T \leq 250000
  • 1 \leq N \leq 250000
  • 0 \leq X_i \leq 1
  • T ケースにわたる N の総和は 250000 以下
  • 入力はすべて整数

入力

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

T
case_1
case_2
\vdots
case_T

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

N
X_1 X_2 \cdots X_N

出力

各ケースについて,以下の形式で出力せよ.

k
s_1
s_2
\vdots
s_k

ただしここで k は必要な最小の操作回数であり,s_ii 回目の操作を表す文字列である. s_iA,B からなる長さ N の文字列であり,s_ij 文字目は,i 回目の操作で X_j を入れるグループを表す. 解が複数ある場合はどれを出力してもよい.


入力例 1

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

出力例 1

2
AABA
BBBB
0

1 つ目のテストケースは,以下のように 2 回の操作で目標を達成することができ,これが最小の操作回数です.

  • 1 回目の操作: s_1=AABA で操作する.(X_1,X_2,X_4) がグループ A に含まれるため,これらの順番を反転させ,(X_1,X_2,X_4)=(1,1,0) となる. 同様に,(X_3) がグループ B に含まれるため,これらの順番を反転させ,(X_3)=(0) となる. よって,全体では X=(1,1,0,0) となる.

  • 2 回目の操作: s_2=BBBB で操作する.() がグループ A に含まれるため,これらの順番を反転させ,()=() となる. 同様に,(X_1,X_2,X_3,X_4) がグループ B に含まれるため,これらの順番を反転させ,(X_1,X_2,X_3,X_4)=(0,0,1,1) となる. よって,全体では X=(0,0,1,1) となる.

2 つ目のテストケースでは,操作を 0 回行うことで目標を達成できます.

Score : 1100 points

Problem Statement

You are given an integer sequence X=(X_1,X_2,\cdots,X_N) of length N consisting of 0 and 1.

You can perform the following operation zero or more times:

  • Classify each element of X into group A or group B. Then, for each group, reverse the order of the elements it contains. More formally, if the elements contained in a group are X_{i_1},X_{i_2},\cdots,X_{i_s} (i_1<i_2<\cdots<i_s), then for all 1 \leq j \leq s, simultaneously replace the value of X_{i_j} with X_{i_{s+1-j}}.

Your goal is to make X non-decreasing. Find the minimum number of operations needed to achieve the goal and the way to perform the operations. It can be proved that a solution always exists under the constraints of this problem.

Solve T cases for one input.

Constraints

  • 1 \leq T \leq 250000
  • 1 \leq N \leq 250000
  • 0 \leq X_i \leq 1
  • The sum of N over the T cases is at most 250000.
  • 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 test case is given in the following format:

N
X_1 X_2 \cdots X_N

Output

For each case, output in the following format:

k
s_1
s_2
\vdots
s_k

Here, k is the minimum number of operations needed, and s_i is a string representing the i-th operation. s_i is a string of length N consisting of A and B, and the j-th character of s_i represents the group to which X_j is assigned in the i-th operation. If there are multiple solutions, you may output any of them.


Sample Input 1

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

Sample Output 1

2
AABA
BBBB
0

For the first test case, the goal can be achieved in two operations as follows, which is the minimum number of operations:

  • First operation: Use s_1=AABA. Since (X_1,X_2,X_4) are in group A, reverse their order, resulting in (X_1,X_2,X_4)=(1,1,0). Similarly, since (X_3) is in group B, reverse their order, resulting in (X_3)=(0). Thus, overall we get X=(1,1,0,0).

  • Second operation: Use s_2=BBBB. Since () are in group A, reverse their order, resulting in ()=(). Similarly, since (X_1,X_2,X_3,X_4) are in group B, reverse their order, resulting in (X_1,X_2,X_3,X_4)=(0,0,1,1). Thus, overall we get X=(0,0,1,1).

For the second test case, the goal can be achieved by performing the operation zero times.

C - Slime Eat Slime

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

配点 : 1200

問題文

1 から N までの番号のついた N 匹のスライムがいます. それぞれのスライムの色は 0 以上 2K 以下の整数で表され,スライム i の色は C_i です.

今,M 組のスライムが(双方向の)友達関係にあり,具体的にはスライム A_i とスライム B_i は友達です (1 \leq i \leq M). ここで,どの 2 匹のスライムについても,友達関係をたどることで一方から他方へ到達できることが保証されます.

あなたは以下の操作を行うことができます.

  • まず,次の条件を両方とも満たすスライムの組 (u,v) を選ぶ.
    • スライム u,v は友達関係にある.
    • (C_u - C_v) \bmod (2K+1) \geq K+1(なお,x \bmod y の結果は常に [0,y-1] の範囲に入るとする)
  • そして,スライム u にスライム v を食べさせる. ここでスライム v は場から消える. また,スライム v と友達関係にあった u 以外のすべてのスライムは,スライム u と友達になる(すでにスライム u と友達であった場合は何も起きない).

適切に操作を繰り返すことでスライム 1 だけを場に残すことが可能かどうか判定してください.

1 つの入力につき,T ケースを解いてください.

制約

  • 1 \leq T \leq 125000
  • 2 \leq N \leq 250000
  • N-1 \leq M \leq 250000
  • 1 \leq K \leq N
  • 0 \leq C_i \leq 2K
  • 1 \leq A_i < B_i \leq N
  • (A_i,B_i) \neq (A_j,B_j) (i \neq j)
  • どの 2 匹のスライムについても,友達関係をたどることで一方から他方へ到達できる
  • T ケースにわたる N の総和は 250000 以下
  • T ケースにわたる M の総和は 250000 以下
  • 入力はすべて整数

入力

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

T
case_1
case_2
\vdots
case_T

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

N M K
C_1 C_2 \cdots C_N
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

T 行出力せよ. i 行目には,i ケース目においてスライム 1 だけを場に残すことが可能ならば Yes,不可能ならば No を出力せよ.


入力例 1

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

出力例 1

No
Yes
No
No

1 つ目のテストケースでは,操作を 1 回も行うことができません.

2 つ目のテストケースでは,以下の手順でスライム 1 だけを場に残すことができます.

  • (u,v)=(3,2) と選び,スライム 3 にスライム 2 を食べさせる.スライム 2 が消え,スライム 1 とスライム 3 が友達になる.
  • (u,v)=(1,3) と選び,スライム 1 にスライム 3 を食べさせる.スライム 3 が消える.

Score : 1200 points

Problem Statement

There are N slimes numbered from 1 to N. The color of each slime is represented by an integer between 0 and 2K (inclusive), and the color of slime i is C_i.

Currently, M pairs of slimes are in a (bidirectional) friendship. Specifically, slimes A_i and B_i are friends (1 \leq i \leq M). Here, it is guaranteed that for any two slimes, one can reach the other by following friendship relations.

You can perform the following operation:

  • First, choose a pair of slimes (u,v) that satisfies both of the following conditions:
    • Slimes u and v are in a friendship.
    • (C_u - C_v) \bmod (2K+1) \geq K+1 (here, the result of x \bmod y is always in the range [0,y-1]).
  • Then, let slime u eat slime v. Slime v disappears from the field. Also, all slimes except u that were in a friendship with slime v become friends with slime u (if they were already friends with slime u, nothing happens).

Determine whether it is possible to leave only slime 1 on the field by repeating the operation appropriately.

Solve T cases for one input.

Constraints

  • 1 \leq T \leq 125000
  • 2 \leq N \leq 250000
  • N-1 \leq M \leq 250000
  • 1 \leq K \leq N
  • 0 \leq C_i \leq 2K
  • 1 \leq A_i < B_i \leq N
  • (A_i,B_i) \neq (A_j,B_j) (i \neq j)
  • For any two slimes, one can reach the other by following friendship relations.
  • The sum of N over the T cases is at most 250000.
  • The sum of M over the T cases is at most 250000.
  • 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 test case is given in the following format:

N M K
C_1 C_2 \cdots C_N
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Output T lines. The i-th line should contain Yes if it is possible to leave only slime 1 on the field for the i-th case, and No otherwise.


Sample Input 1

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

Sample Output 1

No
Yes
No
No

In the first test case, no operation can be performed.

In the second test case, it is possible to leave only slime 1 on the field by the following procedure:

  • Choose (u,v)=(3,2) and let slime 3 eat slime 2. Slime 2 disappears, and slimes 1 and 3 become friends.
  • Choose (u,v)=(1,3) and let slime 1 eat slime 3. Slime 3 disappears.
D - Stochastic Dominance

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

配点 : 1400

問題文

L=10^8 とします.

整数 N 及び長さ M の非負整数列 A=(A_1,A_2,\cdots,A_M) (0 \leq A_i < L) が与えられます. 各 k=1,2,\cdots,N に対して,次の問題を解いてください.

1 から k までの番号のついた k 個の赤いボールと,1 から k までの番号のついた k 個の青いボールがあります. 今から以下の方法で各ボールに実数を書き込みます. なお,すべてのランダムな選択は独立です.

  • 1 \leq i \leq k について,赤いボール i に書き込む数は,L \times (i-1)+A_{j_i} である. ただし j_i1 以上 M 以下の範囲で一様ランダムに選ばれた整数である.

  • 1 \leq i \leq k について,青いボール i に書き込む数は [0,k \times L) の範囲で一様ランダムに選ばれた実数である.

すべてのボールに数を書き込んだあと以下の条件が満たされている確率を \text{mod }{998244353} で求めてください.

  • 任意の実数 x (0 \leq x \leq k \times L) に対して,「x 以下の数が書き込まれた赤いボールの個数」\geqx 以下の数が書き込まれた青いボールの個数」が成立する.
確率 \text{mod }{998244353} の定義

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

制約

  • 1 \leq N \leq 250000
  • 1 \leq M \leq 250000
  • 0 \leq A_i < L=10^8
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \cdots A_M

出力

N 行出力せよ. i 行目には k=i に対する答えを出力せよ.


入力例 1

2 1
50000000

出力例 1

499122177
686292993
  • k=1: 条件を満たす確率は 1/2 です.
  • k=2: 条件を満たす確率は 5/16 です.

入力例 2

3 5
78682095 25048034 71067122 94666780 1927105

出力例 2

741897823
406774904
435399949

入力例 3

10 7
40490214 25131781 2271243 52064315 34467030 27419560 56103210

出力例 3

457696464
497746652
679391958
742383895
245278311
709723420
729551291
1911348
414224424
650563524

Score : 1400 points

Problem Statement

Let L=10^8.

You are given an integer N and a non-negative integer sequence A=(A_1,A_2,\cdots,A_M) of length M (0 \leq A_i < L). For each k=1,2,\cdots,N, solve the following problem:

There are k red balls numbered from 1 to k and k blue balls numbered from 1 to k. Now, you will write a real number on each ball in the following way. Here, all random choices are independent.

  • For each 1 \leq i \leq k, the number written on red ball i is L \times (i-1)+A_{j_i}. Here, j_i is an integer chosen uniformly at random from the range between 1 and M (inclusive).

  • For each 1 \leq i \leq k, the number written on blue ball i is a real number chosen uniformly at random from the range [0,k \times L).

Find the probability, modulo 998244353, that the following condition is satisfied after writing numbers on all balls.

  • For any real number x (0 \leq x \leq k \times L), "the number of red balls with a number written that is at most x" \geq "the number of blue balls with a number written that is at most x".
Definition of probability modulo 998244353

It can be proved that the desired probability is always a rational number. Also, under the constraints of this problem, it can be proved that if the desired rational number is represented as an irreducible fraction \frac{P}{Q}, then Q \neq 0 \pmod{998244353}. Therefore, there uniquely exists an integer R satisfying R \times Q \equiv P \pmod{998244353}, 0 \leq R \lt 998244353. Output this R.

Constraints

  • 1 \leq N \leq 250000
  • 1 \leq M \leq 250000
  • 0 \leq A_i < L=10^8
  • All input values are integers.

Input

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

N M
A_1 A_2 \cdots A_M

Output

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


Sample Input 1

2 1
50000000

Sample Output 1

499122177
686292993
  • k=1: The probability of satisfying the condition is 1/2.
  • k=2: The probability of satisfying the condition is 5/16.

Sample Input 2

3 5
78682095 25048034 71067122 94666780 1927105

Sample Output 2

741897823
406774904
435399949

Sample Input 3

10 7
40490214 25131781 2271243 52064315 34467030 27419560 56103210

Sample Output 3

457696464
497746652
679391958
742383895
245278311
709723420
729551291
1911348
414224424
650563524
E - Squared Norm Maximization

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

配点 : 1400

問題文

N 個の整数組 (A_1,B_1),(A_2,B_2),\cdots,(A_N,B_N) が与えられます.

\{1,2,\cdots,N\} の部分集合 s に対し,そのスコアを以下のように定義します.

  • (\sum_{i \in s} A_i)^2 + (\sum_{i \in s} B_i)^2 - 3 \sum_{i \in s} (A_i^2+B_i^2)

スコアとしてありうる最大値を求めてください.

1 つの入力につき,T ケースを解いてください.

制約

  • 1 \leq T \leq 250000
  • 1 \leq N \leq 250000
  • \sum_{1 \leq i \leq N} |A_i| \leq 10^9
  • \sum_{1 \leq i \leq N} |B_i| \leq 10^9
  • T ケースにわたる N の総和は 250000 以下
  • 入力はすべて整数

入力

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

T
case_1
case_2
\vdots
case_T

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

T 行出力せよ. i 行目には i ケース目に対する答えを出力せよ.


入力例 1

4
4
1 0
0 1
-1 0
0 -1
4
1 0
1 0
1 0
1 0
6
1 -9
-7 -2
0 -10
10 1
2 -8
1 -8
10
19271058 -140039457
-3441379 -134156148
227767903 -70474702
-837477 -74699717
153584327 80404336
115741008 -22141349
-190028077 -21940357
76149725 17176032
159846847 -296045185
-53332199 142922717

出力例 1

0
4
296
178262497119089558

1 つ目のケースでは,s=\{\} とするとスコアが 0 になり,これが最大です.

2 つ目のケースでは,s=\{1,2,3,4\} とするとスコアが 4 になり,これが最大です.

Score : 1400 points

Problem Statement

You are given N pairs of integers (A_1,B_1),(A_2,B_2),\cdots,(A_N,B_N).

For a subset s of \{1,2,\cdots,N\}, we define its score as follows:

  • (\sum_{i \in s} A_i)^2 + (\sum_{i \in s} B_i)^2 - 3 \sum_{i \in s} (A_i^2+B_i^2)

Find the maximum possible value of the score.

Solve T cases for one input.

Constraints

  • 1 \leq T \leq 250000
  • 1 \leq N \leq 250000
  • \sum_{1 \leq i \leq N} |A_i| \leq 10^9
  • \sum_{1 \leq i \leq N} |B_i| \leq 10^9
  • The sum of N over the T cases is at most 250000.
  • 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 test case is given in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Output T lines. The i-th line should contain the answer for the i-th case.


Sample Input 1

4
4
1 0
0 1
-1 0
0 -1
4
1 0
1 0
1 0
1 0
6
1 -9
-7 -2
0 -10
10 1
2 -8
1 -8
10
19271058 -140039457
-3441379 -134156148
227767903 -70474702
-837477 -74699717
153584327 80404336
115741008 -22141349
-190028077 -21940357
76149725 17176032
159846847 -296045185
-53332199 142922717

Sample Output 1

0
4
296
178262497119089558

In the first case, if s=\{\}, the score is 0, which is the maximum.

In the second case, if s=\{1,2,3,4\}, the score is 4, which is the maximum.