A - Last Card

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

1,2,\ldots,N の番号のついた N 人の人に合計 K 枚のカードを配ります。

A から始めて 人 A,A+1,A+2,\ldots,N,1,2,\ldots の順に 1 枚ずつカードを配るとき、最後のカードは誰に配られるでしょうか?

厳密には、人 x(1 \leq x < N) の次には人 x+1 にカードを配り、人 N の次には人 1 にカードを配ります。

制約

  • 1 \leq N,K \leq 1000
  • 1 \leq A \leq N
  • 入力は全て整数

入力

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

N K A

出力

最後のカードが配られた人の番号を出力せよ。


入力例 1

3 3 2

出力例 1

1

2、人 3、人 1 の順にカードを配ります。


入力例 2

1 100 1

出力例 2

1

入力例 3

3 14 2

出力例 3

3

Score : 100 points

Problem Statement

We will hand out a total of K cards to N people numbered 1, 2, \ldots, N.

Beginning with Person A, we will give the cards one by one to the people in this order: A, A+1, A+2, \ldots, N, 1, 2, \ldots. Who will get the last card?

Formally, after Person x(1 \leq x < N) gets a card, Person x+1 will get a card. After Person N gets a card, Person 1 gets a card.

Constraints

  • 1 \leq N,K \leq 1000
  • 1 \leq A \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K A

Output

Print a number representing the person who will get the last card.


Sample Input 1

3 3 2

Sample Output 1

1

The cards are given to Person 2, 3, 1 in this order.


Sample Input 2

1 100 1

Sample Output 2

1

Sample Input 3

3 14 2

Sample Output 3

3
B - Raise Both Hands

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君はたこ焼きを作ってすぬけ君に振る舞うことにしました。高橋君はすぬけ君に、たこ焼きを食べたいならば左手のみを挙げて、そうでないならば右手のみを挙げるよう指示しました。

すぬけ君が挙げた手の情報は整数 L, R によって与えられます。 すぬけ君は L = 1 のとき、またそのときに限り左手を挙げており、R = 1 のとき、またそのときに限り右手を挙げています。すぬけ君は指示に従わず、両手を挙げることも手を挙げないこともあります。

すぬけ君が片方の手のみを挙げている場合、すぬけ君がたこ焼きを食べたいならば Yes を、そうでないならば No を出力してください。すぬけ君が両手を挙げているか、手を挙げていないときは Invalid と出力してください。

ただし、すぬけ君が片方の手のみを挙げている場合は必ず指示に従った行動を取っているものとします。

制約

  • L, R0 または 1

入力

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

L R

出力

問題文の指示に従って Yes, No, Invalid のいずれかを出力せよ。


入力例 1

1 0

出力例 1

Yes

すぬけ君はたこ焼きを食べたいので左手のみを挙げています。


入力例 2

1 1

出力例 2

Invalid

すぬけ君は両手を挙げています。

Score : 100 points

Problem Statement

Takahashi decided to make takoyaki (octopus balls) and serve it to Snuke. Takahashi instructed Snuke to raise only his left hand if he wants to eat takoyaki, and only his right hand otherwise.

You are given the information about which hand Snuke is raising as two integers L and R. He is raising his left hand if and only if L = 1, and raising his right hand if and only if R = 1. He might not follow the instructions and could raise both hands or not raise any hand at all.

If Snuke is raising only one hand, print Yes if he wants to eat takoyaki, and No if he does not. If he is raising both hands or not raising any hand, print Invalid.

Assume that if Snuke is raising only one hand, he is always following the instructions.

Constraints

  • Each of L and R is 0 or 1.

Input

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

L R

Output

Print Yes, No, or Invalid according to the instructions in the problem statement.


Sample Input 1

1 0

Sample Output 1

Yes

Snuke wants to eat takoyaki, so he is raising only his left hand.


Sample Input 2

1 1

Sample Output 2

Invalid

Snuke is raising both hands.

C - Bombs

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

R 行横 C 列の盤面があります。上から i 行目、左から j 列目のマスを (i,j) と表します。

(i,j) の現在の状態が文字 B_{i,j} として与えられます。 . は空きマス、# は壁があるマスを表し、 1, 2,\dots, 9 はそれぞれ威力 1,2,\dots,9 の爆弾があるマスを表します。

次の瞬間に、全ての爆弾が同時に爆発します。 爆弾が爆発すると、爆弾があるマスからのマンハッタン距離がその爆弾の威力以下であるような全てのマス(その爆弾があるマス自体を含む)が空きマスに変わります。 ここで、(r_1,c_1) から (r_2,c_2) までのマンハッタン距離は |r_1-r_2|+|c_1-c_2| です。

爆発後の盤面を出力してください。

制約

  • 1\leq R,C \leq 20
  • R,C は整数
  • B_{i,j}., #, 1, 2,\dots, 9 のいずれかである

入力

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

R C
B_{1,1}B_{1,2}\dots B_{1,C}
\vdots
B_{R,1}B_{R,2}\dots B_{R,C}

出力

爆発後の盤面を R 行で出力せよ。盤面の表し方は入力と同じ形式を用いること (RC を出力する必要はない)。


入力例 1

4 4
.1.#
###.
.#2.
#.##

出力例 1

...#
#...
....
#...

爆弾の効果範囲を表す図

  • (1,2) にある爆弾の爆発によって、上図の青いマスと紫のマスが空きマスに変わります。
  • (3,3) にある爆弾の爆発によって、上図の赤いマスと紫のマスが空きマスに変わります。

この例のように、爆弾が効果を及ぼす範囲に被りがあることもあります。


入力例 2

2 5
..#.#
###.#

出力例 2

..#.#
###.#

爆弾が 1 つもないこともあります。


入力例 3

2 3
11#
###

出力例 3

...
..#

入力例 4

4 6
#.#3#.
###.#.
##.###
#1..#.

出力例 4

......
#.....
#....#
....#.

Score : 200 points

Problem Statement

We have a board with R rows running horizontally and C columns running vertically. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

You are given characters B_{i,j} representing the current states of (i,j). . represents an empty square; # represents a square with a wall; 1, 2,\dots, 9 represent a square with a bomb of power 1,2,\dots,9, respectively.

At the next moment, all bombs will explode simultaneously. When a bomb explodes, every square whose Manhattan distance from the square with the bomb is not greater than the power of the bomb will turn into an empty square. Here, the Manhattan distance from (r_1,c_1) to (r_2,c_2) is |r_1-r_2|+|c_1-c_2|.

Print the board after the explosions.

Constraints

  • 1\leq R,C \leq 20
  • R and C are integers.
  • Each B_{i,j} is one of ., #, 1, 2, \dots, 9.

Input

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

R C
B_{1,1}B_{1,2}\dots B_{1,C}
\vdots
B_{R,1}B_{R,2}\dots B_{R,C}

Output

Print R lines representing the board after the explosions. Use the format used in the input (do not print R or C).


Sample Input 1

4 4
.1.#
###.
.#2.
#.##

Sample Output 1

...#
#...
....
#...

Figure representing the explosion ranges of bombs

  • The explosion of the bomb at (1,2) will turn the blue squares and purple squares in the above figure into empty squares.
  • The explosion of the bomb at (3,3) will turn the red squares and purple squares in the above figure into empty squares.

As seen in this sample, the explosion ranges of bombs may overlap.


Sample Input 2

2 5
..#.#
###.#

Sample Output 2

..#.#
###.#

There may be no bombs.


Sample Input 3

2 3
11#
###

Sample Output 3

...
..#

Sample Input 4

4 6
#.#3#.
###.#.
##.###
#1..#.

Sample Output 4

......
#.....
#....#
....#.
D - Music Player

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は音楽プレイヤーを持っています。はじめ、音量は 0 であり、曲は停止中です。

これから、Q 回の操作を順に行います。i 回目の操作は整数 A_i によって表され、操作の内容は以下の通りです。

  • A_i = 1 のとき、音量を 1 上げる。
  • A_i = 2 のとき、現在の音量が 1 以上であれば音量を 1 下げ、0 であれば何もしない。
  • A_i = 3 のとき、曲が停止中であれば曲を再生し、曲が再生中であれば曲を停止する。

i = 1, 2, \ldots, Q に対して、以下の問題を解いてください。

  • i 回目の操作を終えた直後に音量 3 以上で音楽が再生されているか判定せよ。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • A_i \in \lbrace 1, 2, 3 \rbrace
  • 入力される値はすべて整数

入力

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

Q
A_1
A_2
\vdots
A_Q

出力

Q 行出力せよ。i 行目には、i 回目の操作を終えた直後に音量 3 以上で音楽が再生されているならば Yes を、そうでないならば No を出力せよ。


入力例 1

10
2
1
3
1
3
1
1
3
2
2

出力例 1

No
No
No
No
No
No
No
Yes
Yes
No
  • 1 回目の操作を終えた後、音量は 0 で曲は停止中です。

  • 2 回目の操作を終えた後、音量は 1 で曲は停止中です。

  • 3 回目の操作を終えた後、音量は 1 で曲は再生中です。

  • 4 回目の操作を終えた後、音量は 2 で曲は再生中です。

  • 5 回目の操作を終えた後、音量は 2 で曲は停止中です。

  • 6 回目の操作を終えた後、音量は 3 で曲は停止中です。

  • 7 回目の操作を終えた後、音量は 4 で曲は停止中です。

  • 8 回目の操作を終えた後、音量は 4 で曲は再生中です。

  • 9 回目の操作を終えた後、音量は 3 で曲は再生中です。

  • 10 回目の操作を終えた後、音量は 2 で曲は再生中です。

Score : 200 points

Problem Statement

Takahashi has a music player. Initially, the volume is 0 and the music is stopped.

From now on, Q operations will be performed in order. The i-th operation is represented by an integer A_i, which means the following:

  • If A_i = 1, increase the volume by 1.
  • If A_i = 2, if the current volume is 1 or more, decrease it by 1; if it is 0, do nothing.
  • If A_i = 3, if the music is stopped, play it; if the music is playing, stop it.

For i = 1, 2, \ldots, Q, solve the following problem:

  • Determine whether the music is playing at volume 3 or more immediately after the i-th operation.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • A_i \in \lbrace 1, 2, 3 \rbrace
  • All input values are integers.

Input

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

Q
A_1
A_2
\vdots
A_Q

Output

Output Q lines. The i-th line should contain Yes if the music is playing at volume 3 or more immediately after the i-th operation, and No otherwise.


Sample Input 1

10
2
1
3
1
3
1
1
3
2
2

Sample Output 1

No
No
No
No
No
No
No
Yes
Yes
No
  • After the 1-st operation, the volume is 0 and the music is stopped.

  • After the 2-nd operation, the volume is 1 and the music is stopped.

  • After the 3-rd operation, the volume is 1 and the music is playing.

  • After the 4-th operation, the volume is 2 and the music is playing.

  • After the 5-th operation, the volume is 2 and the music is stopped.

  • After the 6-th operation, the volume is 3 and the music is stopped.

  • After the 7-th operation, the volume is 4 and the music is stopped.

  • After the 8-th operation, the volume is 4 and the music is playing.

  • After the 9-th operation, the volume is 3 and the music is playing.

  • After the 10-th operation, the volume is 2 and the music is playing.

E - Error Correction

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は英小文字からなる文字列 T を青木君に向けて送りました。その結果、青木君は英小文字からなる文字列 T' を受信しました。

T'T から一部が変更されてしまっている可能性があり、具体的には、下記の 4 つのうちのちょうど 1 つが成り立つことがわかっています。

  • T' は、T と等しい。
  • T' は、T のいずれか 1 つの位置(先頭と末尾も含む)に英小文字を 1 つ挿入して得られる文字列である。
  • T' は、T からある 1 文字を削除して得られる文字列である。
  • T' は、T のある 1 文字を別の英小文字に変更して得られる文字列である。

青木君が受信した文字列 T' と、英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N が入力として与えられるので、 S_1, S_2, \ldots, S_N のうち、高橋君が送った文字列 T と等しい可能性があるものをすべて求めてください。

制約

  • N は整数
  • 1 \leq N \leq 5 \times 10^5
  • S_iT' は英小文字からなる長さ 1 以上 5 \times 10^5 以下の文字列
  • S_1, S_2, \ldots, S_N の長さの総和は 5 \times 10^5 以下

入力

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

N T'
S_1
S_2
\vdots
S_N

出力

S_1, S_2, \ldots, S_N のうち T と等しい可能性があるものすべての添字を昇順に並べた列を (i_1, i_2, \ldots, i_K) とする。 この列の長さ K および列自体を、下記の形式にしたがって出力せよ。

K
i_1 i_2 \ldots i_K

入力例 1

5 ababc
ababc
babc
abacbc
abdbc
abbac

出力例 1

4
1 2 3 4

S_1, S_2, \ldots, S_5 のうち、T と等しい可能性があるものは S_1, S_2, S_3, S_44 つであることが下記の通りわかります。

  • S_1T と等しい可能性があります。なぜなら、T' = ababcS_1 = ababc と等しいからです。
  • S_2T と等しい可能性があります。なぜなら、T' = ababcS_2 = babc の先頭に文字 a を挿入して得られる文字列だからです。
  • S_3T と等しい可能性があります。なぜなら、T' = ababcS_3 = abacbc から 4 文字目の c を削除して得られる文字列だからです。
  • S_4T と等しい可能性があります。なぜなら、T' = ababcS_4 = abdbc3 文字目の db に変更して得られる文字列だからです。
  • S_5T と等しい可能性はありません。なぜなら、S_5 = abbacT としたとき、T' = ababc は問題文中の 4 つの条件をいずれも満たさないからです。

入力例 2

1 aoki
takahashi

出力例 2

0


入力例 3

9 atcoder
atoder
atcode
athqcoder
atcoder
tacoder
jttcoder
atoder
atceoder
atcoer

出力例 3

6
1 2 4 7 8 9

Score : 300 points

Problem Statement

Takahashi sent a string T consisting of lowercase English letters to Aoki. As a result, Aoki received a string T' consisting of lowercase English letters.

T' may have been altered from T. Specifically, exactly one of the following four conditions is known to hold.

  • T' is equal to T.
  • T' is a string obtained by inserting one lowercase English letter at one position (possibly the beginning and end) in T.
  • T' is a string obtained by deleting one character from T.
  • T' is a string obtained by changing one character in T to another lowercase English letter.

You are given the string T' received by Aoki and N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters. Find all the strings among S_1, S_2, \ldots, S_N that could equal the string T sent by Takahashi.

Constraints

  • N is an integer.
  • 1 \leq N \leq 5 \times 10^5
  • S_i and T' are strings of length between 1 and 5 \times 10^5, inclusive, consisting of lowercase English letters.
  • The total length of S_1, S_2, \ldots, S_N is at most 5 \times 10^5.

Input

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

N T'
S_1
S_2
\vdots
S_N

Output

Let (i_1, i_2, \ldots, i_K) be the sequence of indices of all the strings among S_1, S_2, \ldots, S_N that could be equal to T, in ascending order. Print the length K of this sequence, and the sequence itself, in the following format:

K
i_1 i_2 \ldots i_K

Sample Input 1

5 ababc
ababc
babc
abacbc
abdbc
abbac

Sample Output 1

4
1 2 3 4

Among S_1, S_2, \ldots, S_5, the strings that could be equal to T are S_1, S_2, S_3, S_4, as explained below.

  • S_1 could be equal to T, because T' = ababc is equal to S_1 = ababc.
  • S_2 could be equal to T, because T' = ababc is obtained by inserting the letter a at the beginning of S_2 = babc.
  • S_3 could be equal to T, because T' = ababc is obtained by deleting the fourth character c from S_3 = abacbc.
  • S_4 could be equal to T, because T' = ababc is obtained by changing the third character d in S_4 = abdbc to b.
  • S_5 could not be equal to T, because if we take S_5 = abbac as T, then T' = ababc does not satisfy any of the four conditions in the problem statement.

Sample Input 2

1 aoki
takahashi

Sample Output 2

0


Sample Input 3

9 atcoder
atoder
atcode
athqcoder
atcoder
tacoder
jttcoder
atoder
atceoder
atcoer

Sample Output 3

6
1 2 4 7 8 9