A - Long Common Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

3 つの 01 文字列 S_1, S_2, S_3 が与えられます。これらはそれぞれ、01N 個ずつ含みます。

長さ 2N+101 文字列であって、S_1 + S_1, S_2 + S_2, S_3 + S_3 のいずれの部分列でもあるものを 1 つ求めてください(s+t は文字列 s, t をこの順に連結したものを表します)。この問題の制約の下では、そのような文字列が常に存在することが保証されます。

ここで、文字列 B が文字列 A の部分列であるとは、A から 0 文字以上を取り除き、残りの文字を順番を変えずに連結することで B を得ることができることを意味します。

テストケースは T 個与えられるので、それぞれを解いてください。

制約

  • 1 \le T \le 10^5
  • 1\le N \le 10^5
  • S_i01N 個ずつ含む 01 文字列である。
  • 全テストケースにおける N の総和は 10^5 以下である。

入力

入力は以下の形式で標準入力から与えられる。 入力の 1 行目は次の通りである。

T

そして、それぞれ以下の形式で T 個のテストケースが続く。

N
S_1
S_2
S_3

出力

各テストケースについて、長さ 2N+101 文字列であって、S_1 + S_1, S_2 + S_2, S_3 + S_3 のいずれの部分列でもあるようなものをいずれか 1 つ出力せよ。 そのような文字列が複数存在するときは、いずれを出力してもよい。


入力例 1

2
1
01
01
10
2
0101
0011
1100

出力例 1

010
11011

1 個目のケースでは、0100101, 0101, 1010 の部分列です。

2 個目のケースでは、1101101010101, 00110011, 11001100 の部分列です。

Score : 400 points

Problem Statement

You are given 3 binary strings S_1, S_2, S_3, each containing exactly N 0's and N 1's.

Find a binary string of length 2N+1, which is a subsequence of all of the strings S_1 + S_1, S_2 + S_2, S_3 + S_3 (s+t denotes the concatenation of strings s and t in order). It's guaranteed that under the constraints above such a string always exists.

Here a string B is a subsequence of a string A if B can be obtained by removing zero or more characters from A and concatenating the remaining characters without changing the order.

You will be given T test cases. Solve each of them.

Constraints

  • 1 \le T \le 10^5
  • 1\le N \le 10^5
  • S_i is a binary string of length 2N consisting of N 0's and N 1's.
  • The sum of N over all test cases doesn't exceed 10^5.

Input

Input is given from Standard Input in the following format. The first line of input is as follows:

T

Then, T test cases follow, each of which is in the following format:

N
S_1
S_2
S_3

Output

For each test case, print any binary string of length 2N+1, which is a subsequence of all of S_1 + S_1, S_2 + S_2, S_3 + S_3. If many such strings exist, you can print any.


Sample Input 1

2
1
01
01
10
2
0101
0011
1100

Sample Output 1

010
11011

In the first case, 010 is a subsequence of 0101, 0101, and 1010.

In the second case, 11011 is a subsequence of 01010101, 00110011, and 11001100.

B - Tree Edges XOR

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

N 頂点の木が与えられます。ここで、N奇数 です。 木の頂点には 1 から N までの、辺には 1 から N-1 までの番号が付けられています。 辺 i は頂点 u_i, v_i を結び、初期状態での重みは w^1_i です。

あなたは、次の操作を何度でも行えます。

  • 木から辺 (u, v) を選ぶ。この辺の現在の重みが w であるとする。u, v のいずれかちょうど一方に接続する各辺について、その重みを w との XOR に置き換える(操作前の辺の重みが w_1 であるとすると、操作後の重みは w_1 \oplus w となる)。

あなたの目標は、各辺 i の重みを w^2_i とすることです。 上記の操作を何度でも行えるとして、目標の達成が可能か判定してください。

制約

  • 1 \le N \le 10^5
  • N は奇数である。
  • 1\le u_i, v_i \le N
  • u_i \neq v_i
  • 0\le w^1_i, w^2_i < 2^{30}
  • 入力中の値は全て整数である。
  • 入力が表すグラフは木である。

入力

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

N
u_1 v_1 w^1_1 w^2_1
u_2 v_2 w^1_2 w^2_2
\vdots
u_{N-1} v_{N-1} w^1_{N-1} w^2_{N-1}

出力

目標とする重みの割り当てに初期状態から至ることが可能であれば YES、そうでなければ NO と出力せよ。 なお、正誤判定器は英大文字と英小文字を区別せず、どちらも受理する。


入力例 1

3
1 2 1 1
2 3 8 9

出力例 1

YES

1 に対して操作を行うと、辺 2 の重みが 8 \oplus 1=9 となります。


入力例 2

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

出力例 2

NO

Score : 800 points

Problem Statement

You are given a tree with N vertices, where N is odd. The vertices are numbered from 1 through N and edges from 1 through N-1. The edge i connects vertices u_i and v_i and its initial weight is w^1_i.

You can perform the following operation any number of times:

  • Choose an edge (u, v) of the tree, and suppose that its weight now is w. For every edge, which is incident to exactly one of u and v, we XOR the weight of that edge with w (if it was w_1, replace it with w_1 \oplus w).

Your goal is to reach the state where each edge i has weight w^2_i. Determine if you can get to the goal by performing the operation above any number of times.

Constraints

  • 1 \le N \le 10^5
  • N is odd.
  • 1\le u_i, v_i \le N
  • u_i \neq v_i
  • 0\le w^1_i, w^2_i < 2^{30}
  • All values in input are integers.
  • The input represents a valid tree.

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w^1_1 w^2_1
u_2 v_2 w^1_2 w^2_2
\vdots
u_{N-1} v_{N-1} w^1_{N-1} w^2_{N-1}

Output

If you can get the goal weight assignment from the initial state, output YES. Otherwise, output NO. Note that the checker is case-insensitive: you can print letters in both uppercase and lowercase.


Sample Input 1

3
1 2 1 1
2 3 8 9

Sample Output 1

YES

If you perform the operation on the edge 1, the weight of the edge 2 becomes 8 \oplus 1=9.


Sample Input 2

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

Sample Output 2

NO
C - Nondivisible Prefix Sums

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

素数 P が与えられます。これはあなたの嫌いな数です。

整数の列 A_1, A_2, \dots, A_N について、どの接頭辞の和も P で割り切れないように要素を並べ替えることができるとき、この列を 良い 列と呼びます(すなわち、並べ替えのあと、1 \le i \le N かつ A_1 + A_2 + \dots + A_i \equiv 0 \bmod P であるような i存在してはいけません)。

各要素が 1 以上 P-1 以下であるような長さ N の整数列は全部で (P-1)^N 通りありますが、このうち 良い 列はいくつでしょうか。

答えは非常に大きい可能性があるため、これを 998244353 で割った余りを出力してください。

制約

  • 1 \le N \le 5000
  • 2 \le P \le 10^8
  • P は素数である。

入力

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

N P

出力

良い 列の個数を 998244353 で割った余りを出力せよ。


入力例 1

2 5

出力例 1

12

良い列は [1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 4], [3, 1], [3, 3], [3, 4], [4, 2], [4, 3], [4, 4]12 通りです。


入力例 2

4 3

出力例 2

8

良い列は [1, 1, 1, 2], [1, 1, 2, 1], [1, 2, 1, 1], [2, 1, 1, 1], [2, 2, 2, 1], [2, 2, 1, 2], [2, 1, 2, 2], [1, 2, 2, 2]8 通りです。


入力例 3

5000 99999989

出力例 3

51699346

入力例 4

2021 307

出力例 4

644635349

Score : 1000 points

Problem Statement

You are given a prime number P, which you don't like.

Let's call an array of integers A_1, A_2, \dots, A_N good, if it is possible to reorder the elements in such a way that no prefix sum is divisible by P (that is, there is no i with 1 \le i \le N and A_1 + A_2 + \dots + A_i \equiv 0 \bmod P after the reordering).

Consider all (P-1)^N arrays of length N with elements from 1 to P-1. How many of them are good?

As this number can be very big, output it modulo 998244353.

Constraints

  • 1 \le N \le 5000
  • 2 \le P \le 10^8
  • P is a prime.

Input

Input is given from Standard Input in the following format:

N P

Output

Output the number of good arrays modulo 998244353.


Sample Input 1

2 5

Sample Output 1

12

There are 12 good arrays: [1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 4], [3, 1], [3, 3], [3, 4], [4, 2], [4, 3], [4, 4].


Sample Input 2

4 3

Sample Output 2

8

There are 8 good arrays: [1, 1, 1, 2], [1, 1, 2, 1], [1, 2, 1, 1], [2, 1, 1, 1], [2, 2, 2, 1], [2, 2, 1, 2], [2, 1, 2, 2], [1, 2, 2, 2].


Sample Input 3

5000 99999989

Sample Output 3

51699346

Sample Input 4

2021 307

Sample Output 4

644635349
D - Equal LIS

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

1 から N までの整数を並べ替えた列 P_1, P_2, \dots, P_N が与えられます。 これを、最長増加部分列の長さが等しいような 2 つの部分列に分けることは可能でしょうか。

形式的に述べると、目標は以下の条件を全て満たす 2 つの整数列 a, b を得ることです。

  • a, b はともに P の部分列である。
  • 各整数 i=1,2,\cdots,Na, b のいずれかちょうど一方に現れる。
  • (a の最長増加部分列の長さ) = (b の最長増加部分列の長さ)

目標が達成可能か判定してください。

テストケースは T 個与えられるので、それぞれを解いてください。

制約

  • 1 \le T \le 2 \cdot 10^5
  • 1 \le N \le 2 \cdot 10^5
  • P_1, P_2, \dots, P_N1 から N までの整数を並べ替えた列である。
  • 全テストケースにおける N の総和は 2 \cdot 10^5 以下である。

入力

入力は以下の形式で標準入力から与えられる。 入力の 1 行目は次の通りである。

T

そして、それぞれ以下の形式で T 個のテストケースが続く。

N
P_1 P_2 \dots P_N

出力

各テストケースについて、与えられた並びを最長増加部分列の長さが等しいような 2 つの部分列に分けることが可能なら YES、そうでなければ NO と出力せよ。 1 行につき 1 個のテストケースへの出力を行え。 なお、正誤判定器は英大文字と英小文字を区別せず、どちらも受理する。


入力例 1

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

出力例 1

NO
YES
NO
YES
NO

[1, 3, 5, 2, 4][1, 5][3, 2, 4] に分けると、両者とも最長増加部分列の長さが 2 となります。

[2, 3, 4, 5, 6, 1] については、最長増加部分列の長さが等しいような 2 つの部分列に分けることは不可能であることが示せます。

Score : 1000 points

Problem Statement

You are given a permutation P_1, P_2, \dots, P_N of integers from 1 to N. Is it possible to split it into 2 subsequences, which have equal length of the Longest Increasing Subsequence?

Formally speaking, your goal is to get two integer sequences a and b such that:

  • Both a and b are subsequences of P.
  • Each integer i=1,2,\cdots,N appears in exactly one of a and b.
  • (The length of a longest increasing subsequence of a) = (The length of a longest increasing subsequence of b)

Determine if the objective is achievable.

You will be given T test cases. Solve each of them.

Constraints

  • 1 \le T \le 2 \cdot 10^5
  • 1 \le N \le 2 \cdot 10^5
  • P_1, P_2, \dots, P_N is a permutation of integers from 1 to N.
  • The sum of N over all test cases doesn't exceed 2 \cdot 10^5.

Input

Input is given from Standard Input in the following format. The first line of input is as follows:

T

Then, T test cases follow, each of which is in the following format:

N
P_1 P_2 \dots P_N

Output

For each test case, print YES if it is possible to split the permutation into 2 subsequences, which have equal length of the Longest Increasing Subsequence, and print NO otherwise. Use one line for each case. Note that the checker is case-insensitive: you can print letters in both uppercase and lowercase.


Sample Input 1

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

Sample Output 1

NO
YES
NO
YES
NO

[1, 3, 5, 2, 4] can be split into [1, 5] and [3, 2, 4], both have LIS of length 2.

It can be shown that there is no way to split [2, 3, 4, 5, 6, 1] into 2 subsequences with an equal length of LIS.

E - 3 Letters

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1500

問題文

A, B, C からなる文字列は、どの連続する 2 文字も異なるとき、良い 文字列であると呼ばれます。例えば、ABABABABC は良い文字列であり、ABBAAABBCC は良い文字列ではありません。

2 つの長さ N良い 文字列 S, T が与えられます。 1 回の操作で、あなたは S から任意の 1 文字を選び、A, B, C のいずれかであるような別の文字に変えることができます。ただし、操作後も S良い 文字列でなければなりません。

ST に変化させるには、最小で何回の操作が必要でしょうか。 なお、これは必ず有限回の操作で可能であることが証明できます。

制約

  • 1\le N \le 5\cdot 10^5
  • SA, B, C からなる長さ N良い 文字列である。
  • TA, B, C からなる長さ N良い 文字列である。

入力

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

N
S
T

出力

ST に変化させるために必要な最小の操作回数を出力せよ。


入力例 1

4
CABC
CBAC

出力例 1

6

6 回の操作で目標を達成する例を以下に示します。

CABC \to BABC \to BCBC \to BCAC \to ACAC \to ABAC \to CBAC

この場合には、少なくとも 6 回の操作が必要であることが示せます。


入力例 2

10
ABABABABAB
BABABABABA

出力例 2

15

Score : 1500 points

Problem Statement

A string, consisting of letters A, B, and C, is called good, if every two consecutive letters are different. For example, strings ABABAB and ABC are good, while ABBA and AABBCC aren't.

You are given two good strings S and T of length N. In one operation, you can choose any letter of S, and change it to another letter among A, B, and C, so that S remains good.

What's the smallest number of operations needed to transform S into T? We can prove that it can always be done in a finite number of operations.

Constraints

  • 1\le N \le 5\cdot 10^5
  • S is a good string of length N consisting of A, B, and C
  • T is a good string of length N consisting of A, B, and C

Input

Input is given from Standard Input in the following format:

N
S
T

Output

Output the smallest number of operations needed to transform S into T.


Sample Input 1

4
CABC
CBAC

Sample Output 1

6

Here is one possible sequence of operations of length 6:

CABC \to BABC \to BCBC \to BCAC \to ACAC \to ABAC \to CBAC

It can be shown that 6 operations are necessary for this case.


Sample Input 2

10
ABABABABAB
BABABABABA

Sample Output 2

15
F - Tree Vertices XOR

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 2000

問題文

N 頂点の木が与えられます。 木の頂点には 1 から N までの番号が付けられており、i 番目の辺は頂点 u_i, v_i を結びます。

はじめ、木の各頂点には 1 という数が書かれています。

1 回の操作で、あなたは以下を行えます。

  • 木から頂点 v を選ぶ。v に隣接する頂点全てに書き込まれた値の XORX であるとする。v に書かれた値が a_v であるとして、この値を (a_v\ \mathrm{XOR}\ X) に置き換える。

この操作を任意の有限回(0 回も含む)行えるとして、得られる木の形態は何通りあるでしょうか。この数は大きい可能性があるため、これを 998244353 で割った余りを出力してください。

ここで、木の 2 つの形態は、書かれた数が異なるような頂点 v が存在するときに異なるとみなされます。

制約

  • 3 \le N \le 2\cdot 10^5
  • 1\le u_i, v_i \le N
  • u_i \neq v_i
  • 入力が表すグラフは木である。

入力

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

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

出力

初期状態から得られる木の形態の個数を 998244353 で割った余りを出力せよ。


入力例 1

4
1 2
2 3
3 4

出力例 1

10

頂点 1,2,3,4a,b,c,d が書かれている形態を abcd と表すと、到達可能な形態は 1111, 1110, 1100, 1000, 0111, 0110, 0100, 0011, 0010, 0001 です。


入力例 2

5
1 2
1 3
1 4
1 5

出力例 2

24

入力例 3

6
1 3
2 3
3 4
4 5
4 6

出力例 3

40

入力例 4

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

出力例 4

255

Score : 2000 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered from 1 to N, and the i-th edge connects vertices u_i and v_i.

Initially, on each vertex of the tree is written number 1.

In one operation, you can do the following:

  • Choose any vertex v of the tree. Let X be the XOR of the values written in the neighbors of v. Then, if the number written on v is a_v, replace it by (a_v\ \mathrm{XOR}\ X).

You can perform this operation any finite (maybe zero) number of times. How many configurations can you achieve? As this number can be large, output it modulo 998244353.

Two configurations are called different if there exists a vertex v, such that the numbers written in v in these configurations are different.

Constraints

  • 3 \le N \le 2\cdot 10^5
  • 1\le u_i, v_i \le N
  • u_i \neq v_i
  • The input represents a valid tree.

Input

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

Output the number of configurations you can get from the initial state, modulo 998244353.


Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

10

We can get the following configurations (abcd means vertices 1,2,3,4 have a,b,c,d written on them): 1111, 1110, 1100, 1000, 0111, 0110, 0100, 0011, 0010, 0001.


Sample Input 2

5
1 2
1 3
1 4
1 5

Sample Output 2

24

Sample Input 3

6
1 3
2 3
3 4
4 5
4 6

Sample Output 3

40

Sample Input 4

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

Sample Output 4

255