A - Snuke's favorite YAKINIKU

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

りんごさんは、すぬけ君にプレゼントを贈ろうとしています。

すぬけ君の好物が焼肉であることを知ったりんごさんは、すぬけ君は名前が YAKI から始まるものを好み、そうでないものを好まないと判断しました。

りんごさんが贈ろうと思っているものの名前を表す文字列 S が与えられます。SYAKI から始まるかどうか判定してください。

制約

  • 1 \leq |S| \leq 10
  • S は英大文字からなる

入力

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

S

出力

SYAKI から始まるなら Yes を、そうでないなら No を出力せよ。


入力例 1

YAKINIKU

出力例 1

Yes

YAKINIKUYAKI で始まります。


入力例 2

TAKOYAKI

出力例 2

No

TAKOYAKIYAKI で始まりません。


入力例 3

YAK

出力例 3

No

Score : 100 points

Problem Statement

Ringo is giving a present to Snuke.

Ringo has found out that Snuke loves yakiniku (a Japanese term meaning grilled meat. yaki: grilled, niku: meat). He supposes that Snuke likes grilled things starting with YAKI in Japanese, and does not like other things.

You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with YAKI.

Constraints

  • 1 \leq |S| \leq 10
  • S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S starts with YAKI, print Yes; otherwise, print No.


Sample Input 1

YAKINIKU

Sample Output 1

Yes

YAKINIKU starts with YAKI.


Sample Input 2

TAKOYAKI

Sample Output 2

No

TAKOYAKI (a Japanese snack. tako: octopus) does not start with YAKI.


Sample Input 3

YAK

Sample Output 3

No
B - fLIP

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

NM 列のマス目があり、最初は全てのマスが白いです。

各行各列には 1 つずつボタンがあります。 ある行のボタンを押すと、その行のマスの色が全て反転します。すなわち、白なら黒、黒なら白に色が変わります。 また、ある列のボタンを押すと、その列のマスの色が全て反転します。

高橋君は、ボタンを押す操作を好きな回数行うことができます。黒く塗られたマスの個数をちょうど K 個にすることができるかどうか判定してください。

制約

  • 1 \leq N,M \leq 1000
  • 0 \leq K \leq NM

入力

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

N M K

出力

黒く塗られたマスの個数をちょうど K 個にできるなら Yes を、そうでないなら No を出力せよ。


入力例 1

2 2 2

出力例 1

Yes

1 行目、 1 列目の順にボタンを押せばよいです。


入力例 2

2 2 1

出力例 2

No

入力例 3

3 5 8

出力例 3

Yes

1 列目、3 列目、2 行目、5 列目の順にボタンを押せばよいです。


入力例 4

7 9 20

出力例 4

No

Score : 200 points

Problem Statement

We have a grid with N rows and M columns of squares. Initially, all the squares are white.

There is a button attached to each row and each column. When a button attached to a row is pressed, the colors of all the squares in that row are inverted; that is, white squares become black and vice versa. When a button attached to a column is pressed, the colors of all the squares in that column are inverted.

Takahashi can freely press the buttons any number of times. Determine whether he can have exactly K black squares in the grid.

Constraints

  • 1 \leq N,M \leq 1000
  • 0 \leq K \leq NM

Input

Input is given from Standard Input in the following format:

N M K

Output

If Takahashi can have exactly K black squares in the grid, print Yes; otherwise, print No.


Sample Input 1

2 2 2

Sample Output 1

Yes

Press the buttons in the order of the first row, the first column.


Sample Input 2

2 2 1

Sample Output 2

No

Sample Input 3

3 5 8

Sample Output 3

Yes

Press the buttons in the order of the first column, third column, second row, fifth column.


Sample Input 4

7 9 20

Sample Output 4

No
C - Palindromic Matrix

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 400

問題文

H 行、横 W 列の行列 A があります。 上から i 行目、左から j 列目の要素を a_{ij} とします。 各 a_{ij} は英小文字です。

すぬけ君は、A の要素を自由に並べ替え、縦 H 行、横 W 列の行列 A' を作ろうとしています。 このとき、次の条件が成り立つようにします。

  • A' のどの行およびどの列もそれぞれ回文になっている。

条件を満たす A' が存在するか判定してください。

注釈

回文とは、前後を反転しても変わらない文字列のことです。 例えば、a, aa, abba, abcba は回文ですが、ab, abab, abcda は回文ではありません。

制約

  • 1 ≤ H, W ≤ 100
  • a_{ij} は英小文字である。

入力

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

H W
a_{11}a_{12}...a_{1W}
:
a_{H1}a_{H2}...a_{HW}

出力

条件を満たす A' が存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

3 4
aabb
aabb
aacc

出力例 1

Yes

例えば、次の A' は条件を満たします。

abba
acca
abba

入力例 2

2 2
aa
bb

出力例 2

No

どのように A の要素を並べ替えても、条件を満たす A' を作れません。


入力例 3

5 1
t
w
e
e
t

出力例 3

Yes

例えば、次の A' は条件を満たします。

t
e
w
e
t

入力例 4

2 5
abxba
abyba

出力例 4

No

入力例 5

1 1
z

出力例 5

Yes

Score : 400 points

Problem Statement

We have an H-by-W matrix. Let a_{ij} be the element at the i-th row from the top and j-th column from the left. In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A. Here, he wants to satisfy the following condition:

  • Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward. For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

  • 1 ≤ H, W ≤ 100
  • a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11}a_{12}...a_{1W}
:
a_{H1}a_{H2}...a_{HW}

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.


Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.


Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes
D - Four Coloring

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 700

問題文

H 行、横 W 列のマス目があります。 上から i 行目、左から j 列目のマスを (i,\ j) と表します。 また、マス (i_1,\ j_1)(i_2,\ j_2) の間の距離を |i_1 - i_2| + |j_1 - j_2| と定義します。

すぬけ君は各マスを 赤 / 黄 / 緑 / 青 のいずれかの色で塗ろうとしています。 このとき、正の整数 d に対して、次の条件が成り立つようにします。

  • 距離がちょうど d であるようなマスのペアには、異なる色が塗られている。

条件を満たす色の塗り方をひとつ求めてください。 解は必ず存在することが示せます。

制約

  • 2 ≤ H, W ≤ 500
  • 1 ≤ d ≤ H + W - 2

入力

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

H W d

出力

条件を満たす色の塗り方をひとつ出力せよ。 色の塗り方は次のフォーマットで出力せよ。 マス (i,\ j) の色が 赤 / 黄 / 緑 / 青 ならば、c_{ij} はそれぞれ R / Y / G / B とせよ。

c_{11}c_{12}...c_{1W}
:
c_{H1}c_{H2}...c_{HW}

入力例 1

2 2 1

出力例 1

RY
GR

距離がちょうど 1 であるようなマスのペアは、次の 4 組です。 右側に示したように、どのペアにも異なる色が塗られています。

  • (1,\ 1)(1,\ 2) : RY
  • (1,\ 2)(2,\ 2) : YR
  • (2,\ 2)(2,\ 1) : RG
  • (2,\ 1)(1,\ 1) : GR

入力例 2

2 3 2

出力例 2

RYB
RGB

距離がちょうど 2 であるようなマスのペアは、次の 6 組です。 右側に示したように、どのペアにも異なる色が塗られています。

  • (1,\ 1)(1,\ 3) : RB
  • (1,\ 3)(2,\ 2) : BG
  • (2,\ 2)(1,\ 1) : GR
  • (2,\ 1)(2,\ 3) : RB
  • (2,\ 3)(1,\ 2) : BY
  • (1,\ 2)(2,\ 1) : YR

Score : 700 points

Problem Statement

We have a grid with H rows and W columns of squares. We will represent the square at the i-th row from the top and j-th column from the left as (i,\ j). Also, we will define the distance between the squares (i_1,\ j_1) and (i_2,\ j_2) as |i_1 - i_2| + |j_1 - j_2|.

Snuke is painting each square in red, yellow, green or blue. Here, for a given positive integer d, he wants to satisfy the following condition:

  • No two squares with distance exactly d have the same color.

Find a way to paint the squares satisfying the condition. It can be shown that a solution always exists.

Constraints

  • 2 ≤ H, W ≤ 500
  • 1 ≤ d ≤ H + W - 2

Input

Input is given from Standard Input in the following format:

H W d

Output

Print a way to paint the squares satisfying the condition, in the following format. If the square (i,\ j) is painted in red, yellow, green or blue, c_{ij} should be R, Y, G or B, respectively.

c_{11}c_{12}...c_{1W}
:
c_{H1}c_{H2}...c_{HW}

Sample Input 1

2 2 1

Sample Output 1

RY
GR

There are four pairs of squares with distance exactly 1. As shown below, no two such squares have the same color.

  • (1,\ 1), (1,\ 2) : R, Y
  • (1,\ 2), (2,\ 2) : Y, R
  • (2,\ 2), (2,\ 1) : R, G
  • (2,\ 1), (1,\ 1) : G, R

Sample Input 2

2 3 2

Sample Output 2

RYB
RGB

There are six pairs of squares with distance exactly 2. As shown below, no two such squares have the same color.

  • (1,\ 1) , (1,\ 3) : R , B
  • (1,\ 3) , (2,\ 2) : B , G
  • (2,\ 2) , (1,\ 1) : G , R
  • (2,\ 1) , (2,\ 3) : R , B
  • (2,\ 3) , (1,\ 2) : B , Y
  • (1,\ 2) , (2,\ 1) : Y , R
E - Modern Painting

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1600

問題文

現代美術に興味を持ったりんごさんは、CODE FESTIVAL 2017 の会場に作られた N+2M+2 列の盤面と、何人かの人を使って絵を描くことにしました。

盤面の上から i+1 行目、左から j+1 列目のマスは 2 つの整数の組 (i,j) であらわされます。すなわち、左上のマスが (0,0) で、右下のマスが (N+1,M+1) です。 最初、1 \leq x \leq N, 1 \leq y \leq M を満たすマス (x,y) は白で塗られており、それ以外の (外周の) マスは黒で塗られています。

りんごさんは、盤面の外周のマスのうちのいくつかに、人を内向きに配置しました。 より厳密には、配置の情報は 4 つの文字列 A,B,C,D によってあらわされ、以下のように配置が行われます。

  • 端以外の各行について、Ai(1 \leq i \leq N) 文字目が 1 のときマス (i,0) に、右を向いた人を 1 人配置する。そうでないとき、何もしない。
  • 端以外の各行について、Bi(1 \leq i \leq N) 文字目が 1 のときマス (i,M+1) に、左を向いた人を 1 人配置する。そうでないとき、何もしない。
  • 端以外の各列について、Ci(1 \leq i \leq M) 文字目が 1 のときマス (0,i) に、下を向いた人を 1 人配置する。そうでないとき、何もしない。
  • 端以外の各列について、Di(1 \leq i \leq M) 文字目が 1 のときマス (N+1,i) に、上を向いた人を 1 人配置する。そうでないとき、何もしない。

各人はそれぞれ、白でない色のペンキを充分な量持っています。どの相異なる 2 人の持っているペンキの色も、互いに異なります。

人の配置の例(便宜上、黒く塗られたマスを灰色で表しています)

りんごさんは、以下の一連の操作を、全ての人が会場から追い出されていなくなるまで繰り返します。

  • まだ追い出されていない人を 1 人選ぶ。
  • 選ばれた人は、目の前のマスが白で塗られている間、自分の向いている向きに 1 マス分進み、進んだ先のマスを自分の持っているペンキで塗る。目の前のマスが白で塗られていない場合、動作を終了する。
  • 動作を終了した人を会場から追い出す。

塗られ方の例

りんごさんが作ることのできる、最終的な盤面の塗られ方は何通りあるでしょうか。998244353 で割ったあまりを求めてください。

なお、 2 つの盤面の塗られ方が異なるとは、あるマスが存在し、そのマスの色が異なることを指します。

制約

  • 1 \leq N,M \leq 10^5
  • |A|=|B|=N
  • |C|=|D|=M
  • A,B,C,D01 からなる

入力

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

N M
A
B
C
D

出力

最終的な盤面の塗られ方の総数を 998244353 で割ったあまりを出力せよ。


入力例 1

2 2
10
01
10
01

出力例 1

6

以下の 6 通りの塗られ方があります。


入力例 2

2 2
11
11
11
11

出力例 2

32

入力例 3

3 4
111
111
1111
1111

出力例 3

1276

入力例 4

17 21
11001010101011101
11001010011010111
111010101110101111100
011010110110101000111

出力例 4

548356548

998244353 で割ったあまりを求めるのを忘れないようにしてください。


入力例 5

3 4
000
101
1111
0010

出力例 5

21

入力例 6

9 13
111100001
010101011
0000000000000
1010111111101

出力例 6

177856

入力例 7

23 30
01010010101010010001110
11010100100100101010101
000101001001010010101010101101
101001000100101001010010101000

出力例 7

734524988

Score : 1600 points

Problem Statement

Ringo got interested in modern art. He decided to draw a big picture on the board with N+2 rows and M+2 columns of squares constructed in the venue of CODE FESTIVAL 2017, using some people.

The square at the (i+1)-th row and (j+1)-th column in the board is represented by the pair of integers (i,j). That is, the top-left square is (0,0), and the bottom-right square is (N+1,M+1). Initially, the squares (x,y) satisfying 1 \leq x \leq N and 1 \leq y \leq M are painted white, and the other (outermost) squares are painted black.

Ringo arranged people at some of the outermost squares, facing inward. More specifically, the arrangement of people is represented by four strings A, B, C and D, as follows:

  • For each row except the top and bottom, if the i-th character (1 \leq i \leq N) in A is 1, place a person facing right at the square (i,0); otherwise, do nothing.
  • For each row except the top and bottom, if the i-th character (1 \leq i \leq N) in B is 1, place a person facing left at the square (i,M+1); otherwise, do nothing.
  • For each column except the leftmost and rightmost, if the i-th character (1 \leq i \leq M) in C is 1, place a person facing down at the square (0,i); otherwise, do nothing.
  • For each column except the leftmost and rightmost, if the i-th character (1 \leq i \leq M) in D is 1, place a person facing up at the square (N+1,i); otherwise, do nothing.

Each person has a sufficient amount of non-white paint. No two people have paint of the same color.

An example of an arrangement of people (For convenience, black squares are displayed in gray)

Ringo repeats the following sequence of operations until all people are dismissed from the venue.

  • Select a person who is still in the venue.
  • The selected person repeats the following action while the square in front of him/her is white: move one square forward, and paint the square he/she enters with his/her paint. When the square in front of him/her is not white, he/she stops doing the action.
  • The person is now dismissed from the venue.

An example of a way the board is painted

How many different states of the board can Ringo obtain at the end of the process? Find the count modulo 998244353.

Two states of the board are considered different when there is a square painted in different colors.

Constraints

  • 1 \leq N,M \leq 10^5
  • |A|=|B|=N
  • |C|=|D|=M
  • A, B, C and D consist of 0 and 1.

Input

Input is given from Standard Input in the following format:

N M
A
B
C
D

Output

Print the number of the different states of the board Ringo can obtain at the end of the process, modulo 998244353.


Sample Input 1

2 2
10
01
10
01

Sample Output 1

6

There are six possible states as shown below.


Sample Input 2

2 2
11
11
11
11

Sample Output 2

32

Sample Input 3

3 4
111
111
1111
1111

Sample Output 3

1276

Sample Input 4

17 21
11001010101011101
11001010011010111
111010101110101111100
011010110110101000111

Sample Output 4

548356548

Be sure to find the count modulo 998244353.


Sample Input 5

3 4
000
101
1111
0010

Sample Output 5

21

Sample Input 6

9 13
111100001
010101011
0000000000000
1010111111101

Sample Output 6

177856

Sample Input 7

23 30
01010010101010010001110
11010100100100101010101
000101001001010010101010101101
101001000100101001010010101000

Sample Output 7

734524988
F - Squeezing Slimes

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1600

問題文

A 匹のスライムが横一列に並んでいます。 最初、スライムの大きさはすべて 1 です。

すぬけ君は次の操作を繰り返し行うことができます。

  • 正の偶数 M をひとつ選ぶ。 位置が連続する M 匹のスライムを選び、それらのうち左から (1,\ 2) 番目、(3,\ 4) 番目、…、(M - 1,\ M) 番目のスライムをそれぞれペアにする。 そして、各ペアごとに 2 匹のスライムを合成して 1 匹のスライムにする。 ここで、合成後のスライムの大きさは、合成前のスライムの大きさの和とする。 また、合成後の M / 2 匹のスライムの順序は、合成前の M / 2 組のペアの順序のままである。

すぬけ君の目標は、スライムをちょうど N 匹にして、それらのうち左から i (1 ≤ i ≤ N) 番目のスライムの大きさをちょうど a_i にすることです。 すぬけ君が目標を達成するために必要な操作回数の最小値を求めてください。

なお、A は入力として与えられず、A = a_1 + a_2 + ... + a_N であるとします。

制約

  • 1 ≤ N ≤ 10^5
  • a_i は整数である。
  • 1 ≤ a_i ≤ 10^9

入力

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

N
a_1 a_2 ... a_N

出力

すぬけ君が目標を達成するために必要な操作回数の最小値を出力せよ。


入力例 1

2
3 3

出力例 1

2

次のように操作を行えばよいです。 操作対象のスライムを太字で表しています。

  • (1, 1, 1, 1, 1, 1) → (1, 2, 2, 1)
  • (1, 2, 2, 1) → (3, 3)

入力例 2

4
2 1 2 2

出力例 2

2

次のように操作を行えばよいです。

  • (1, 1, 1, 1, 1, 1, 1) → (2, 1, 1, 1, 1, 1)
  • (2, 1, 1, 1, 1, 1) → (2, 1, 2, 2)

入力例 3

1
1

出力例 3

0

入力例 4

10
3 1 4 1 5 9 2 6 5 3

出力例 4

10

Score : 1600 points

Problem Statement

There are A slimes lining up in a row. Initially, the sizes of the slimes are all 1.

Snuke can repeatedly perform the following operation.

  • Choose a positive even number M. Then, select M consecutive slimes and form M / 2 pairs from those slimes as follows: pair the 1-st and 2-nd of them from the left, the 3-rd and 4-th of them, ..., the (M-1)-th and M-th of them. Combine each pair of slimes into one larger slime. Here, the size of a combined slime is the sum of the individual slimes before combination. The order of the M / 2 combined slimes remain the same as the M / 2 pairs of slimes before combination.

Snuke wants to get to the situation where there are exactly N slimes, and the size of the i-th (1 ≤ i ≤ N) slime from the left is a_i. Find the minimum number of operations required to achieve his goal.

Note that A is not directly given as input. Assume A = a_1 + a_2 + ... + a_N.

Constraints

  • 1 ≤ N ≤ 10^5
  • a_i is an integer.
  • 1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of operations required to achieve Snuke's goal.


Sample Input 1

2
3 3

Sample Output 1

2

One way to achieve Snuke's goal is as follows. Here, the selected slimes are marked in bold.

  • (1, 1, 1, 1, 1, 1) → (1, 2, 2, 1)
  • (1, 2, 2, 1) → (3, 3)

Sample Input 2

4
2 1 2 2

Sample Output 2

2

One way to achieve Snuke's goal is as follows.

  • (1, 1, 1, 1, 1, 1, 1) → (2, 1, 1, 1, 1, 1)
  • (2, 1, 1, 1, 1, 1) → (2, 1, 2, 2)

Sample Input 3

1
1

Sample Output 3

0

Sample Input 4

10
3 1 4 1 5 9 2 6 5 3

Sample Output 4

10