A - Flip Row or Col 2

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

配点 : 800

問題文

N\times N 行列 A=(A_{i,j})\ (1\leq i,j\leq N) および長さ N の整数列 R=(R_1,R_2,\ldots,R_N), C=(C_1,C_2,\ldots,C_N) が与えられます. A の各要素は 0 または 1 であり,R,C の各要素は 0 以上 \frac{N}{4} 未満です.

あなたは 0 および 1 からなる長さ N の文字列の組 X,Y を自由に選ぶことができます.選んだ文字列 X,Y をもとに以下の操作を行います.

  • まず,各 i=1,2,\ldots,N に対して以下を行う.X_iXi 文字目として,
    • X_i0 のとき,なにもしない.
    • X_i1 のとき,Ai 行目の要素を全て反転する.すなわち,各 j=1,2,\ldots,N に対して A_{i,j}1-A_{i,j} で置き換える.
  • 次に,各 j=1,2,\ldots,N に対して以下を行う.Y_jYj 文字目として,
    • Y_j0 のとき,なにもしない.
    • Y_j1 のとき,Aj 列目の要素を全て反転する.すなわち,各 i=1,2,\ldots,N に対して A_{i,j}1-A_{i,j} で置き換える.

X,Y をうまく選ぶことで操作後の A が以下の条件を満たすことが可能かどうか判定し,可能ならばそのような組を一つを出力してください.

  • i=1,2,\ldots,N に対して,Ai 行目の要素の総和 \displaystyle \sum_{j=1}^N A_{i,j}R_i である.
  • j=1,2,\ldots,N に対して,Aj 列目の要素の総和 \displaystyle \sum_{i=1}^N A_{i,j}C_j である.

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

制約

  • 1\leq T\leq 10^5
  • 1\leq N\leq 1000
  • A_{i,j} \in\lbrace 0,1\rbrace
  • 0\leq R_i,C_j\lt \frac{N}{4}
  • T,N,R_i,C_j は整数
  • 1 つの入力ファイルに含まれる N^2 の総和は 10^6 以下

入力

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

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

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
R_1 R_2 \ldots R_N
C_1 C_2 \ldots C_N

出力

\textrm{case}_1,\textrm{case}_2,\ldots,\textrm{case}_T に対する答えを順に以下の形式で出力せよ.

条件を満たす X,Y の組が存在しなければ No と出力せよ.

存在する場合,

Yes
X
Y

と出力せよ.解が複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

3
2
10
01
0 0
0 0
5
10101
11110
01111
10011
00110
1 1 1 1 1
1 1 1 1 1
10
0001011010
0101101000
0000101111
0010011100
0111000001
0011100110
1110001110
1100110000
0110111011
1101101101
1 1 0 2 0 0 2 2 2 2
2 2 0 1 0 2 1 2 1 1

出力例 1

Yes
01
10
Yes
01101
10001
No

1 つ目のテストケースについて,A=\begin{pmatrix}1&0 \\ 0&1\end{pmatrix} です. X,Y がそれぞれ 01, 10 の場合を考えます.操作は次のように行われます.

  • X_10 なのでなにもしません.
  • X_21 なので A2 行目の要素を反転して A=\begin{pmatrix}1&0 \\ 1&0\end{pmatrix} となります.
  • Y_11 なので A1 列目の要素を反転して A=\begin{pmatrix}0&0 \\ 0&0\end{pmatrix} となります.
  • Y_20 なのでなにもしません.

したがって,操作後の AA=\begin{pmatrix}0&0 \\ 0&0\end{pmatrix} です.A1 行目の要素の総和,2 行目の要素の総和,1 列目の要素の総和,2 列目の要素の総和は全て 0 であるため,条件を満たします.

他にも

Yes
10
01

と出力しても正解となります.

Score : 800 points

Problem Statement

You are given an N\times N matrix A=(A_{i,j})\ (1\leq i,j\leq N) and integer sequences of length N: R=(R_1,R_2,\ldots,R_N), C=(C_1,C_2,\ldots,C_N). Each element of A is 0 or 1, and each element of R,C is at least 0 and less than \frac{N}{4}.

You can freely choose a pair of strings X,Y of length N consisting of 0 and 1. Based on the chosen strings X,Y, perform the following operations:

  • First, for each i=1,2,\ldots,N, do the following. Let X_i be the i-th character of X:
    • If X_i is 0, do nothing.
    • If X_i is 1, flip all elements in the i-th row of A. That is, for each j=1,2,\ldots,N, replace A_{i,j} with 1-A_{i,j}.
  • Next, for each j=1,2,\ldots,N, do the following. Let Y_j be the j-th character of Y:
    • If Y_j is 0, do nothing.
    • If Y_j is 1, flip all elements in the j-th column of A. That is, for each i=1,2,\ldots,N, replace A_{i,j} with 1-A_{i,j}.

Determine whether it is possible to choose X,Y cleverly so that the matrix A after the operations satisfies the following conditions, and if possible, output one such pair.

  • For each i=1,2,\ldots,N, the sum of elements in the i-th row of A, \displaystyle \sum_{j=1}^N A_{i,j}, is R_i.
  • For each j=1,2,\ldots,N, the sum of elements in the j-th column of A, \displaystyle \sum_{i=1}^N A_{i,j}, is C_j.

You are given T test cases, so answer each of them.

Constraints

  • 1\leq T\leq 10^5
  • 1\leq N\leq 1000
  • A_{i,j} \in\lbrace 0,1\rbrace
  • 0\leq R_i,C_j\lt \frac{N}{4}
  • T,N,R_i,C_j are integers.
  • The sum of N^2 over all test cases in one input file is at most 10^6.

Input

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

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

Each test case is given in the following format:

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}
R_1 R_2 \ldots R_N
C_1 C_2 \ldots C_N

Output

Output your solutions for \textrm{case}_1,\textrm{case}_2,\ldots,\textrm{case}_T in order in the following format:

If there is no pair X,Y that satisfies the conditions, output No.

If there exists such a pair, output:

Yes
X
Y

If there are multiple solutions, any of them will be considered correct.


Sample Input 1

3
2
10
01
0 0
0 0
5
10101
11110
01111
10011
00110
1 1 1 1 1
1 1 1 1 1
10
0001011010
0101101000
0000101111
0010011100
0111000001
0011100110
1110001110
1100110000
0110111011
1101101101
1 1 0 2 0 0 2 2 2 2
2 2 0 1 0 2 1 2 1 1

Sample Output 1

Yes
01
10
Yes
01101
10001
No

For the first test case, A=\begin{pmatrix}1&0 \\ 0&1\end{pmatrix}. Consider the case where X,Y are 01 and 10, respectively. The operations are performed as follows:

  • X_1 is 0, so do nothing.
  • X_2 is 1, so flip the elements in the 2nd row of A, resulting in A=\begin{pmatrix}1&0 \\ 1&0\end{pmatrix}.
  • Y_1 is 1, so flip the elements in the 1st column of A, resulting in A=\begin{pmatrix}0&0 \\ 0&0\end{pmatrix}.
  • Y_2 is 0, so do nothing.

Therefore, the matrix A after the operations is A=\begin{pmatrix}0&0 \\ 0&0\end{pmatrix}. The sum of elements in the 1st row, the sum of elements in the 2nd row, the sum of elements in the 1st column, and the sum of elements in the 2nd column are all 0, so the conditions are satisfied.

It would also be correct to output:

Yes
10
01
B - Adjacent Replace

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

配点 : 800

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) および非負整数 K が与えられます.

以下の操作を 10^4 回以下行うことで A_1=K にすることが可能かどうか判定してください.

  • 整数 i\ (1\leq i\leq N-1) を選ぶ.x=A_i\oplus A_{i+1} としたとき,A_i,A_{i+1} をそれぞれ x で置き換える.

ここで,\oplus はビット単位 \mathrm{XOR} 演算です.

可能な場合,そのような操作手順を一つ出力してください.

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

ビット単位 \mathrm{XOR} 演算とは

非負整数 A, B のビット単位 \mathrm{XOR}A \oplus B は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。

制約

  • 1\leq T\leq 50
  • 3\leq N\leq 60
  • 0\leq A_i,K\lt 2^{60}
  • 入力は全て整数

入力

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

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

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

N K
A_1 A_2 \ldots A_N

出力

\mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T に対する答えを順に以下の形式で出力せよ.

操作を 10^4 回以下行うことで A_1=K とすることが不可能な場合 No と出力せよ.

可能な場合,操作手順を以下の形式で出力せよ.

Yes
M
i_1 i_2 \ldots i_M

ここで, M は操作回数,i_j\ (1\leq j\leq M)j 回目の操作で選ぶ整数 i を表す.0\leq M\leq 10^4, 1\leq i_j\leq N-1 を満たす必要がある.

条件を満たす操作手順が複数存在する場合,そのどれを出力しても正解とみなされる.


入力例 1

5
3 5
2 3 4
3 0
2 3 4
3 8
2 3 4
3 2
2 3 4
4 10
2 3 4 8

出力例 1

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

Yes
4
2 3 2 1

1 つ目のテストケースについて,次のように操作をすることで A_1=5 とすることができます.

  1. i=2 を選ぶ.A=(2,7,7) になる.
  2. i=1 を選ぶ.A=(5,5,7) になる.

2 つ目のテストケースについて,次のように操作をすることで A_1=0 とすることができます.

  1. i=1 を選ぶ.A=(1,1,4) になる.
  2. i=1 を選ぶ.A=(0,0,4) になる.
  3. i=1 を選ぶ.A=(0,0,4) になる.

3 つ目のテストケースについて,10^4 回以下の操作によって A_1=8 とすることはできません.

Score : 800 points

Problem Statement

You are given a sequence of non-negative integers of length N: A=(A_1,A_2,\ldots,A_N) and a non-negative integer K.

Determine whether it is possible to make A_1=K by performing the following operation at most 10^4 times:

  • Choose an integer i\ (1\leq i\leq N-1). Let x=A_i\oplus A_{i+1}, then replace both A_i,A_{i+1} with x.

Here, \oplus denotes the bitwise XOR operation.

If possible, output one such sequence of operations.

You are given T test cases, so solve each of them.

What is bitwise XOR operation?

The bitwise XOR of non-negative integers A, B, denoted A \oplus B, is defined as follows:

  • When A \oplus B is written in binary, the digit in the 2^k (k \geq 0) place is 1 if exactly one of A, B has 1 in the 2^k place when written in binary, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).

Constraints

  • 1\leq T\leq 50
  • 3\leq N\leq 60
  • 0\leq A_i,K\lt 2^{60}
  • All input values are integers.

Input

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

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

Each test case is given in the following format:

N K
A_1 A_2 \ldots A_N

Output

Output your solutions for \mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T in order in the following format:

If it is impossible to make A_1=K by performing the operation at most 10^4 times, output No.

If possible, output the sequence of operations in the following format:

Yes
M
i_1 i_2 \ldots i_M

Here, M is the number of operations, and i_j\ (1\leq j\leq M) represents the integer i chosen in the j-th operation. You need to satisfy 0\leq M\leq 10^4, 1\leq i_j\leq N-1.

If there are multiple sequences of operations that satisfy the conditions, any of them will be considered correct.


Sample Input 1

5
3 5
2 3 4
3 0
2 3 4
3 8
2 3 4
3 2
2 3 4
4 10
2 3 4 8

Sample Output 1

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

Yes
4
2 3 2 1

For the first test case, you can make A_1=5 by performing the operations as follows:

  1. Choose i=2. A becomes (2,7,7).
  2. Choose i=1. A becomes (5,5,7).

For the second test case, you can make A_1=0 by performing the operations as follows:

  1. Choose i=1. A becomes (1,1,4).
  2. Choose i=1. A becomes (0,0,4).
  3. Choose i=1. A becomes (0,0,4).

For the third test case, it is impossible to make A_1=8 with at most 10^4 operations.

C - Circular Tree Embedding

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

配点 : 900

問題文

この問題では,頂点に 1,2,\ldots,N の番号が付けられ,辺に番号が付いていない N 頂点の木を単に N 頂点の木と呼ぶことにします.

N 頂点の木 T および (1,2,\ldots,N) の順列 Q=(Q_1,Q_2,\ldots,Q_N) が次の条件を満たすとき,順列 Q は木 T良い順列 であると呼びます.

円周上に N 個の点 1,2,\ldots,N がこの順で反時計回りに等間隔で配置されている.木 T の各辺 \lbrace u,v\rbrace に対して 2Q_u,Q_v を結ぶ線分を書き込む.このとき,書き込まれた N-1 本の線分のうちどの 2 本を選んでも,それらは端点を除いて共有点を持たない.

M 個の (1,2,\ldots,N) の順列 P^1=(P^1_1,P^1_2,\ldots,P^1_N),P^2=(P^2_1,P^2_2,\ldots,P^2_N),\ldots,P^M=(P^M_1,P^M_2,\ldots,P^M_N) が与えられます.

P^1,P^2,\ldots,P^M が全て良い順列となるような N 頂点の木の個数を 998244353 で割った余りを求めて下さい.

制約

  • 2\leq N,M\leq 500
  • P^1,P^2,\ldots,P^M(1,2,\ldots,N) の順列
  • 入力は全て整数

入力

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

N M
P^1_1 P^1_2 \ldots P^1_N
P^2_1 P^2_2 \ldots P^2_N
\vdots
P^M_1 P^M_2 \ldots P^M_N

出力

P^1,P^2,\ldots,P^M が全て良い順列となるような N 頂点の木の個数を 998244353 で割った余りを出力せよ.


入力例 1

4 2
1 4 2 3
3 1 4 2

出力例 1

8

たとえば以下の 2 つの木に対して,P^1,P^2 はどちらも良い順列です.(青い数字は頂点番号を表しています.)

一方,以下の木に対して P^2 は良い順列ではありません.

P^1,P^2 がともに良い順列となるような 4 頂点の木は全部で 8 つあります.


入力例 2

12 3
7 9 1 10 8 12 2 6 11 5 4 3
8 10 4 12 11 7 6 3 1 2 9 5
10 4 9 7 5 1 3 11 8 12 6 2

出力例 2

540

Score : 900 points

Problem Statement

In this problem, we simply call a tree with N vertices numbered 1,2,\ldots,N and unnumbered edges an N-vertex tree.

When an N-vertex tree T and a permutation Q=(Q_1,Q_2,\ldots,Q_N) of (1,2,\ldots,N) satisfy the following condition, the permutation Q is called a good permutation of tree T:

There are N points 1,2,\ldots,N arranged counterclockwise at equal intervals on a circle in this order. For each edge \lbrace u,v\rbrace of tree T, draw a line segment connecting the two points Q_u,Q_v. Then, among the N-1 line segments drawn, no two of them have a common point except at their endpoints.

You are given M permutations of (1,2,\ldots,N): P^1=(P^1_1,P^1_2,\ldots,P^1_N),P^2=(P^2_1,P^2_2,\ldots,P^2_N),\ldots,P^M=(P^M_1,P^M_2,\ldots,P^M_N).

Find the number, modulo 998244353, of N-vertex trees such that P^1,P^2,\ldots,P^M are all good permutations.

Constraints

  • 2\leq N,M\leq 500
  • P^1,P^2,\ldots,P^M are permutations of (1,2,\ldots,N).
  • All input values are integers.

Input

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

N M
P^1_1 P^1_2 \ldots P^1_N
P^2_1 P^2_2 \ldots P^2_N
\vdots
P^M_1 P^M_2 \ldots P^M_N

Output

Output the number, modulo 998244353, of N-vertex trees such that P^1,P^2,\ldots,P^M are all good permutations.


Sample Input 1

4 2
1 4 2 3
3 1 4 2

Sample Output 1

8

For example, for the following two trees, both P^1,P^2 are good permutations. (The blue numbers represent vertex numbers.)

On the other hand, for the following tree, P^2 is not a good permutation.

There are 8 four-vertex trees in total such that both P^1,P^2 are good permutations.


Sample Input 2

12 3
7 9 1 10 8 12 2 6 11 5 4 3
8 10 4 12 11 7 6 3 1 2 9 5
10 4 9 7 5 1 3 11 8 12 6 2

Sample Output 2

540
D - Limestone

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

配点 : 1000

問題文

正整数 H,W が与えられます.

HW 列の行列 A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W) があり,はじめ全ての要素は 0 です.

この行列に対して,以下の 2 種類の操作を好きな順番で何度でも行うことができます.

  • 整数 r,c\ (1\leq r\leq H,1\leq c\leq W) を選ぶ.A_{r,1},A_{r,2},\ldots,A_{r,c} を全て 1 にする.
  • 整数 r,c\ (1\leq r\leq H,1\leq c\leq W) を選ぶ.A_{1,c},A_{2,c},\ldots,A_{r,c} を全て 1 にする.

上記の操作を繰り返して得られる行列全てに対する \displaystyle \sum_{i=1}^H\sum_{j=1}^W A_{i,j} の総和を 998244353 で割った余りを求めてください.

制約

  • 1\leq H,W
  • HW\leq 2\times 10^5
  • 入力は全て整数

入力

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

H W

出力

操作を繰り返して得られる行列全てに対する \displaystyle \sum_{i=1}^H\sum_{j=1}^W A_{i,j} の総和を 998244353 で割った余りを出力せよ.


入力例 1

2 2

出力例 1

29

操作を繰り返して得られる行列は以下の 14 通りあります.

\begin{pmatrix}0&0 \\ 0&0\end{pmatrix} \begin{pmatrix}1&0 \\ 0&0\end{pmatrix} \begin{pmatrix}0&1 \\ 0&0\end{pmatrix} \begin{pmatrix}1&1 \\ 0&0\end{pmatrix} \begin{pmatrix}0&0 \\ 1&0\end{pmatrix} \begin{pmatrix}1&0 \\ 1&0\end{pmatrix} \begin{pmatrix}0&1 \\ 1&0\end{pmatrix} \begin{pmatrix}1&1 \\ 1&0\end{pmatrix} \begin{pmatrix}0&1 \\ 0&1\end{pmatrix} \begin{pmatrix}1&1 \\ 0&1\end{pmatrix} \begin{pmatrix}0&0 \\ 1&1\end{pmatrix} \begin{pmatrix}1&0 \\ 1&1\end{pmatrix} \begin{pmatrix}0&1 \\ 1&1\end{pmatrix} \begin{pmatrix}1&1 \\ 1&1\end{pmatrix}

答えは 0+1+1+2+1+2+2+3+2+3+2+3+3+4=29 です.

\begin{pmatrix}0&0 \\ 0&1\end{pmatrix}\begin{pmatrix}1&0 \\ 0&1\end{pmatrix} はどのように操作しても得ることができません.


入力例 2

1 10

出力例 2

5120

入力例 3

123 456

出力例 3

428623282

Score : 1000 points

Problem Statement

You are given positive integers H,W.

There is an H \times W matrix A=(A_{i,j})\ (1\leq i\leq H,1\leq j\leq W), where initially all elements are 0.

You can perform the following two types of operations on this matrix in any order and any number of times:

  • Choose integers r,c\ (1\leq r\leq H,1\leq c\leq W). Set all of A_{r,1},A_{r,2},\ldots,A_{r,c} to 1.
  • Choose integers r,c\ (1\leq r\leq H,1\leq c\leq W). Set all of A_{1,c},A_{2,c},\ldots,A_{r,c} to 1.

Find the sum of \displaystyle \sum_{i=1}^H\sum_{j=1}^W A_{i,j} over all matrices that can be obtained by repeating the above operations, modulo 998244353.

Constraints

  • 1\leq H,W
  • HW\leq 2\times 10^5
  • All input values are integers.

Input

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

H W

Output

Output the sum of \displaystyle \sum_{i=1}^H\sum_{j=1}^W A_{i,j} over all matrices that can be obtained by repeating the operations, modulo 998244353.


Sample Input 1

2 2

Sample Output 1

29

There are 14 matrices that can be obtained by repeating the operations:

\begin{pmatrix}0&0 \\ 0&0\end{pmatrix} \begin{pmatrix}1&0 \\ 0&0\end{pmatrix} \begin{pmatrix}0&1 \\ 0&0\end{pmatrix} \begin{pmatrix}1&1 \\ 0&0\end{pmatrix} \begin{pmatrix}0&0 \\ 1&0\end{pmatrix} \begin{pmatrix}1&0 \\ 1&0\end{pmatrix} \begin{pmatrix}0&1 \\ 1&0\end{pmatrix} \begin{pmatrix}1&1 \\ 1&0\end{pmatrix} \begin{pmatrix}0&1 \\ 0&1\end{pmatrix} \begin{pmatrix}1&1 \\ 0&1\end{pmatrix} \begin{pmatrix}0&0 \\ 1&1\end{pmatrix} \begin{pmatrix}1&0 \\ 1&1\end{pmatrix} \begin{pmatrix}0&1 \\ 1&1\end{pmatrix} \begin{pmatrix}1&1 \\ 1&1\end{pmatrix}

The answer is 0+1+1+2+1+2+2+3+2+3+2+3+3+4=29.

\begin{pmatrix}0&0 \\ 0&1\end{pmatrix} and \begin{pmatrix}1&0 \\ 0&1\end{pmatrix} cannot be obtained by any sequence of operations.


Sample Input 2

1 10

Sample Output 2

5120

Sample Input 3

123 456

Sample Output 3

428623282