A - Non-Adjacent Flip

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1 から N の番号がついた、表裏が区別できるコインが N 枚あります。コインの表裏は長さ N の文字列 S で表され、Si 番目の文字が 1 のときコイン i は表を向いており、0 のときコイン i は裏を向いています。

あなたは、以下の操作を 0 回以上好きな回数繰り返すことができます。

  • 1\leq i < j\leq N かつ j-i\geq \bm{2} を満たす整数組 (i,j) を選ぶ。コイン i とコイン j を裏返す。

操作によって N 枚のコイン全てを裏向きにできるか判定し、可能な場合必要な操作の回数の最小値を求めてください。

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

制約

  • 1 \leq T \leq 2\times 10^5
  • 3 \leq N \leq 2\times 10^5
  • S0, 1 からなる長さ N の文字列
  • 入力される数値は全て整数
  • 1 つの入力に含まれるテストケースについて、N の総和は 2\times 10^5 以下

入力

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

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

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

N
S

出力

T 行出力せよ。i\ (1\leq i \leq T) 行目には、 i 番目のテストケースについて、操作によりコインを全て裏向きにできる場合は必要な操作回数の最小値を、できない場合は -1 を出力せよ。


入力例 1

5
3
101
6
101101
5
11111
6
000000
30
111011100110101100101000000111

出力例 1

1
2
-1
0
8

1 番目のテストケースについては、(i,j)=(1,3) として操作を 1 回行うと、1 回の操作でコインを全て裏向きにできます。

2 番目のテストケースについては、(i,j)=(1,3) として操作を 1 回行い、(i,j)=(4,6) として操作を 1 回行うと、2 回の操作でコインを全て裏向きにできます。

3 番目のテストケースについては、コインを全て裏向きにできないことが証明できるので、-1 を出力してください。

4 番目のテストケースについては、コインは既に全て裏向きなので、操作は必要ありません。

Score : 400 points

Problem Statement

We have N coins numbered 1 to N with two distinguishable sides. A string S represents the current state of the coins. If the i-th character of S is 1, coin i is showing heads; if that character is 0, coin i is showing tails.

You can repeat the following operation zero or more times.

  • Choose a pair of integers (i,j) such that 1\leq i < j\leq N and j-i\geq \bm{2}. Flip coin i and coin j.

Determine whether it is possible to make all the N coins show tails. If it is possible, find the minimum number of operations needed.

You are given T test cases to solve.

Constraints

  • 1 \leq T \leq 2\times 10^5
  • 3 \leq N \leq 2\times 10^5
  • S is a string of length N consisting of 0 and 1.
  • All numbers in the input are integers.
  • For each input file, the sum of N over the test cases is at most 2\times 10^5.

Input

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

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

​ Each case is in the following format:

N
S

Output

Print T lines. The i-th line (1\leq i \leq T) should contain the minimum number of operations needed to make all the coins show tails if it is possible, and -1 otherwise.


Sample Input 1

5
3
101
6
101101
5
11111
6
000000
30
111011100110101100101000000111

Sample Output 1

1
2
-1
0
8

For the first test case, you can perform the operation with (i,j)=(1,3) to make all the coins show tails in one operation.

For the second test case, you can perform the operation with (i,j)=(1,3) and then with (i,j)=(4,6) to make all the coins show tails in two operations.

For the third test case, you can prove that there is no way to make all the coins show tails, so you should print -1.

For the fourth test case, the coins already show tails, so no operation is needed.

B - Mex on Blackboard

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

有限個の非負整数からなる多重集合 S にたいして、\mathrm{mex}(S) を、S に含まれない最小の非負整数と定義します。例えば、\mathrm{mex}(\lbrace 0,0, 1,3\rbrace ) = 2, \mathrm{mex}(\lbrace 1 \rbrace) = 0, \mathrm{mex}(\lbrace \rbrace) = 0 です。

黒板に N 個の非負整数が書かれており、i 番目の非負整数は A_i です。

あなたは、以下の操作をちょうど K 回行います。

  • 黒板に書かれている非負整数を 0 個以上選ぶ。選んだ非負整数からなる多重集合を S として、\mathrm{mex}(S) を黒板に 1 個書き込む。

最終的に黒板に書かれている非負整数の多重集合としてありうるものの個数を 998244353 で割ったあまりを求めてください。

制約

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

入力

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

N K
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

3 1
0 1 3

出力例 1

3

操作後に得られる多重集合は、以下の 3 通りです。

  • \lbrace 0,0,1,3 \rbrace
  • \lbrace 0,1,1,3\rbrace
  • \lbrace 0,1,2,3 \rbrace

例えば、\lbrace 0,1,1,3\rbrace は黒板に書かれている 0 を選び、S=\lbrace 0\rbrace として操作をすることで得られます。


入力例 2

2 1
0 0

出力例 2

2

操作後に得られる多重集合は、以下の 2 通りです。

  • \lbrace 0,0,0 \rbrace
  • \lbrace 0,0,1\rbrace

操作で選ぶ整数は 0 個でも良いことに注意してください。


入力例 3

5 10
3 1 4 1 5

出力例 3

7109

Score : 500 points

Problem Statement

For a finite multiset S of non-negative integers, let us define \mathrm{mex}(S) as the smallest non-negative integer not in S. For instance, \mathrm{mex}(\lbrace 0,0, 1,3\rbrace ) = 2, \mathrm{mex}(\lbrace 1 \rbrace) = 0, \mathrm{mex}(\lbrace \rbrace) = 0.

There are N non-negative integers on a blackboard. The i-th integer is A_i.

You will perform the following operation exactly K times.

  • Choose zero or more integers on the blackboard. Let S be the multiset of chosen integers, and write \mathrm{mex}(S) on the blackboard once.

How many multisets can be the multiset of integers on the final blackboard? Find this count modulo 998244353.

Constraints

  • 1 \leq N,K \leq 2\times 10^5
  • 0\leq A_i\leq 2\times 10^5
  • All numbers in the input are integers.

Input

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

N K
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

3 1
0 1 3

Sample Output 1

3

The following three multisets can be obtained by the operations.

  • \lbrace 0,0,1,3 \rbrace
  • \lbrace 0,1,1,3\rbrace
  • \lbrace 0,1,2,3 \rbrace

For instance, you can get \lbrace 0,1,1,3\rbrace by choosing the 0 on the blackboard to let S=\lbrace 0\rbrace in the operation.


Sample Input 2

2 1
0 0

Sample Output 2

2

The following two multisets can be obtained by the operations.

  • \lbrace 0,0,0 \rbrace
  • \lbrace 0,0,1\rbrace

Note that you may choose zero integers in the operation.


Sample Input 3

5 10
3 1 4 1 5

Sample Output 3

7109
C - Tree and LCS

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

頂点に 1 から N の番号がついた木 T があります。 Ti\ (1\leq i \leq N-1) 番目の辺は頂点 u_i と頂点 v_i を結んでいます。

T を用いて、(1,2,\ldots,N) の順列 P = (P_1,P_2,\ldots,P_N)類似度を以下で定めます。

  • T 上の任意の単純パス x=(x_1,x_2,\ldots,x_k) に対して、y=(P_{x_1}, P_{x_2},\ldots,P_{x_k}) とする。このとき、xy の最長共通部分列の長さとして考えられる最大値を類似度とする。

類似度が最小となるような順列 P を一つ構築してください。

部分列とは 数列の部分列とは、数列から 0 個以上の要素を取り除いた後、残りの要素を元の順序で連結して得られる数列のことをいいます。 例えば、(10,30)(10,20,30) の部分列ですが、(20,10)(10,20,30) の部分列ではありません。
単純パスとは グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_iv_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への ウォーク と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス (あるいは単に パス) と呼びます。

制約

  • 2 \leq N \leq 5000
  • 1\leq u_i,v_i\leq N
  • 与えられるグラフは木
  • 入力される数値は全て整数

入力

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

類似度が最小となるような順列 P を空白区切りで出力せよ。解が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

3
1 2
2 3

出力例 1

3 2 1

出力例の順列の類似度は 1 となっています。これは、以下のように計算できます。

  • x=(1) のとき y=(P_1)=(3) です。x,y の最長共通部分列の長さは 0 です。

  • x=(2) のとき y=(P_2)=(2) です。x,y の最長共通部分列の長さは 1 です。

  • x=(3) のとき y=(P_3)=(1) です。x,y の最長共通部分列の長さは 0 です。

  • x=(1,2) のとき y=(P_1,P_2)=(3,2) です。x,y の最長共通部分列の長さは 1 です。 これを反転した x=(2,1) についても同様です。

  • x=(2,3) のとき y=(P_2,P_3)=(2,1) です。x,y の最長共通部分列の長さは 1 です。 これを反転した x=(3,2) についても同様です。

  • x = (1,2,3) のとき y=(P_1,P_2,P_3)=(3,2, 1) です。x,y の最長共通部分列の長さは 1 です。これを反転した x=(3,2,1) についても同様です。

類似度が 0 以下の順列は存在しないことが証明できるので、これが答えとなります。


入力例 2

4
2 1
2 3
2 4

出力例 2

3 4 1 2

類似度が最小の順列が複数存在する場合、どれを出力してもよいです。例えば、4 3 2 1 といった出力も正解になります。

Score : 600 points

Problem Statement

We have a tree T with vertices numbered 1 to N. The i-th edge of T connects vertex u_i and vertex v_i.

Let us use T to define the similarity of a permutation P = (P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) as follows.

  • For a simple path x=(x_1,x_2,\ldots,x_k) in T, let y=(P_{x_1}, P_{x_2},\ldots,P_{x_k}). The similarity is the maximum possible length of a longest common subsequence of x and y.

Construct a permutation P with the minimum similarity.

What is a subsequence? A subsequence of a sequence is a sequence obtained by removing zero or more elements from that sequence and concatenating the remaining elements without changing the relative order. For instance, (10,30) is a subsequence of (10,20,30), but (20,10) is not.
What is a simple path? For vertices X and Y in a graph G, a walk from X to Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and there is an edge connecting v_i and v_{i+1}. A simple path (or simply a path) is a walk such that v_1,v_2, \ldots, v_k are all different.

Constraints

  • 2 \leq N \leq 5000
  • 1\leq u_i,v_i\leq N
  • The given graph is a tree.
  • All numbers in the input are integers.

Input

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print a permutation P with the minimum similarity, separated by spaces. If multiple solutions exist, you may print any of them.


Sample Input 1

3
1 2
2 3

Sample Output 1

3 2 1

This permutation has a similarity of 1, which can be computed as follows.

  • For x=(1), we have y=(P_1)=(3). The length of a longest common subsequence of x and y is 0.

  • For x=(2), we have y=(P_2)=(2). The length of a longest common subsequence of x and y is 1.

  • For x=(3), we have y=(P_2)=(1). The length of a longest common subsequence of x and y is 0.

  • For x=(1,2), we have y=(P_1,P_2)=(3,2). The length of a longest common subsequence of x and y is 1. The same goes for x=(2,1), the reversal of (1,2).

  • For x=(2,3), we have y=(P_2,P_3)=(2,1). The length of a longest common subsequence of x and y is 1. The same goes for x=(3,2), the reversal of (2,3).

  • For x=(1,2,3), we have y=(P_1,P_2,P_3)=(3,2,1). The length of a longest common subsequence of x and y is 1. The same goes for x=(3,2,1), the reversal of (1,2,3).

We can prove that no permutation has a similarity of 0 or less, so this permutation is a valid answer.


Sample Input 2

4
2 1
2 3
2 4

Sample Output 2

3 4 1 2

If multiple permutations have the minimum similarity, you may print any of them. For instance, you may also print 4 3 2 1.

D - Xor Sum 5

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

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

1 \leq X_i \leq N\ (1\leq i \leq K) を満たす長さ K の正整数列 X=(X_1,X_2,\dots,X_K)N^K 通り考えられますが、それらすべてに対する \displaystyle \sum_{i=1}^{K} A_{X_i} のビット単位 \mathrm{XOR} を求めてください。

ビット単位 \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)。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR}(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。

制約

  • 1 \leq N \leq 1000
  • 1 \leq K \leq 10^{12}
  • 0 \leq A_i \leq 1000
  • 与えられる入力はすべて整数

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

2 2
10 30

出力例 1

40

X として考えられるのは (X_1,X_2)=(1,1),(1,2),(2,1),(2,2)4 通りであり、それぞれに対する A_{X_1}+A_{X_2}20,40,40,60 です。よって答えは 20 \oplus 40 \oplus 40 \oplus 60=40 となります。


入力例 2

4 10
0 0 0 0

出力例 2

0

入力例 3

11 998244353
314 159 265 358 979 323 846 264 338 327 950

出力例 3

236500026047

Score : 700 points

Problem Statement

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

Find the bitwise \mathrm{XOR} of \displaystyle \sum_{i=1}^{K} A_{X_i} over all N^K sequences of K positive integer sequences X=(X_1,X_2,\dots,X_K) such that 1 \leq X_i \leq N\ (1\leq i \leq K).

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of non-negative integers A and B, A \oplus B, is defined as follows:

  • When A \oplus B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of the digits in that place of A and B is 1, and 0 otherwise.
For example, we have 3 \oplus 5 = 6 (in base two: 011 \oplus 101 = 110).
Generally, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k). We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots, p_k.

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq K \leq 10^{12}
  • 0 \leq A_i \leq 1000
  • All values in the input are integers.

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

2 2
10 30

Sample Output 1

40

There are four sequences to consider: (X_1,X_2)=(1,1),(1,2),(2,1),(2,2), for which A_{X_1}+A_{X_2} is 20,40,40,60, respectively. Thus, the answer is 20 \oplus 40 \oplus 40 \oplus 60=40.


Sample Input 2

4 10
0 0 0 0

Sample Output 2

0

Sample Input 3

11 998244353
314 159 265 358 979 323 846 264 338 327 950

Sample Output 3

236500026047
E - Non-Adjacent Matching

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 800

問題文

長さが N、各要素が 0 以上 M 以下、総和が K 以下の整数列のうち、良い数列 の個数を 998244353 で割ったあまりを求めてください。

ここで、長さ N の数列 X=(X_1,X_2,\ldots,X_N) は以下の条件を全て満たすグラフ G が存在するとき、かつ、そのときに限り良い数列です。

  • G1 から N の番号がついた N 頂点からなる、自己ループを持たないグラフである。(多重辺はあってもよい。)
  • i\ (1\leq i \leq N) について、頂点 i の次数は X_i である。
  • i\ (1\leq i \leq N) について、頂点 i と頂点 i+1 を結ぶ辺は存在しない。ここで、頂点 N+1 は頂点 1 を意味する。

制約

  • 4 \leq N \leq 3000
  • 0 \leq M \leq 3000
  • 0\leq K \leq NM
  • 入力される数値は全て整数

入力

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

N M K

出力

答えを出力せよ。


入力例 1

4 1 2

出力例 1

3

条件を満たす良い数列は以下の 3 個です。

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

入力例 2

10 0 0

出力例 2

1

入力例 3

314 159 26535

出力例 3

248950743

998244353 で割ったあまりを答えてください。

Score : 800 points

Problem Statement

Find the number, modulo 998244353, of good sequences of length N whose elements are integers between 0 and M, inclusive, and whose sum is at most K.

Here, a length-N sequence X=(X_1,X_2,\ldots,X_N) is said to be good if and only if there is a graph G that satisfies all of the following conditions.

  • G is a graph with N vertices numbered 1 to N without self-loops. (It may have multi-edges.)
  • For each i\ (1\leq i \leq N), the degree of vertex i is X_i.
  • For each i\ (1\leq i \leq N), no edge connects vertex i and vertex i+1. Here, vertex N+1 means vertex 1.

Constraints

  • 4 \leq N \leq 3000
  • 0 \leq M \leq 3000
  • 0\leq K \leq NM
  • All numbers in the input are integers.

Input

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

N M K

Output

Print the answer.


Sample Input 1

4 1 2

Sample Output 1

3

The following three sequences are good.

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

Sample Input 2

10 0 0

Sample Output 2

1

Sample Input 3

314 159 26535

Sample Output 3

248950743

Print the count modulo 998244353.

F - Make Same Set

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N),C=(C_1,C_2,\dots,C_N) が与えられます。

整数からなる集合のうち、以下の条件を満たすものを 1 つ求めてください。

  • 空集合に対し i=1,2,\dots,N の順に A_i,B_i のいずれかを追加していくことで得られる集合である。
  • 空集合に対し i=1,2,\dots,N の順に A_i,C_i のいずれかを追加していくことで得られる集合である。
  • 上記の 2 つの条件を満たす集合の中で、要素数が最大である。

制約

  • 1 \leq N \leq 5000
  • 1 \leq A_i,B_i,C_i \leq 10000
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
C_1 C_2 \dots C_N

出力

条件を満たす整数集合の要素数 k と、整数集合の k 個の要素 x_i\ (1\leq i \leq k) を以下の形式で出力せよ。

k
x_1 x_2 \dots x_k

条件を満たす整数集合が複数存在する場合、いずれを出力してもかまわない。


入力例 1

3
1 1 1
2 3 4
5 4 2

出力例 1

3
4 1 2

集合 \lbrace 1,2,4\rbrace

  • 1 番目の条件について、空集合に B_1,A_2,B_3 を追加することで得られます。
  • 2 番目の条件について、空集合に A_1,C_2,C_3 を追加することで得られます。

条件を満たす集合の要素数は明らかに N=3 以下であるため、この集合は 3 番目の条件も満たしています。


入力例 2

15
1 1 15 11 13 7 7 1 6 1 5 7 4 9 8
11 30 1 18 16 15 19 17 3 27 22 7 21 29 9
24 14 23 17 18 16 9 12 10 5 26 29 20 19 11

出力例 2

12
7 9 11 17 19 1 15 4 5 6 29 13

Score : 1000 points

Problem Statement

You are given three integer sequences of length N: A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N),C=(C_1,C_2,\dots,C_N).

Find one set of integers that satisfies the following conditions.

  • It can be obtained as follows: start with an empty set, and for each i=1,2,\dots,N in this order, add A_i or B_i to the set.
  • It can be obtained as follows: start with an empty set, and for each i=1,2,\dots,N in this order, add A_i or C_i to the set.
  • It has the maximum number of elements among the sets that satisfy the two conditions above.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq A_i,B_i,C_i \leq 10000
  • All values in the input are integers.

Input

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
C_1 C_2 \dots C_N

Output

Print k, the number of elements in the set of integers satisfying the conditions, and x_i\ (1\leq i \leq k), the k elements of the set, in the following format:

k
x_1 x_2 \dots x_k

If multiple sets satisfy the conditions, you may print any of them.


Sample Input 1

3
1 1 1
2 3 4
5 4 2

Sample Output 1

3
4 1 2

For the set \lbrace 1,2,4\rbrace, we have the following.

  • The first condition is satisfied because you can add B_1,A_2,B_3 to an empty set to obtain this set.
  • The second condition is satisfied because you can add A_1,C_2,C_3 to an empty set to obtain this set.

Clearly, any set satisfying these conditions has at most N=3 elements, so this set also satisfies the third condition.


Sample Input 2

15
1 1 15 11 13 7 7 1 6 1 5 7 4 9 8
11 30 1 18 16 15 19 17 3 27 22 7 21 29 9
24 14 23 17 18 16 9 12 10 5 26 29 20 19 11

Sample Output 2

12
7 9 11 17 19 1 15 4 5 6 29 13