A - Kenken Race

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N 個の一列に並んだマス目があり、左から順に番号 1, 2, ..., N がついています。長さ N., # からなる文字列 S が与えられ、Si 文字目が # のときマス目 i には岩が置かれており、Si 文字目が . のときマス目 i には何も置かれていません。

最初、マス目 A にすぬけ君、B にふぬけ君がいます。

あなたは以下の操作を好きなだけ繰り返すことができます。

  • すぬけ君かふぬけ君を選び、1 マス右か 2 マス右にジャンプさせる。このときジャンプ先にマスが存在しなければならず、またそのマスに岩が置かれていたりもう一人がいてはならない。

あなたはこの操作を繰り返し、マス目 C にすぬけ君が、D にふぬけ君がいるようにしたいです。

このようなことが可能かどうかを判定してください。

制約

  • 4 \leq N \leq 200,000
  • S., # からなる長さ N の文字列
  • 1 \leq A, B, C, D \leq N
  • マス目 A, B, C, D に岩は置かれていない
  • A, B, C, D はすべて異なる
  • A < B
  • A < C
  • B < D

入力

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

N A B C D
S

出力

題意が達成可能ならば Yes、不可能ならば No を出力せよ。


入力例 1

7 1 3 6 7
.#..#..

出力例 1

Yes

たとえば、以下のように移動させれば良いです(すぬけ君、ふぬけ君を A, B で表します)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

入力例 2

7 1 3 7 6
.#..#..

出力例 2

No

入力例 3

15 1 3 15 13
...#.#...#.#...

出力例 3

Yes

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right. You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

  • Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

  • 4 \leq N \leq 200\ 000
  • S is a string of length N consisting of . and #.
  • 1 \leq A, B, C, D \leq N
  • Square A, B, C and D do not contain a rock.
  • A, B, C and D are all different.
  • A < B
  • A < C
  • B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.


Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes
B - ABC

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

A,B,C からなる文字列 s が与えられます。

すぬけ君は s に対して次の操作をできるだけ多く行おうとしています。

  • s の連続した部分文字列であって ABC であるようなものをひとつ選び、 BCA に書き換える。

操作回数の最大値を求めてください。

制約

  • 1 ≦ |s| ≦ 200000
  • s の各文字は A,B,C のいずれか

入力

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

s

出力

操作回数の最大値を出力せよ。


入力例 1

ABCABC

出力例 1

3

ABCABCBCAABCBCABCABCBCAA とすることで 3 回操作可能で、これが最大です。


入力例 2

C

出力例 2

0

入力例 3

ABCACCBABCBCAABCB

出力例 3

6

Score : 600 points

Problem Statement

You are given a string s consisting of A, B and C.

Snuke wants to perform the following operation on s as many times as possible:

  • Choose a contiguous substring of s that reads ABC and replace it with BCA.

Find the maximum possible number of operations.

Constraints

  • 1 \leq |s| \leq 200000
  • Each character of s is A, B and C.

Input

Input is given from Standard Input in the following format:

s

Output

Find the maximum possible number of operations.


Sample Input 1

ABCABC

Sample Output 1

3

You can perform the operations three times as follows: ABCABCBCAABCBCABCABCBCAA. This is the maximum result.


Sample Input 2

C

Sample Output 2

0

Sample Input 3

ABCACCBABCBCAABCB

Sample Output 3

6
C - Tests

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

高橋くんと青木くんは 1 から N までの番号がついたテストを受けようとしています。 二人はこのテストの結果を使って勝負することにしました。 具体的には、次のようにして勝敗を決めます。

  • 高橋くんが各テスト i について、その重要度 c_i を決める。ただしこの値は l_i 以上 u_i 以下の整数である必要がある。

  • \sum_{i=1}^{N} c_i \times (高橋くんのテスト i の点数) を A, \ \sum_{i=1}^{N} c_i \times (青木くんのテスト i の点数) を B とする。 A \geq B なら高橋くんの勝ち、A < B なら青木くんの勝ち。

高橋くんはエスパーなので、青木くんがテスト ib_i 点をとることがわかっています。

高橋くんはこのままだとすべてのテストで 0 点をとってしまいますが、 1 時間勉強するごとに、好きなテストの点数を 1 だけ上げることができます。(1 時間単位でしか勉強できません。) ただしテストはすべて X 点満点なので、 X より大きい点数にすることはできません。

高橋くんが勝つために必要な最小の勉強時間を出力してください。

制約

  • 1 ≦ N ≦ 10^5
  • 1 ≦ X ≦ 10^5
  • 0 ≦ b_i ≦ X (1 \leq i \leq N)
  • 1 ≦ l_i ≦ u_i ≦ 10^5 (1 \leq i \leq N)
  • 入力はすべて整数

入力

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

N X
b_1 l_1 u_1
b_2 l_2 u_2
:
b_N l_N u_N

出力

高橋くんが勝つために必要な最小の勉強時間を出力せよ。


入力例 1

2 100
85 2 3
60 1 1

出力例 1

115

例えば次のようにするのが最適です。

  • c_1 = 3, c_2 = 1 とする。

  • テスト 1100 点、テスト 215 点とるように勉強する。

このとき A = 3 \times 100 + 1 \times 15 = 315, B = 3 \times 85 + 1 \times 60 = 315 なので高橋くんが勝ちます。


入力例 2

2 100
85 2 3
60 10 10

出力例 2

77

入力例 3

1 100000
31415 2718 2818

出力例 3

31415

入力例 4

10 1000
451 4593 6263
324 310 6991
378 1431 7068
71 1757 9218
204 3676 4328
840 6221 9080
684 1545 8511
709 5467 8674
862 6504 9835
283 4965 9980

出力例 4

2540

Score : 800 points

Problem Statement

Takahashi and Aoki will take N exams numbered 1 to N. They have decided to compete in these exams. The winner will be determined as follows:

  • For each exam i, Takahashi decides its importance c_i, which must be an integer between l_i and u_i (inclusive).

  • Let A be \sum_{i=1}^{N} c_i \times (Takahashi's score on Exam i), and B be \sum_{i=1}^{N} c_i \times (Aoki's score on Exam i). Takahashi wins if A \geq B, and Aoki wins if A < B.

Takahashi knows that Aoki will score b_i on Exam i, with his supernatural power.

Takahashi himself, on the other hand, will score 0 on all the exams without studying more. For each hour of study, he can increase his score on some exam by 1. (He can only study for an integer number of hours.) However, he cannot score more than X on an exam, since the perfect score for all the exams is X.

Print the minimum number of study hours required for Takahashi to win.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq X \leq 10^5
  • 0 \leq b_i \leq X (1 \leq i \leq N)
  • 1 \leq l_i \leq u_i \leq 10^5 (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
b_1 l_1 u_1
b_2 l_2 u_2
:
b_N l_N u_N

Output

Print the minimum number of study hours required for Takahashi to win.


Sample Input 1

2 100
85 2 3
60 1 1

Sample Output 1

115

One optimal strategy is as follows:

  • Choose c_1 = 3, c_2 = 1.

  • Study to score 100 on Exam 1 and 15 on Exam 2.

Then, A = 3 \times 100 + 1 \times 15 = 315, B = 3 \times 85 + 1 \times 60 = 315 and Takahashi will win.


Sample Input 2

2 100
85 2 3
60 10 10

Sample Output 2

77

Sample Input 3

1 100000
31415 2718 2818

Sample Output 3

31415

Sample Input 4

10 1000
451 4593 6263
324 310 6991
378 1431 7068
71 1757 9218
204 3676 4328
840 6221 9080
684 1545 8511
709 5467 8674
862 6504 9835
283 4965 9980

Sample Output 4

2540
D - Manhattan Max Matching

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 1200

問題文

すぬけくんは、二次元平面上に赤いボールと青いボールを置いて遊んでいます。

すぬけくんはまず、赤いボールを置く操作を N 回行いました。 i 回目の操作では、座標 (RX_i,RY_i)RC_i 個の赤いボールを置きました。 すぬけくんは次に、青いボールを置く操作を N 回行いました。 i 回目の操作では、座標 (BX_i,BY_i)BC_i 個の青いボールを置きました。 ここで、すぬけくんが置いた赤いボールの個数の総和と青いボールの個数の総和は等しいです。 つまり、\sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i です。 以後、この値を S とおきます。

すぬけくんはこれから、赤いボールと青いボールのペアを S 個作ろうとしています。 どのボールも、ちょうど 1 つのペアに属するようにします。 ここで、座標 (rx,ry) にある赤いボールと座標 (bx,by) にある青いボールのペアのスコアを、 |rx-bx| + |ry-by| と定義します。

すぬけくんは、ペアのスコアの総和を最大化したいです。 すぬけくんのために、ペアのスコアの総和の最大値を求めてください。

制約

  • 1 \leq N \leq 1000
  • 0 \leq RX_i,RY_i,BX_i,BY_i \leq 10^9
  • 1 \leq RC_i,BC_i \leq 10
  • \sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i
  • 入力される値はすべて整数である。

入力

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

N
RX_1 RY_1 RC_1
RX_2 RY_2 RC_2
\vdots
RX_N RY_N RC_N
BX_1 BY_1 BC_1
BX_2 BY_2 BC_2
\vdots
BX_N BY_N BC_N

出力

ペアのスコアの総和の最大値を出力せよ。


入力例 1

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

出力例 1

8

座標 (0,0) に置いてある赤いボールと座標 (2,2) に置いてある青いボールをペアにすると、 そのスコアは |0-2| + |0-2|=4 です。 また、座標 (3,2) に置いてある赤いボールと座標 (5,0) に置いてある青いボールをペアにすると、 そのスコアは |3-5| + |2-0|=4 です。 この 2 つのペアを作ると、スコアの総和は 8 になり、これが最大です。


入力例 2

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

出力例 2

16

同じ座標に複数回操作を行うこともあります。


入力例 3

10
582463373 690528069 8
621230322 318051944 4
356524296 974059503 6
372751381 111542460 9
392867214 581476334 6
606955458 513028121 5
882201596 791660614 9
250465517 91918758 3
618624774 406956634 6
426294747 736401096 5
974896051 888765942 5
726682138 336960821 3
715144179 82444709 6
599055841 501257806 6
390484433 962747856 4
912334580 219343832 8
570458984 648862300 6
638017635 572157978 10
435958984 585073520 7
445612658 234265014 6

出力例 3

45152033546

Score : 1200 points

Problem Statement

Snuke is playing with red and blue balls, placing them on a two-dimensional plane.

First, he performed N operations to place red balls. In the i-th of these operations, he placed RC_i red balls at coordinates (RX_i,RY_i). Then, he performed another N operations to place blue balls. In the i-th of these operations, he placed BC_i blue balls at coordinates (BX_i,BY_i). The total number of red balls placed and the total number of blue balls placed are equal, that is, \sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i. Let this value be S.

Snuke will now form S pairs of red and blue balls so that every ball belongs to exactly one pair. Let us define the score of a pair of a red ball at coordinates (rx, ry) and a blue ball at coordinates (bx, by) as |rx-bx| + |ry-by|.

Snuke wants to maximize the sum of the scores of the pairs. Help him by finding the maximum possible sum of the scores of the pairs.

Constraints

  • 1 \leq N \leq 1000
  • 0 \leq RX_i,RY_i,BX_i,BY_i \leq 10^9
  • 1 \leq RC_i,BC_i \leq 10
  • \sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
RX_1 RY_1 RC_1
RX_2 RY_2 RC_2
\vdots
RX_N RY_N RC_N
BX_1 BY_1 BC_1
BX_2 BY_2 BC_2
\vdots
BX_N BY_N BC_N

Output

Print the maximum possible sum of the scores of the pairs.


Sample Input 1

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

Sample Output 1

8

If we pair the red ball at coordinates (0,0) and the blue ball at coordinates (2,2), the score of this pair is |0-2| + |0-2|=4. Then, if we pair the red ball at coordinates (3,2) and the blue ball at coordinates (5,0), the score of this pair is |3-5| + |2-0|=4. Making these two pairs results in the total score of 8, which is the maximum result.


Sample Input 2

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

Sample Output 2

16

Snuke may have performed multiple operations at the same coordinates.


Sample Input 3

10
582463373 690528069 8
621230322 318051944 4
356524296 974059503 6
372751381 111542460 9
392867214 581476334 6
606955458 513028121 5
882201596 791660614 9
250465517 91918758 3
618624774 406956634 6
426294747 736401096 5
974896051 888765942 5
726682138 336960821 3
715144179 82444709 6
599055841 501257806 6
390484433 962747856 4
912334580 219343832 8
570458984 648862300 6
638017635 572157978 10
435958984 585073520 7
445612658 234265014 6

Sample Output 3

45152033546
E - Complete Compress

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1500

問題文

頂点に番号 1, 2, ..., N がついた N 頂点の木が与えられます。i 番目の辺は頂点 a_i と頂点 b_i を結んでいます。 また長さ N0, 1 からなる文字列 S が与えられ、Si 文字目は頂点 i に置いてあるコマの個数を表しています。

すぬけ君は、以下の操作を好きなだけ行います。

  • 距離が 2 以上離れたコマ 2 個を選び、お互いに 1 ずつ近づける。 正確に述べると、コマの置かれた頂点 u, v を選び、u, v 間の最短パスを考える。ここでパスの辺数が 2 以上となるように選ぶことにする。パスにおいて u に隣り合う頂点にコマを 1u から動かし、v に隣り合う頂点にコマを 1v から動かす。

すぬけ君はこれを繰り返し、すべてのコマを同じ頂点に集めたいです。このようなことは可能でしょうか? 可能な場合、それを達成するのに必要な操作の最小回数も求めてください。

制約

  • 2 \leq N \leq 2000
  • |S| = N
  • S0, 1からなり、また少なくとも 1 文字は 1 を含む
  • 1 \leq a_i, b_i \leq N(a_i \neq b_i)
  • (a_1, b_1), (a_2, b_2), ..., (a_{N - 1}, b_{N - 1}) は木をなす

入力

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

N
S
a_1 b_1
a_2 b_2
:
a_{N - 1} b_{N - 1}

出力

コマを同じ頂点に集められない場合 -1、集められる場合は操作の最小回数を出力せよ。


入力例 1

7
0010101
1 2
2 3
1 4
4 5
1 6
6 7

出力例 1

3
  • 頂点 3, 5 のコマを選ぶ
  • 頂点 2, 7 のコマを選ぶ
  • 頂点 4, 6 のコマを選ぶ

3 回の操作ですべてのコマを頂点 1 に集めることができます。


入力例 2

7
0010110
1 2
2 3
1 4
4 5
1 6
6 7

出力例 2

-1

入力例 3

2
01
1 2

出力例 3

0

Score : 1500 points

Problem Statement

You are given a tree with N vertices numbered 1, 2, ..., N. The i-th edge connects Vertex a_i and Vertex b_i. You are also given a string S of length N consisting of 0 and 1. The i-th character of S represents the number of pieces placed on Vertex i.

Snuke will perform the following operation some number of times:

  • Choose two pieces the distance between which is at least 2, and bring these pieces closer to each other by 1. More formally, choose two vertices u and v, each with one or more pieces, and consider the shortest path between them. Here the path must contain at least two edges. Then, move one piece from u to its adjacent vertex on the path, and move one piece from v to its adjacent vertex on the path.

By repeating this operation, Snuke wants to have all the pieces on the same vertex. Is this possible? If the answer is yes, also find the minimum number of operations required to achieve it.

Constraints

  • 2 \leq N \leq 2000
  • |S| = N
  • S consists of 0 and 1, and contains at least one 1.
  • 1 \leq a_i, b_i \leq N(a_i \neq b_i)
  • The edges (a_1, b_1), (a_2, b_2), ..., (a_{N - 1}, b_{N - 1}) forms a tree.

Input

Input is given from Standard Input in the following format:

N
S
a_1 b_1
a_2 b_2
:
a_{N - 1} b_{N - 1}

Output

If it is impossible to have all the pieces on the same vertex, print -1. If it is possible, print the minimum number of operations required.


Sample Input 1

7
0010101
1 2
2 3
1 4
4 5
1 6
6 7

Sample Output 1

3

We can gather all the pieces in three operations as follows:

  • Choose the pieces on Vertex 3 and 5.
  • Choose the pieces on Vertex 2 and 7.
  • Choose the pieces on Vertex 4 and 6.

Sample Input 2

7
0010110
1 2
2 3
1 4
4 5
1 6
6 7

Sample Output 2

-1

Sample Input 3

2
01
1 2

Sample Output 3

0
F - RNG and XOR

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1600

問題文

すぬけくんはある乱数生成器を手に入れました。 この乱数生成器は、0 以上 2^N-1 以下の整数を生成します。 それぞれの整数を生成する確率は、整数列 A_0,A_1,\cdots,A_{2^N-1} によって表され、 整数 i (0 \leq i \leq 2^N-1) が生成される確率は A_i / S です。 ただしここで S = \sum_{i=0}^{2^N-1} A_i とします。 また、乱数生成は毎回独立に行われます。

すぬけくんは整数 X を持っています。 今、X=0 です。 すぬけくんは、次の操作を好きなだけ行うことが出来ます。

  • 乱数生成器で一つの整数 v を生成する。そして、XX \oplus v で置き換える。 ただしここで、\oplus はビットごとの排他的論理和を表す。

それぞれの整数 i (0 \leq i \leq 2^N-1) について、Xi にするために必要な操作の回数の期待値を求めてください。 ただし、期待値は mod 998244353 で出力してください。 より正確には、期待値が既約分数 P/Q で表されるとき、 R \times Q \equiv P \mod 998244353,\ 0 \leq R < 998244353 を満たす整数 R が一意に定まるので、その R を出力してください。

なお、すべての i について、Xi にするために必要な操作の回数の期待値が有理数として存在し、 さらに mod 998244353 での整数表現が定義できることが問題の制約から証明できます。

制約

  • 1 \leq N \leq 18
  • 1 \leq A_i \leq 1000
  • 入力される値はすべて整数である。

入力

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

N
A_0 A_1 \cdots A_{2^N-1}

出力

2^N 行出力せよ。 i+1 行目 (0 \leq i \leq 2^N-1) には、Xi にするために必要な操作の回数の期待値を mod 998244353 で出力せよ。


入力例 1

2
1 1 1 1

出力例 1

0
4
4
4

0 回操作をした段階で X=0 なので、X0 にするのに必要な操作回数の期待値は 0 です。

また、どの状態から操作しても、操作後の X の値はそれぞれ 1/4 の確率で 0,1,2,3 になります。 よって、X1,2,3 にするのに必要な操作回数の期待値はいずれも 4 です。


入力例 2

2
1 2 1 2

出力例 2

0
499122180
4
499122180

X0,1,2,3 にするのに必要な操作回数の期待値は、それぞれ 0,7/2,4,7/2 です。


入力例 3

4
337 780 799 10 796 875 331 223 941 67 148 483 390 565 116 355

出力例 3

0
468683018
635850749
96019779
657074071
24757563
745107950
665159588
551278361
143136064
557841197
185790407
988018173
247117461
129098626
789682908

Score : 1600 points

Problem Statement

Snuke found a random number generator. It generates an integer between 0 and 2^N-1 (inclusive). An integer sequence A_0, A_1, \cdots, A_{2^N-1} represents the probability that each of these integers is generated. The integer i (0 \leq i \leq 2^N-1) is generated with probability A_i / S, where S = \sum_{i=0}^{2^N-1} A_i. The process of generating an integer is done independently each time the generator is executed.

Snuke has an integer X, which is now 0. He can perform the following operation any number of times:

  • Generate an integer v with the generator and replace X with X \oplus v, where \oplus denotes the bitwise XOR.

For each integer i (0 \leq i \leq 2^N-1), find the expected number of operations until X becomes i, and print it modulo 998244353. More formally, represent the expected number of operations as an irreducible fraction P/Q. Then, there exists a unique integer R such that R \times Q \equiv P \mod 998244353,\ 0 \leq R < 998244353, so print this R.

We can prove that, for every i, the expected number of operations until X becomes i is a finite rational number, and its integer representation modulo 998244353 can be defined.

Constraints

  • 1 \leq N \leq 18
  • 1 \leq A_i \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 \cdots A_{2^N-1}

Output

Print 2^N lines. The (i+1)-th line (0 \leq i \leq 2^N-1) should contain the expected number of operations until X becomes i, modulo 998244353.


Sample Input 1

2
1 1 1 1

Sample Output 1

0
4
4
4

X=0 after zero operations, so the expected number of operations until X becomes 0 is 0.

Also, from any state, the value of X after one operation is 0, 1, 2 or 3 with equal probability. Thus, the expected numbers of operations until X becomes 1, 2 and 3 are all 4.


Sample Input 2

2
1 2 1 2

Sample Output 2

0
499122180
4
499122180

The expected numbers of operations until X becomes 0, 1, 2 and 3 are 0, 7/2, 4 and 7/2, respectively.


Sample Input 3

4
337 780 799 10 796 875 331 223 941 67 148 483 390 565 116 355

Sample Output 3

0
468683018
635850749
96019779
657074071
24757563
745107950
665159588
551278361
143136064
557841197
185790407
988018173
247117461
129098626
789682908