A - All Winners

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

配点 : 400

問題文

将棋の団体戦の大会に N チームが参加しています。 各チームは M 人の選手からなります。 この大会は総当たり戦で、合計で \frac{N(N-1)}{2} 試合が行われます。 各試合ではそれぞれのチームの M 人の選手同士がランダムにマッチングされて対局を行い、必ず一方が勝って一方が負けます。 全ての試合が行われた後、各選手はちょうど N-1 回対局を行ったことになりますが、全ての対局に勝った選手には全勝賞が与えられます。 全勝賞を与えられる選手の人数としてありうる最大値を求めてください。

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

制約

  • 1 \leq T \leq 2 \times 10^5
  • 2 \leq N \leq 10^9
  • 1 \leq M \leq 10^9
  • 入力される値は全て整数

入力

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

T
case_1
case_2
\vdots
case_T

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

N M

出力

答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースについて全勝賞を与えられる選手の人数としてありうる最大値を出力せよ。


入力例 1

2
3 3
5 1

出力例 1

4
1

1 つ目のテストケースについて、 以下の 3 チームが大会に参加しているとします。
チーム T:選手 T_1,T_2,T_3
チーム W:選手 W_1,W_2,W_3
チーム R:選手 R_1,R_2,R_3
として、以下の結果になったとします。

  • チーム T 対 チーム W の試合
    • T_1W_1 の対局 → W_1 勝ち
    • T_2W_2 の対局 → W_2 勝ち
    • T_3W_3 の対局 → T_3 勝ち
  • チーム T 対 チーム R の試合
    • T_1R_3 の対局 → T_1 勝ち
    • T_2R_1 の対局 → R_1 勝ち
    • T_3R_2 の対局 → T_3 勝ち
  • チーム W 対 チーム R の試合
    • W_1R_3 の対局 → R_3 勝ち
    • W_2R_2 の対局 → R_2 勝ち
    • W_3R_1 の対局 → W_3 勝ち

このとき、全勝賞を与えられるのはチーム T の選手 T_3 のみです。
このケースでは、全勝賞を与えられる選手の人数としてありうる最大値は 4 です。

Score : 400 points

Problem Statement

N teams are participating in a team shogi tournament. Each team consists of M players. This tournament is a round-robin format, with a total of \frac{N(N-1)}{2} matches played. In each match, the M players from each team are randomly matched against each other, and one player always wins while the other loses. After all matches are played, each player will have played exactly N-1 games, and players who won all their games will be awarded the perfect record prize. Find the maximum possible number of players who can be awarded the perfect record prize.

Solve T test cases for each input file.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 2 \leq N \leq 10^9
  • 1 \leq M \leq 10^9
  • 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:

N M

Output

Output the answers in a total of T lines. The t-th line should contain the maximum possible number of players who can be awarded the perfect record prize for the t-th test case.


Sample Input 1

2
3 3
5 1

Sample Output 1

4
1

For the first test case, suppose the following 3 teams participate in the tournament:
Team T: players T_1,T_2,T_3
Team W: players W_1,W_2,W_3
Team R: players R_1,R_2,R_3
and suppose the following results occur:

  • Match between Team T and Team W
    • Game between T_1 and W_1: W_1 wins
    • Game between T_2 and W_2: W_2 wins
    • Game between T_3 and W_3: T_3 wins
  • Match between Team T and Team R
    • Game between T_1 and R_3: T_1 wins
    • Game between T_2 and R_1: R_1 wins
    • Game between T_3 and R_2: T_3 wins
  • Match between Team W and Team R
    • Game between W_1 and R_3: R_3 wins
    • Game between W_2 and R_2: R_2 wins
    • Game between W_3 and R_1: W_3 wins

Then, only player T_3 from Team T is awarded the perfect record prize.
In this case, the maximum possible number of players who can be awarded the perfect record prize is 4.

B - Swap If Equal Sum

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

配点 : 500

問題文

01 からなる長さ N の整数列 A=(A_1,A_2,\dots,A_N),\; B=(B_1,B_2,\dots,B_N) が与えられます。 数列 Ai 番目から j 番目の連続部分列を A[i,j] と表すことにします。ただし、i>j のとき、A[i,j] は長さ 0 の数列とします。 A に対して、以下の操作を何回でも行えます。

  • 以下の 2 条件を満たす 4 整数 a,b,c,d を選ぶ。
    • 1 \leq a \leq b < c \leq d \leq N
    • \sum_{i=a}^{b}{A_i}=\sum_{i=c}^{d}{A_i}
  • A[a,b]A[c,d] を入れ替える。より厳密には、A を、A[1,a-1], A[c,d], A[b+1,c-1], A[a,b], A[d+1,N] の順に結合した数列に置き換える。

AB に一致させられるかどうかを判定してください。

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

制約

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 1
  • 0 \leq B_i \leq 1
  • 全てのテストケースにおける N の総和は 2\times 10^5 以下
  • 入力される値は全て整数

入力

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

T
case_1
case_2
\vdots
case_T

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースについて AB に一致させられるならば Yes を、一致させられないならば No を出力せよ。


入力例 1

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

出力例 1

Yes
No
Yes

1 つ目のテストケースでは、A は最初、(1,1,1,0,0,1) です。
(a,b,c,d)=(1,2,3,6) を選んで操作を行うと、A(1,0,0,1,1,1) になります。
次に、(a,b,c,d)=(1,2,3,4) を選んで操作を行うと、A(0,1,1,0,1,1) になり、B に一致します。
2 つ目のテストケースでは、AB に一致させることはできません。
3 つ目のテストケースでは、A は最初から B に一致しています。

Score : 500 points

Problem Statement

You are given length-N integer sequences A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N), each consisting of 0 and 1. Let A[i,j] denote the consecutive subsequence of A from the i-th through the j-th element. If i>j, then A[i,j] is a length-0 sequence. You can perform the following operation on A any number of times:

  • Choose four integers a,b,c,d that satisfy the following two conditions:
    • 1 \leq a \leq b < c \leq d \leq N
    • \sum_{i=a}^{b}{A_i}=\sum_{i=c}^{d}{A_i}
  • Swap A[a,b] and A[c,d]. More precisely, replace A with the sequence formed by concatenating A[1,a-1], A[c,d], A[b+1,c-1], A[a,b], A[d+1,N] in this order.

Determine whether A can be made to match B.

Solve T test cases for each input file.

Constraints

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 1
  • 0 \leq B_i \leq 1
  • The sum of N over all test cases is at most 2\times 10^5.
  • 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:

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Output the answers in a total of T lines. The t-th line should contain Yes if A can be made to match B for the t-th test case, and No otherwise.


Sample Input 1

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

Sample Output 1

Yes
No
Yes

In the first test case, A is initially (1,1,1,0,0,1).
By choosing (a,b,c,d)=(1,2,3,6) and performing the operation, A becomes (1,0,0,1,1,1).
Next, by choosing (a,b,c,d)=(1,2,3,4) and performing the operation, A becomes (0,1,1,0,1,1), which matches B.
In the second test case, A cannot be made to match B.
In the third test case, A already matches B from the beginning.

C - Destruction of Walls

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

配点 : 600

問題文

H 行、横 W 列のグリッド状に部屋が並んでいます。 辺を共有して隣り合う部屋の間には壁があります。
ある部屋 A にいて、部屋 B が部屋 A上下左右 のいずれかに隣接し、部屋 A と部屋 B の間の壁が取り壊されているとき、部屋 A から部屋 B に移動することができます。

以下の条件を満たすような K 個の壁の組の選び方の総数を 998244353 で割った余りを求めてください。

  • 選んだ壁をすべて取り壊すと、最も左上の部屋からいくつかの部屋を経由して最も右下の部屋に移動することができる。

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

制約

  • 1 \leq T \leq 2 \times 10^5
  • 2 \leq H \leq 2 \times 10^5
  • 2 \leq W \leq 2 \times 10^5
  • \boldsymbol{0 \leq K \leq H+W}
  • 入力される値は全て整数

入力

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

T
case_1
case_2
\vdots
case_T

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

H W K

出力

答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースについて条件を満たすような K 個の壁の組の選び方の総数を 998244353 で割った余りを出力せよ。


入力例 1

6
2 2 1
2 2 3
2 2 2
2 2 4
200000 200000 400000
2 203 203

出力例 1

0
4
2
1
387815582
203

1 つ目のケースでは、どの 1 枚の壁を取り壊しても左上の部屋から右下の部屋に移動することはできません。
2 つ目のケースでは、4 枚ある壁から任意の 3 枚を選んで取り壊すと左上の部屋から右下の部屋に移動することができます。

Score : 600 points

Problem Statement

Rooms are arranged in a grid with H rows and W columns. There are walls between adjacent rooms that share an edge.
When you are in room A and room B is adjacent to room A in one of the four directions (up, down, left, right), and the wall between room A and room B has been demolished, you can move from room A to room B.

Find the total number, modulo 998244353, of ways to choose K walls such that the following condition is satisfied.

  • When all chosen walls are demolished, it is possible to move from the top-left room to the bottom-right room by passing through some rooms.

Solve T test cases for each input file.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 2 \leq H \leq 2 \times 10^5
  • 2 \leq W \leq 2 \times 10^5
  • \boldsymbol{0 \leq K \leq H+W}
  • 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:

H W K

Output

Output the answers in a total of T lines. The t-th line should contain the total number, modulo 998244353, of ways to select K walls that satisfy the condition for the t-th test case.


Sample Input 1

6
2 2 1
2 2 3
2 2 2
2 2 4
200000 200000 400000
2 203 203

Sample Output 1

0
4
2
1
387815582
203

In the first case, demolishing any single wall does not allow movement from the top-left room to the bottom-right room.
In the second case, choosing and demolishing any three walls out of the four available walls allows movement from the top-left room to the bottom-right room.

D - Insert XOR

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

配点 : 800

問題文

01 からなる長さ N の整数列 A=(A_1,A_2,\dots,A_N) があります。 Q 個のクエリが与えられます。 q 番目のクエリは以下のようなものです。

  • 1 以上 N 以下の整数 i_q が与えられる。A_{i_q}0 なら 1 に、1 なら 0 に変更する。

各クエリを処理するたびに、以下の問題の答えを求めてください。

01 からなる数列 B=(B_1,B_2,\dots,B_{|B|}) のうち以下の条件を満たすものを考えます。

  • B に対して以下の操作を好きな回数行って、数列 A と一致させることができる。
    • 1 以上 |B|-1 以下の整数 i を選ぶ。
    • B_iB_{i+1} の間に B_i\oplus B_{i+1} を挿入する。
条件を満たす B は必ず存在することが証明できます。 |B| として考えられる値の最小値を求めてください。

制約

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 1
  • 1 \leq Q \leq 5 \times 10^5
  • 1 \leq i_q \leq N
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \dots A_N
Q
i_1
i_2
\vdots
i_Q

出力

答えを合計 Q 行で出力せよ。 q 行目には、q 番目のクエリを処理した後の問題の答えを出力せよ。


入力例 1

3
0 0 0
2
1
2

出力例 1

3
2

A=(1,0,0) のとき、|B|B=(1,0,0) で最小となります。 A=(1,1,0) のとき、|B|B=(1,0) で最小となります。


入力例 2

8
0 1 0 0 1 1 1 0
3
1
4
1

出力例 2

5
2
3

Score : 800 points

Problem Statement

There is an integer sequence A=(A_1,A_2,\dots,A_N) of length N consisting of 0 and 1. You are given Q queries. The q-th query is as follows:

  • An integer i_q between 1 and N is given. Change A_{i_q} to 1 if it is 0, and to 0 if it is 1.

After processing each query, find the answer to the following problem:

Consider sequences B=(B_1,B_2,\dots,B_{|B|}) consisting of 0 and 1 that satisfy the following condition:

  • By performing the following operation any number of times on B, it can be made to match sequence A:
    • Choose an integer i between 1 and |B|-1, inclusive.
    • Insert B_i\oplus B_{i+1} between B_i and B_{i+1}.
It can be proved that there always exists a B that satisfies the condition. Find the minimum possible value of |B|.

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 1
  • 1 \leq Q \leq 5 \times 10^5
  • 1 \leq i_q \leq N
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N
Q
i_1
i_2
\vdots
i_Q

Output

Output the answers in a total of Q lines. The q-th line should contain the answer to the problem after processing the q-th query.


Sample Input 1

3
0 0 0
2
1
2

Sample Output 1

3
2

When A=(1,0,0), |B| is minimized by B=(1,0,0). When A=(1,1,0), |B| is minimized by B=(1,0).


Sample Input 2

8
0 1 0 0 1 1 1 0
3
1
4
1

Sample Output 2

5
2
3
E - Tile Grid with One Hole

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

配点 : 800

問題文

H 行、横 W 列のグリッドがあって、上から i 番目、左から j 番目のマスを (i,j) と表すことにします。 グリッドは (r,c) のマスだけに穴が空いています。穴が空いていないマス全体を何枚かのタイルで敷き詰めます。H \times W=L \times (N+M)+1 を満たす非負整数 N,M が与えられます。 縦 1 行、横 L 列のタイルを横長タイル、縦 L 行、横 1 列のタイルを縦長タイルと呼ぶことにします。 横長タイル N 枚と縦長タイル M 枚を回転させないまま使う敷き詰め方が存在するかを判定し、存在するなら敷き詰め方も示してください。
出力方法やより厳密な条件についての詳細は出力セクションを確認してください。

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

制約

  • 1 \leq T \leq 5
  • 1 \leq H \leq 1000
  • 1 \leq W \leq 1000
  • 2 \leq H \times W
  • 2 \leq L \leq 1000
  • 0 \leq N
  • 0 \leq M
  • 1 \leq r \leq H
  • 1 \leq c \leq W
  • H \times W=L \times (N+M)+1
  • 全てのテストケースにおける N+M の総和は 6\times 10^5 以下
  • 入力される値は全て整数

入力

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

T
case_1
case_2
\vdots
case_T

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

H W L N M r c

出力

答えを以下の形式で出力せよ。

output_1
output_2
\vdots
output_T

ここで、output_tt 番目のテストケースへの出力を表す。
各ケースでは、条件を満たすように敷き詰めることが可能ならば、i 枚目の横長タイルが覆うマスのうち最も左にあるマスを (A_i,B_i)j 枚目の縦長タイルが覆うマスのうち最も上にあるマスを (C_j,D_j) として、以下の形式で出力せよ。

Yes
A_1 B_1
A_2 B_2
\vdots
A_N B_N
C_1 D_1
C_2 D_2
\vdots
C_M D_M

より厳密には、以下の条件を全て満たす長さ N の整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N) と長さ M の整数列 C=(C_1,C_2,\dots,C_M),D=(D_1,D_2,\dots,D_M) を出力せよ。

  • \{(A_i,B_i+l)\mid i=1,2,\dots,N,\;l=0,1,\dots,L-1\}\{(C_j+l,D_j)\mid j=1,2,\dots,M,\;l=0,1,\dots,L-1\}\{(r,c)\} の和集合が、\{(h,w)\mid h=1,2,\dots,H,\;w=1,2,\dots,W\} に等しい。

なお、H \times W=L \times (N+M)+1 という制約より、この条件が成り立つとき、タイル同士が重なることはない。

条件を満たすことが不可能ならば No を出力せよ。


入力例 1

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

出力例 1

Yes
1 2
No
Yes
3 2
2 1
1 2
1 3

3 つ目のテストケースでは、最も左上のマスに穴が空いています。以下のように敷き詰めることができます。

  ┌─┬─┐
┌─┤ │ │
│ ├─┴─┤
└─┴───┘

Score : 800 points

Problem Statement

There is a grid with H rows and W columns, where the cell at the i-th row from the top and j-th column from the left is denoted as (i,j). The grid has a hole only in cell (r,c). We will tile all cells without a hole using several tiles. You are given non-negative integers N and M such that H \times W=L \times (N+M)+1. A tile of 1 row and L columns is called a horizontal tile, and a tile of L rows and 1 column is called a vertical tile. Determine whether there exists a way to tile using exactly N horizontal tiles and M vertical tiles without rotation, and also show one such way if it exists. For details on the output format and more precise conditions, check the output section.

Solve T test cases for each input file.

Constraints

  • 1 \leq T \leq 5
  • 1 \leq H \leq 1000
  • 1 \leq W \leq 1000
  • 2 \leq H \times W
  • 2 \leq L \leq 1000
  • 0 \leq N
  • 0 \leq M
  • 1 \leq r \leq H
  • 1 \leq c \leq W
  • H \times W=L \times (N+M)+1
  • The sum of N+M over all test cases is at most 6\times 10^5.
  • 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:

H W L N M r c

Output

Output the answers in the following format:

output_1
output_2
\vdots
output_T

Here, output_t represents the output for the t-th test case.
For each case, if it is possible to tile satisfying the conditions, let (A_i,B_i) be the leftmost cell covered by the i-th horizontal tile and (C_j,D_j) be the topmost cell covered by the j-th vertical tile, and output in the following format:

Yes
A_1 B_1
A_2 B_2
\vdots
A_N B_N
C_1 D_1
C_2 D_2
\vdots
C_M D_M

More precisely, output integer sequences A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N) of length N and C=(C_1,C_2,\dots,C_M),D=(D_1,D_2,\dots,D_M) of length M that satisfy all of the following conditions:

  • The union of \{(A_i,B_i+l)\mid i=1,2,\dots,N,\;l=0,1,\dots,L-1\}, \{(C_j+l,D_j)\mid j=1,2,\dots,M,\;l=0,1,\dots,L-1\}, and \{(r,c)\} equals \{(h,w)\mid h=1,2,\dots,H,\;w=1,2,\dots,W\}.

Note that due to the constraint H \times W=L \times (N+M)+1, when this condition holds, tiles do not overlap with each other.

If it is impossible to satisfy the conditions, output No.


Sample Input 1

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

Sample Output 1

Yes
1 2
No
Yes
3 2
2 1
1 2
1 3

In the third test case, there is a hole in the top-left cell. It can be tiled as follows:

  ┌─┬─┐
┌─┤ │ │
│ ├─┴─┤
└─┴───┘