A - Schedule Optimization

Time Limit: 2.5 sec / Memory Limit: 2048 MiB

配点 : 900

問題文

高橋君は全 10^9 日間からなるトーナメント形式の大会を開くことにしました。
選手は 2^N 人いて、それぞれ選手 1\ldots、選手 2^N と呼ばれます。 選手 i は大会の l_i 日目から r_i 日目までの r_i-l_i+1 日間参加する予定です。

まず、大会の流れを述べます。1 \leq i \leq N, 1 \leq j \leq 2^{N-i} を満たす整数組 (i,j)2^N-1 通りありますが、それらと大会中の試合が一対一で対応します。(i,j) に対応する試合では以下に述べる 2 人の選手が対戦して勝者と敗者を決めます。

  • i=1 の場合、選手 2j-1 と選手 2j
  • i \geq 2 の場合、(i-1, 2j-1) に対応する試合の勝者と (i-1,2j) に対応する試合の勝者

各試合は、対戦することになる 2 人を決める為に必要な試合すべてが完了していて、かつその 2 人が大会に参加中ならばただちに完了させられます。特に、一人の選手が同日に複数の試合を行うことも可能です。
(N, 1) に対応する試合は決勝戦と呼ばれ、これを完了させるのが大会の目的です。

高橋君は決勝戦を完了させて大会を成功させるために、以下の工作を行うことにしました。

  • 審判に指示を出し、各試合の勝者を都合よく決める。
  • 各選手にお金を払い、参加する日程を変えてもらう。選手 il'_i 日目から r'_i 日目まで参加してもらう場合、|l_i-l'_i|+|r_i-r'_i| 円を支払う必要がある。ここで、l'_i, r'_i1\leq l'_i \leq r'_i \leq 10^9 を満たす整数である。

高橋君が選手たちに支払う必要のある金額の総和の最小値を求めてください。

制約

  • 1 \leq N \leq 18
  • 1 \leq l_i \leq r_i \leq 10^9
  • 入力はすべて整数

入力

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

N
l_1 r_1
\vdots
l_{2^N} r_{2^N}

出力

支払う必要のある金額の総和の最小値を X 円とする。X を出力せよ。


入力例 1

3
1 4
1 3
3 4
2 2
3 4
4 4
2 3
3 4

出力例 1

1

選手 41 円を払って (l'_4, r'_4)=(2,3) に変え、他の選手の日程は変えないことにします。すると、例えば以下のようにして決勝戦を完了させることができます。

  1. 1 日目に (1,1) に対応する試合(選手 1 対選手 2 )を行い、選手 2 を勝たせる。
  2. 3 日目に (1,2) に対応する試合(選手 3 対選手 4 )を行い、選手 3 を勝たせる。
  3. 3 日目に (2,1) に対応する試合(選手 2 対選手 3 )を行い、選手 3 を勝たせる。
  4. 3 日目に (1,4) に対応する試合(選手 7 対選手 8 )を行い、選手 8 を勝たせる。
  5. 4 日目に (1,3) に対応する試合(選手 5 対選手 6 )を行い、選手 5 を勝たせる。
  6. 4 日目に (2,2) に対応する試合(選手 5 対選手 8 )を行い、選手 8 を勝たせる。
  7. 4 日目に (3,1) に対応する試合(選手 3 対選手 8 )を行い、選手 8 を勝たせる。

一方、1 円未満の支払いで決勝戦を完了させることはできません。そのため、1 が期待される出力です。


入力例 2

1
1 1
1000000000 1000000000

出力例 2

999999999

入力例 3

4
158260522 877914575
24979445 602436426
623690081 861648772
433933447 476190629
211047202 262703497
628894325 971407775
731963982 822804784
430302156 450968417
161735902 982631932
880895728 923078537
189330739 707723857
802329211 910286918
303238506 404539679
317063340 492686568
125660016 773361868
650287940 839296263

出力例 3

1088492036

Score : 900 points

Problem Statement

Takahashi decided to hold a tournament that lasts for 10^9 days in a single-elimination format.
There are 2^N players, called player 1, \ldots, player 2^N. Player i plans to participate from day l_i to day r_i of the tournament, for a total of r_i - l_i + 1 days.

First, we describe the flow of the tournament. There are 2^N - 1 matches in total, corresponding one-to-one with the integer pairs (i, j) satisfying 1 \leq i \leq N and 1 \leq j \leq 2^{N - i}. In the match corresponding to (i, j), the following two players compete to decide the winner and the loser:

  • If i = 1, players 2j - 1 and 2j
  • If i \geq 2, the winners of the matches corresponding to (i - 1, 2j - 1) and (i - 1, 2j)

Each match can be completed immediately when all the matches necessary to determine the two players who will compete in it have been completed, and both players are currently participating in the tournament. In particular, a single player can participate in multiple matches on the same day.
The match corresponding to (N, 1) is called the final, and the goal of the tournament is to complete it.

To successfully complete the final, Takahashi decided to perform the following manipulations:

  • Instruct the referees to fix the outcomes of the matches as desired.
  • Pay the players to change their participation schedules. To make player i participate from day l'_i to day r'_i, Takahashi needs to pay |l_i - l'_i| + |r_i - r'_i| yen. Here, l'_i and r'_i are integers satisfying 1 \leq l'_i \leq r'_i \leq 10^9.

Find the minimum total amount of money Takahashi needs to pay the players.

Constraints

  • 1 \leq N \leq 18
  • 1 \leq l_i \leq r_i \leq 10^9
  • All input values are integers.

Input

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

N
l_1 r_1
\vdots
l_{2^N} r_{2^N}

Output

Let X be the minimum total amount of money Takahashi needs to pay. Print X.


Sample Input 1

3
1 4
1 3
3 4
2 2
3 4
4 4
2 3
3 4

Sample Output 1

1

Assume that Takahashi pays 1 yen to player 4 to change his participation schedule to (l'_4, r'_4) = (2, 3), and not change the schedules of the other players. Then, for example, the final can be completed as follows:

  1. On Day 1, conduct the match corresponding to (1, 1) (player 1 vs. player 2), and let player 2 win.
  2. On Day 3, conduct the match corresponding to (1, 2) (player 3 vs. player 4), and let player 3 win.
  3. On Day 3, conduct the match corresponding to (2, 1) (player 2 vs. player 3), and let player 3 win.
  4. On Day 3, conduct the match corresponding to (1, 4) (player 7 vs. player 8), and let player 8 win.
  5. On Day 4, conduct the match corresponding to (1, 3) (player 5 vs. player 6), and let player 5 win.
  6. On Day 4, conduct the match corresponding to (2, 2) (player 5 vs. player 8), and let player 8 win.
  7. On Day 4, conduct the match corresponding to (3, 1) (player 3 vs. player 8), and let player 8 win.

On the other hand, it is impossible to complete the final with less than 1 yen of payments. Therefore, the expected output is 1.


Sample Input 2

1
1 1
1000000000 1000000000

Sample Output 2

999999999

Sample Input 3

4
158260522 877914575
24979445 602436426
623690081 861648772
433933447 476190629
211047202 262703497
628894325 971407775
731963982 822804784
430302156 450968417
161735902 982631932
880895728 923078537
189330739 707723857
802329211 910286918
303238506 404539679
317063340 492686568
125660016 773361868
650287940 839296263

Sample Output 3

1088492036
B - Pair Guessing

Time Limit: 2 sec / Memory Limit: 2048 MiB

配点 : 1000

問題文

N 個の、長さ N01 文字列 S_1,\ldots,S_N が与えられます。S_ij 文字目を S_{i,j} と表します。ここで、S_{i,j}=\ 1 を満たす整数組 (i,j) が少なくとも一つ存在することが制約より保証されています。

高橋君と青木君が以下のようなゲームを行います。

  1. 高橋君が 1 \leq i,j \leq N, S_{i,j}=\ 1 を満たす整数組 (i,j)1 つ選ぶ。
  2. 0 回以上 N 回以下、青木君が高橋君に質問を行う。各質問では青木君が 1\leq i',j' \leq N を満たす整数組 (i',j') を選び、「i=i'j=j' のうち少なくとも一方が成り立つ」の真偽を高橋君から教えてもらう。
  3. 青木君が (i,j) を予想する。予想が当たっていれば青木君の勝ち、そうでなければ負けとなる。

青木君は高橋君が選ぶ (i,j) の候補、すなわち S_1,\ldots,S_N を知った状態でゲームを行います。また、上記2では以前の質問に対する返事を聞いたうえで (i', j') を選ぶことができます。

青木君が適切な戦略を取った場合、高橋君の (i,j) の選び方や運によらず必ずゲームに勝てるかどうかを判定してください。

1 つの入力につきテストケースは T 個あります。

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 500
  • 1 つの入力の中のテストケースすべてにわたる N^2 の総和は 500^2 以下である
  • S_i は長さ N01 文字列
  • S_{i,j}=\ 1 を満たす整数組 (i,j) が少なくとも一つ存在する

入力

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

T
case_1
\vdots
case_T

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

N
S_1
\vdots
S_N

出力

各テストケースに対し、青木君が必ずゲームに勝てるならば Yes と、そうでなければ No と出力せよ。


入力例 1

3
2
01
11
2
11
11
10
0101011110
1100100001
1101100000
0111101010
1000011001
1110101010
1110110100
1110000110
0000001011
1001111100

出力例 1

Yes
No
Yes

1 番目のテストケースに対するゲームの一例を以下に示します。

  1. 高橋君が S_{i,j}=\ 1 を満たす (i,j) として (2,2) を選ぶ。
  2. 青木君が 2 回質問を行う。1 回目の質問では (i',j')=(1,1) として、高橋君から「i=1j=1 のうち少なくとも一方が成り立つ」が偽であると教えてもらう。2 回目の質問では (i',j')=(2,2) として、高橋君から「i=2j=2 の少なくとも一方が成り立つ」が真であると教えてもらう。
  3. 青木君が (i,j)=(2,2) と予想する。この予想は当たっているため、青木君の勝ちである。

これはあくまでゲームの一例であり、青木君の戦略が適切とは限りません。しかし、青木君が適切な戦略を取った場合には必ず青木君がゲームに勝つため、1 番目のテストケースに対する出力は Yes になります。

Score : 1000 points

Problem Statement

You are given N strings S_1, \ldots, S_N, each of length N, consisting only of 0 and 1. Let S_{i,j} denote the j-th character of S_i. It is guaranteed by the constraints that there exists at least one integer pair (i, j) satisfying S_{i,j} = 1.

Takahashi and Aoki play the following game:

  1. Takahashi chooses one integer pair (i, j) satisfying 1 \leq i, j \leq N and S_{i,j} = 1.
  2. Aoki asks Takahashi at least 0 and at most N questions. In each question, Aoki chooses an integer pair (i', j') satisfying 1 \leq i', j' \leq N, and Takahashi tells Aoki whether "At least one of i = i' or j = j' holds" is true or false.
  3. Aoki guesses (i, j). If his guess is correct, Aoki wins; otherwise, he loses.

Aoki knows the possible choices of (i, j) that Takahashi can choose, that is, he knows S_1, \ldots, S_N before playing the game. Also, in step 2, Aoki can choose (i', j') based on the answers to previous questions.

Determine whether Aoki can always win the game regardless of Takahashi's choice of (i, j) and randomness, if he uses an appropriate strategy.

There are T test cases in each input.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 500
  • The sum of N^2 over all test cases in each input is at most 500^2.
  • S_i is a string of length N consisting of 0 and 1.
  • There is at least one integer pair (i, j) satisfying S_{i,j} = 1.

Input

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

T
case_1
\vdots
case_T

Each test case is given in the following format:

N
S_1
\vdots
S_N

Output

For each test case, print Yes if Aoki can always win the game, and No otherwise.


Sample Input 1

3
2
01
11
2
11
11
10
0101011110
1100100001
1101100000
0111101010
1000011001
1110101010
1110110100
1110000110
0000001011
1001111100

Sample Output 1

Yes
No
Yes

In the first test case, an example of the game is as follows:

  1. Takahashi chooses (i, j) = (2, 2) satisfying S_{i,j}=\ 1.
  2. Aoki asks two questions. In the first question, he chooses (i', j') = (1, 1). Takahashi tells him that "At least one of i = 1 or j = 1 holds" is false. In the second question, he chooses (i', j') = (2, 2). Takahashi tells him that "At least one of i = 2 or j = 2 holds" is true.
  3. Aoki guesses (i, j) = (2, 2). Since his guess is correct, Aoki wins.

This is just an example of the game, and Aoki's strategy might not be optimal. However, if Aoki uses an appropriate strategy, he can always win the game, so the output for the first test case is Yes.

C - AB*A Changing

Time Limit: 2 sec / Memory Limit: 2048 MiB

配点 : 1200

問題文

長さ NAB のみからなる文字列 S,T が与えられます。Si 文字目を s_i と表すことにします。

あなたは S に対して次の操作を 0 回以上何度でも行えます。

  • 以下の条件を満たす整数組 (i,j) を選ぶ。
    • 1 \leq i \lt j \leq N
    • s_i = s_j = A
    • s_{i+1} = s_{i+2} = \ldots = s_{j-1} = B
  • そして、s_i,s_{i+1},\ldots,s_j を、それぞれ AB のうち操作前と違う方の文字に同時に置き換える。

操作の繰り返しによって ST を一致させることが可能ならば必要な操作回数の最小値を、不可能ならば -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • S,T は長さ NAB のみからなる文字列

入力

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

N
S
T

出力

ST を一致させることが可能ならば必要な操作回数の最小値を、不可能ならば -1 を出力せよ。


入力例 1

5
AAABA
BAAAB

出力例 1

2

以下のように操作をすると 2 回で ST を一致させられます。

  • (i,j)=(2,3) として操作をする。これによって SABBBA になる。
  • (i,j)=(1,5) として操作をする。これによって SBAAAB になる。

1 回以下の操作で ST を一致させることはできないため、答えは 2 になります。


入力例 2

1
A
B

出力例 2

-1

ST を一致させることができません。(i,j)i \lt j となるように選ばないといけないことに気を付けてください。


入力例 3

1
A
A

出力例 3

0

操作をしなくても ST が一致しています。


入力例 4

10
AAABBABAAB
BBABBAAABB

出力例 4

7

Score : 1200 points

Problem Statement

You are given two strings S and T of length N consisting of A and B. Let s_i denote the i-th character of S.

You can perform the following operation on S any number of times, possibly zero:

  • Choose an integer pair (i, j) satisfying the following conditions:
    • 1 \leq i < j \leq N
    • s_i = s_j = A
    • s_{i+1} = s_{i+2} = \ldots = s_{j-1} = B
  • Then, simultaneously replace each of s_i, s_{i+1}, \ldots, s_j with the other character (A becomes B, and B becomes A).

Determine the minimum number of operations required to make S equal to T. If it is impossible, print -1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • S and T are strings of length N consisting of A and B.

Input

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

N
S
T

Output

If it is possible to make S equal to T, print the minimum number of operations required; otherwise, output -1.


Sample Input 1

5
AAABA
BAAAB

Sample Output 1

2

You can make S equal to T in two operations as follows:

  1. Perform the operation with (i, j) = (2, 3). S becomes ABBBA.
  2. Perform the operation with (i, j) = (1, 5). S becomes BAAAB.

It is impossible to make S equal to T in fewer than two operations, so the answer is 2.


Sample Input 2

1
A
B

Sample Output 2

-1

It is impossible to make S equal to T. Note that you must choose (i, j) with i < j.


Sample Input 3

1
A
A

Sample Output 3

0

Without any operation, S is already equal to T.


Sample Input 4

10
AAABBABAAB
BBABBAAABB

Sample Output 4

7
D - Tree and Intervals

Time Limit: 4.5 sec / Memory Limit: 2048 MiB

配点 : 1200

問題文

整数 N と素数 P が与えられます。

頂点に 1 から N までの番号がついた N 頂点の木に対し、i\ (1 \leq i \leq N-1) 番目の辺が結ぶ頂点の番号を a_i, b_i とします。また、x_j\ (1 \leq j \leq N-1) を次のように定めます。

  • \min(a_i,b_i) \leq j \lt \max(a_i,b_i) を満たす整数 i\ (1 \leq i \leq N-1) の個数

(x_1,x_2, \ldots,x_{N-1}) としてあり得るものの個数を P で割った余りを求めてください。

制約

  • 2 \leq N \leq 500
  • 10^8 \leq P \leq 10^9
  • P は素数

入力

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

N P

出力

答えを出力せよ。


入力例 1

3 998244353

出力例 1

3

3 頂点の木は、頂点同士は番号で区別して辺同士は区別しないことにすると 3 個あります。
それぞれに対応する (x_1,x_2)(1,1),(2,1),(1,2) であり、期待される出力は 3P=998244353 で割った余りになります。


入力例 2

69 433416647

出力例 2

243082757

P で割った余りを求めてください。

Score : 1200 points

Problem Statement

You are given an integer N and a prime P.

For a tree with N vertices labeled 1 through N, let a_i and b_i be the endpoints of the i-th edge (1 \leq i \leq N-1). Also, define x_j\ (1 \leq j \leq N-1) as:

  • The number of integers i\ (1 \leq i \leq N-1) satisfying \min(a_i,b_i) \leq j \lt \max(a_i,b_i).

Find the number, modulo P, of sequences that could be (x_1, x_2, \ldots, x_{N - 1}).

Constraints

  • 2 \leq N \leq 500
  • 10^8 \leq P \leq 10^9
  • P is prime.

Input

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

N P

Output

Print the answer.


Sample Input 1

3 998244353

Sample Output 1

3

There are three different trees with three vertices, if we distinguish the vertices by their labels but not the edges.
The corresponding (x_1, x_2) are (1, 1), (2, 1), and (1, 2), so the expected output is 3 modulo P = 998244353.


Sample Input 2

69 433416647

Sample Output 2

243082757

Find the count modulo P.

E - Pair of Sequences

Time Limit: 8 sec / Memory Limit: 2048 MiB

配点 : 1200

問題文

整数 N,M,X,Y が与えられます。

数列 A=(a_1,\ldots,a_N), B=(b_1,\ldots,b_N) の組 (A,B) であって以下の条件すべてを満たすものの個数を 998244353 で割った余りを求めてください。

  • A は非負整数列
  • B(0,1,\ldots,M-1) の部分列
  • \sum\limits_{i=1}^{N} a_i = X
  • \sum\limits_{i=1}^{N} a_i b_i = Y

制約

  • 1 \leq N \leq M \leq 2 \times 10^5
  • 1 \leq X , Y \leq 2 \times 10^5
  • 入力はすべて整数

入力

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

N M X Y

出力

答えを出力せよ。


入力例 1

3 4 3 4

出力例 1

5

条件を満たす (A,B) の組は以下の 5 個です。

  • A=(0,2,1), B=(0,1,2)
  • A=(1,0,2), B=(0,1,2)
  • A=(1,1,1), B=(0,1,3)
  • A=(1,2,0), B=(0,2,3)
  • A=(2,1,0), B=(1,2,3)

入力例 2

1 1 1 1

出力例 2

0

条件を満たす (A,B) の組が存在しません。


入力例 3

12345 67890 9876 54321

出力例 3

150392014

998244353 で割った余りを求めてください。

Score : 1200 points

Problem Statement

You are given integers N, M, X, and Y.

Find the number, modulo 998244353, of pairs (A, B) of sequences A=(a_1,\ldots,a_N) and B=(b_1,\ldots,b_N) that satisfy all of the following conditions:

  • A = (a_1, \ldots, a_N) is a sequence of non-negative integers.
  • B = (b_1, \ldots, b_N) is a subsequence of (0, 1, \ldots, M - 1).
  • \sum\limits_{i=1}^{N} a_i = X.
  • \sum\limits_{i=1}^{N} a_i b_i = Y.

Constraints

  • 1 \leq N \leq M \leq 2 \times 10^5
  • 1 \leq X, Y \leq 2 \times 10^5
  • All input values are integers.

Input

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

N M X Y

Output

Print the answer modulo 998244353.


Sample Input 1

3 4 3 4

Sample Output 1

5

Here are the five pairs (A, B) satisfying the conditions:

  • A = (0, 2, 1), B = (0, 1, 2)
  • A = (1, 0, 2), B = (0, 1, 2)
  • A = (1, 1, 1), B = (0, 1, 3)
  • A = (1, 2, 0), B = (0, 2, 3)
  • A = (2, 1, 0), B = (1, 2, 3)

Sample Input 2

1 1 1 1

Sample Output 2

0

There are no pairs (A, B) satisfying the conditions.


Sample Input 3

12345 67890 9876 54321

Sample Output 3

150392014

Find the count modulo 998244353.