A - Full House 2

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

配点 : 100

問題文

4 枚のカードがあり、それぞれのカードには整数 A,B,C,D が書かれています。
ここに 1 枚カードを加え、フルハウスとできるか判定してください。

ただし、 5 枚組のカードは以下の条件を満たすとき、またそのときに限って、フルハウスであると呼ばれます。

  • 異なる整数 x,y について、 x が書かれたカード 3 枚と y が書かれたカード 2 枚からなる。

制約

  • 入力は全て整数
  • 1 \le A,B,C,D \le 13

入力

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

A B C D

出力

1 枚カードを加えてフルハウスとできる場合は Yes 、そうでないときは No と出力せよ。


入力例 1

7 7 7 1

出力例 1

Yes

7,7,7,11 を加えた時、フルハウスとなります。


入力例 2

13 12 11 10

出力例 2

No

13,12,11,10 に何を加えてもフルハウスにはなりません。


入力例 3

3 3 5 5

出力例 3

Yes

3,3,5,53 を加えた時、フルハウスとなります。
また、 5 を加えてもフルハウスとなります。


入力例 4

8 8 8 8

出力例 4

No

8,8,8,8 に何を加えてもフルハウスにはなりません。
同じ 5 枚のカードはフルハウスではないことに注意してください。


入力例 5

1 3 4 1

出力例 5

No

Score : 100 points

Problem Statement

There are four cards with integers A,B,C,D written on them.
Determine whether a Full House can be formed by adding one card.

A set of five cards is called a Full House if and only if the following condition is satisfied:

  • For two distinct integers x and y, there are three cards with x written on them and two cards with y written on them.

Constraints

  • All input values are integers.
  • 1 \le A,B,C,D \le 13

Input

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

A B C D

Output

If adding one card can form a Full House, print Yes; otherwise, print No.


Sample Input 1

7 7 7 1

Sample Output 1

Yes

Adding 1 to 7,7,7,1 forms a Full House.


Sample Input 2

13 12 11 10

Sample Output 2

No

Adding anything to 13,12,11,10 does not form a Full House.


Sample Input 3

3 3 5 5

Sample Output 3

Yes

Adding 3,3,5,5 to 3 forms a Full House.
Also, adding 5 forms a Full House.


Sample Input 4

8 8 8 8

Sample Output 4

No

Adding anything to 8,8,8,8 does not form a Full House.
Note that five identical cards do not form a Full House.


Sample Input 5

1 3 4 1

Sample Output 5

No
B - Calculator

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

配点 : 200

問題文

00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 のボタンがある電卓があります。
この電卓で文字列 x が表示されている時に b のボタンを押すと、表示される文字列は x の末尾に b を連結したものとなります。

最初、電卓には空文字列 ( 0 文字の文字列 ) が表示されています。
この電卓に文字列 S を表示させるためにボタンを押す回数の最小値を求めてください。

制約

  • S0, 1, 2, 3, 4, 5, 6, 7, 8, 9 からなる長さ 1 以上 1000 以下の列
  • S の先頭は 0 でない

入力

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

S

出力

答えを整数として出力せよ。


入力例 1

1000000007

出力例 1

6

1000000007 を表示させるには、 1, 00, 00, 00, 00, 7 のボタンをこの順に押せばよく、ボタンを押した回数は 6 回で、これが達成可能な最小値です。


入力例 2

998244353

出力例 2

9

入力例 3

32000

出力例 3

4

Score : 200 points

Problem Statement

There is a calculator with the buttons 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
When a string x is displayed on this calculator and you press a button b, the resulting displayed string becomes the string x with b appended to its end.

Initially, the calculator displays the empty string (a string of length 0).
Find the minimum number of button presses required to display the string S on this calculator.

Constraints

  • S is a string of length at least 1 and at most 1000, consisting of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
  • The first character of S is not 0.

Input

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

S

Output

Print the answer as an integer.


Sample Input 1

1000000007

Sample Output 1

6

To display 1000000007, you can press the buttons 1, 00, 00, 00, 00, 7 in this order. The total number of button presses is 6, and this is the minimum possible.


Sample Input 2

998244353

Sample Output 2

9

Sample Input 3

32000

Sample Output 3

4
C - Operate 1

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

配点 : 350

問題文

この問題は F 問題 (Operate K) の部分問題であり、 K=1 です。
F 問題に正解するコードをこの問題に提出することで、この問題に正解できます。

文字列 S に対して以下の操作を 0 回以上 K 回以下行って、文字列 T と一致させられるか判定してください。

  • 次の 3 種類の操作のうちひとつを選択し、実行する。
    • S 中の (先頭や末尾を含む) 任意の位置に、任意の文字を 1 つ挿入する。
    • S 中の文字を 1 つ選び、削除する。
    • S 中の文字を 1 つ選び、別の 1 つの文字に変更する。

制約

  • S,T は英小文字からなる長さ 1 以上 500000 以下の文字列
  • \color{red}{K=1}

入力

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

K
S
T

出力

K 回以下の操作で ST に一致させられる時 Yes 、そうでない時 No と出力せよ。


入力例 1

1
abc
agc

出力例 1

Yes

abc2 文字目の bg に置き換えることで、 abc1 回の操作で agc に変換できます。


入力例 2

1
abc
awtf

出力例 2

No

1 回の操作では abcawtf に変換できません。


入力例 3

1
abc
ac

出力例 3

Yes

abc2 文字目の b を削除することで、 abc1 回の操作で ac に変換できます。


入力例 4

1
back
black

出力例 4

Yes

back1 文字目と 2 文字目の間に l を挿入することで、 back1 回の操作で black に変換できます。


入力例 5

1
same
same

出力例 5

Yes

初めから S=T である場合もあります。


入力例 6

1
leap
read

出力例 6

No

Score : 350 points

Problem Statement

This problem is a sub-problem of Problem F (Operate K), with K=1.
You can solve this problem by submitting a correct solution for Problem F to this problem.

Determine whether it is possible to perform the following operation on string S between 0 and K times, inclusive, to make it identical to string T.

  • Choose one of the following three operations and execute it.
    • Insert any one character at any position in S (possibly the beginning or end).
    • Delete one character from S.
    • Choose one character in S and replace it with another character.

Constraints

  • Each of S and T is a string of length between 1 and 500000, inclusive, consisting of lowercase English letters.
  • \color{red}{K=1}

Input

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

K
S
T

Output

If S can be made identical to T with at most K operations, print Yes; otherwise, print No.


Sample Input 1

1
abc
agc

Sample Output 1

Yes

Replacing the second character b of abc with g converts abc to agc in one operation.


Sample Input 2

1
abc
awtf

Sample Output 2

No

abc cannot be converted to awtf in one operation.


Sample Input 3

1
abc
ac

Sample Output 3

Yes

Deleting the second character b of abc converts abc to ac in one operation.


Sample Input 4

1
back
black

Sample Output 4

Yes

Inserting l between the first and second characters of back converts back to black in one operation.


Sample Input 5

1
same
same

Sample Output 5

Yes

It is also possible that S = T from the beginning.


Sample Input 6

1
leap
read

Sample Output 6

No
D - Diagonal Separation

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

配点 : 425

問題文

NN 列のグリッドがあります。高橋君は以下の条件を満たすように各マスを黒または白のいずれかに塗り分けたいと考えています。

  • すべての行について以下の条件が成り立つ。
    • ある整数 i\ (0\leq i\leq N) が存在して、その行の左から i 個のマスは黒、それ以外は白で塗られている。
  • すべての列について以下の条件が成り立つ。
    • ある整数 i\ (0\leq i\leq N) が存在して、その列の上から i 個のマスは黒、それ以外は白で塗られている。

M 個のマスがすでに塗られています。そのうち i 個目は上から X_i 行目、左から Y_i 列目のマスで、C_iB のとき黒で、 W のとき白で塗られています。

高橋君はまだ塗られていない残りの N^2-M 個のマスの色をうまく決めることで条件を満たすことができるか判定してください。

制約

  • 1\leq N\leq 10^9
  • 1\leq M\leq \min(N^2,2\times 10^5)
  • 1\leq X_i,Y_i\leq N
  • (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
  • C_iB または W
  • 入力される数値はすべて整数

入力

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

N M
X_1 Y_1 C_1
\vdots
X_M Y_M C_M

出力

条件を満たすことができるとき Yes を、そうでないとき No を出力せよ。


入力例 1

4 3
4 1 B
3 2 W
1 3 B

出力例 1

Yes

例えば以下の図のように色を塗り分けると条件を満たすことができます。すでに塗られているマスを赤色の線で囲んでいます。


入力例 2

2 2
1 2 W
2 2 B

出力例 2

No

塗られていない残りの 2 つのマスをどのように塗っても,条件を満たすことはできません。


入力例 3

1 1
1 1 W

出力例 3

Yes

入力例 4

2289 10
1700 1083 W
528 967 B
1789 211 W
518 1708 W
1036 779 B
136 657 B
759 1497 B
902 1309 B
1814 712 B
936 763 B

出力例 4

No

Score : 425 points

Problem Statement

There is an N \times N grid. Takahashi wants to color each cell black or white so that all of the following conditions are satisfied:

  • For every row, the following condition holds:
    • There exists an integer i\ (0\leq i\leq N) such that the leftmost i cells are colored black, and the rest are colored white.
  • For every column, the following condition holds:
    • There exists an integer i\ (0\leq i\leq N) such that the topmost i cells are colored black, and the rest are colored white.

Out of these N^2 cells, M of them have already been colored. Among them, the i-th one is at the X_i-th row from the top and the Y_i-th column from the left, and it is colored black if C_i is B and white if C_i is W.

Determine whether he can color the remaining uncolored N^2 - M cells so that all the conditions are satisfied.

Constraints

  • 1\leq N\leq 10^9
  • 1\leq M\leq \min(N^2,2\times 10^5)
  • 1\leq X_i,Y_i\leq N
  • (X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
  • C_i is B or W.
  • All input numbers are integers.

Input

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

N M
X_1 Y_1 C_1
\vdots
X_M Y_M C_M

Output

If it is possible to satisfy the conditions, print Yes; otherwise, print No.


Sample Input 1

4 3
4 1 B
3 2 W
1 3 B

Sample Output 1

Yes

For example, one can color the grid as in the following figure to satisfy the conditions. The cells already colored are surrounded by red borders.


Sample Input 2

2 2
1 2 W
2 2 B

Sample Output 2

No

No matter how the remaining two cells are colored, the conditions cannot be satisfied.


Sample Input 3

1 1
1 1 W

Sample Output 3

Yes

Sample Input 4

2289 10
1700 1083 W
528 967 B
1789 211 W
518 1708 W
1036 779 B
136 657 B
759 1497 B
902 1309 B
1814 712 B
936 763 B

Sample Output 4

No
E - Maximize XOR

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

配点 : 500

問題文

長さ N の非負整数列 A および整数 K が与えられます。ここで二項係数 \dbinom{N}{K}10^6 以下であることが保証されます。

A から異なる K 項を選ぶとき、選んだ K 個の数の総 XOR としてあり得る最大値を求めてください。

すなわち \underset{1\leq i_1\lt i_2\lt \ldots\lt i_K\leq N}{\max} A_{i_1}\oplus A_{i_2}\oplus \ldots \oplus A_{i_K} を求めてください。

XOR とは 非負整数 A,B の 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, \dots, p_k の XOR は (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k) と定義され、これは p_1, \dots, p_k の順番によらないことが証明できます。

制約

  • 1\leq K\leq N\leq 2\times 10^5
  • 0\leq A_i\lt 2^{60}
  • \dbinom{N}{K}\leq 10^6
  • 入力は全て整数

入力

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

N K
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 2
3 2 6 4

出力例 1

7

(3,2,6,4) から異なる 2 つの項を選ぶ方法は以下の 6 通りあります。

  • (3,2) のとき、選んだ数の総 XOR は 3\oplus 2 = 1 です。
  • (3,6) のとき、選んだ数の総 XOR は 3\oplus 6 = 5 です。
  • (3,4) のとき、選んだ数の総 XOR は 3\oplus 4 = 7 です。
  • (2,6) のとき、選んだ数の総 XOR は 2\oplus 6 = 4 です。
  • (2,4) のとき、選んだ数の総 XOR は 2\oplus 4 = 6 です。
  • (6,4) のとき、選んだ数の総 XOR は 6\oplus 4 = 2 です。

よって、求める最大値は 7 です。


入力例 2

10 4
1516 1184 1361 2014 1013 1361 1624 1127 1117 1759

出力例 2

2024

Score : 500 points

Problem Statement

You are given a sequence A of non-negative integers of length N, and an integer K. It is guaranteed that the binomial coefficient \dbinom{N}{K} is at most 10^6.

When choosing K distinct elements from A, find the maximum possible value of the XOR of the K chosen elements.

That is, find \underset{1\leq i_1\lt i_2\lt \ldots\lt i_K\leq N}{\max} A_{i_1}\oplus A_{i_2}\oplus \ldots \oplus A_{i_K}.

About XOR For non-negative integers A,B, the XOR A \oplus B is defined as follows:
  • In the binary representation of A \oplus B, the bit corresponding to 2^k (k \ge 0) is 1 if and only if exactly one of the bits corresponding to 2^k in A and B is 1, and is 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary notation: 011 \oplus 101 = 110).
In general, the XOR of K integers p_1, \dots, p_k is defined as (\cdots((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k). It can be proved that it does not depend on the order of p_1, \dots, p_k.

Constraints

  • 1\leq K\leq N\leq 2\times 10^5
  • 0\leq A_i<2^{60}
  • \dbinom{N}{K}\leq 10^6
  • All input values 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

4 2
3 2 6 4

Sample Output 1

7

Here are six ways to choose two distinct elements from (3,2,6,4).

  • (3,2): The XOR is 3\oplus 2 = 1.
  • (3,6): The XOR is 3\oplus 6 = 5.
  • (3,4): The XOR is 3\oplus 4 = 7.
  • (2,6): The XOR is 2\oplus 6 = 4.
  • (2,4): The XOR is 2\oplus 4 = 6.
  • (6,4): The XOR is 6\oplus 4 = 2.

Hence, the maximum possible value is 7.


Sample Input 2

10 4
1516 1184 1361 2014 1013 1361 1624 1127 1117 1759

Sample Output 2

2024
F - Operate K

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

配点 : 525

問題文

この問題は C 問題 (Operate 1) を完全に含んでおり、 K \le 20 です。
この問題に正解するコードを C 問題に提出することで、 C 問題に正解できます。

文字列 S に対して以下の操作を 0 回以上 K 回以下行って、文字列 T と一致させられるか判定してください。

  • 次の 3 種類の操作のうちひとつを選択し、実行する。
    • S 中の (先頭や末尾を含む) 任意の位置に、任意の文字を 1 つ挿入する。
    • S 中の文字を 1 つ選び、削除する。
    • S 中の文字を 1 つ選び、別の 1 つの文字に変更する。

制約

  • S,T は英小文字からなる長さ 1 以上 500000 以下の文字列
  • K\color{red}{1 \le K \le 20} を満たす整数

入力

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

K
S
T

出力

K 回以下の操作で ST に一致させられる時 Yes 、そうでない時 No と出力せよ。


入力例 1

3
abc
awtf

出力例 1

Yes

例えば、次のように操作することで、 3 回の操作で abcawtf に変換できます。

  • 2 文字目の bw に変更する。操作後の文字列は awc となる。
  • 3 文字目の cf に変更する。操作後の文字列は awf となる。
  • 2 文字目と 3 文字目の間に t を挿入する。操作後の文字列は awtf となる。

入力例 2

2
abc
awtf

出力例 2

No

2 回以下の操作では abcawtf に変換できません。


入力例 3

17
twothousandtwentyfour
happynewyear

出力例 3

Yes

Score : 525 points

Problem Statement

This problem fully contains Problem C (Operate 1), with K \le 20.
You can solve Problem C by submitting a correct solution to this problem for Problem C.

Determine whether it is possible to perform the following operation on string S between 0 and K times, inclusive, to make it identical to string T.

  • Choose one of the following three operations and execute it.
    • Insert any one character at any position in S (possibly the beginning or end).
    • Delete one character from S.
    • Choose one character in S and replace it with another character.

Constraints

  • Each of S and T is a string of length between 1 and 500000, inclusive, consisting of lowercase English letters.
  • K is an integer satisfying \color{red}{1 \le K \le 20}.

Input

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

K
S
T

Output

If S can be made identical to T with at most K operations, print Yes; otherwise, print No.


Sample Input 1

3
abc
awtf

Sample Output 1

Yes

For example, here is a way to convert abc to awtf with three operations:

  • Replace the second character b with w. After the operation, the string becomes awc.
  • Replace the third character c with f. After the operation, the string becomes awf.
  • Insert t between the second and third characters. After the operation, the string becomes awtf.

Sample Input 2

2
abc
awtf

Sample Output 2

No

abc cannot be converted to awtf with two or fewer operations.


Sample Input 3

17
twothousandtwentyfour
happynewyear

Sample Output 3

Yes
G - Many MST

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

配点 : 600

問題文

正整数 N,M が与えられます。頂点に 1 から N までの番号がつけられている N 頂点の重み付き完全グラフであって各辺の重みが 1 以上 M 以下の整数であるようなものは M^{N(N-1)/2} 通りありますが、それぞれについて最小全域木に含まれる辺の重みの和を求めたとき、それらの総和はいくつになりますか?総和を 998244353 で割った余りを出力してください。

制約

  • 2\leq N\leq 500
  • 1\leq M\leq 500
  • 入力は全て整数

入力

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

N M

出力

答えを出力せよ。


入力例 1

3 2

出力例 1

21

辺の重みが 1 または 2 である 3 頂点の重み付き完全グラフは以下の 8 つあります。それぞれの完全グラフについて、最小全域木に含まれる辺は赤色で塗られています。

それぞれの完全グラフの最小全域木に含まれる辺の重みの和は 2,2,2,3,2,3,3,4 であるため、求める答えは 2+2+2+3+2+3+3+4=21 です。


入力例 2

2 100

出力例 2

5050

入力例 3

20 24

出力例 3

707081320

Score : 600 points

Problem Statement

You are given positive integers N and M. Consider a weighted complete graph with N vertices labeled from 1 to N, where the weight of each edge is an integer between 1 and M, inclusive. There are M^{N(N-1)/2} such graphs. For each of them, consider the sum of the weights of the edges included in its Minimum Spanning Tree. What is the total of these sums? Print the result modulo 998244353.

Constraints

  • 2 \le N \le 500
  • 1 \le M \le 500
  • All input values are integers.

Input

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

N M

Output

Print the answer.


Sample Input 1

3 2

Sample Output 1

21

Here are eight complete graphs with three vertices where edge weights are 1 or 2. The edges in each graph’s MST are highlighted in red in the figure below.

The sums of the MST edges for these graphs are 2,2,2,3,2,3,3,4, so the total is 2+2+2+3+2+3+3+4=21.


Sample Input 2

2 100

Sample Output 2

5050

Sample Input 3

20 24

Sample Output 3

707081320