C - Maritozzo

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

配点 : 200

問題文

英小文字からなる 3 つの文字列 S_1, S_2, S_3 と、123 のみからなる文字列 T が与えられます。

T の各文字に対応する文字列を連結してできる文字列を出力してください。より厳密には、以下の指示にしたがって文字列を出力してください。

  • 1 \leq i \leq |T| を満たす整数 i に対し、文字列 s_i を次のように定める。
    • Ti 文字目が 1 のとき、S_1
    • Ti 文字目が 2 のとき、S_2
    • Ti 文字目が 3 のとき、S_3
  • s_1, s_2, \dots, s_{|T|} をこの順に連結してできる文字列を出力する。

制約

  • 1 \leq |S_1|, |S_2|, |S_3| \leq 10
  • 1 \leq |T| \leq 1000
  • S_1, S_2, S_3 は英小文字からなる。
  • T123 のみからなる。

入力

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

S_1
S_2
S_3
T

出力

答えを出力せよ。


入力例 1

mari
to
zzo
1321

出力例 1

marizzotomari

s_1 = mari, s_2 = zzo, s_3 = to, s_4 = mari であるので、これらを連結してできる文字列である marizzotomari を出力します。


入力例 2

abra
cad
abra
123

出力例 2

abracadabra

入力例 3

a
b
c
1

出力例 3

a

Score : 200 points

Problem Statement

You are given three strings S_1, S_2, S_3 consisting of lowercase English letters, and a string T consisting of 1, 2, 3.

Concatenate the three strings according to the characters in T and print the resulting string. Formally, conform to the following instructions.

  • For each integer i such that 1 \leq i \leq |T|, let the string s_i be defined as follows:
    • S_1, if the i-th character of T is 1;
    • S_2, if the i-th character of T is 2;
    • S_3, if the i-th character of T is 3.
  • Concatenate the strings s_1, s_2, \dots, s_{|T|} in this order and print the resulting string.

Constraints

  • 1 \leq |S_1|, |S_2|, |S_3| \leq 10
  • 1 \leq |T| \leq 1000
  • S_1, S_2, and S_3 consist of lowercase English letters.
  • T consists of 1, 2, and 3.

Input

Input is given from Standard Input in the following format:

S_1
S_2
S_3
T

Output

Print the answer.


Sample Input 1

mari
to
zzo
1321

Sample Output 1

marizzotomari

We have s_1 = mari, s_2 = zzo, s_3 = to, s_4 = mari. Concatenate these and print the resulting string: marizzotomari.


Sample Input 2

abra
cad
abra
123

Sample Output 2

abracadabra

Sample Input 3

a
b
c
1

Sample Output 3

a
D - Fill the Gaps

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

配点 : 200

問題文

正整数からなる長さ N の数列 A=(A_1,\ldots,A_N) があります。どの隣接する 2 項の値も相異なります。

この数列に対し、次の操作によりいくつか数を挿入します。

  1. 数列 A のどの隣接する 2 項の差の絶対値も 1 であるとき、操作を終了する。
  2. 数列 A の先頭から見て、隣接する 2 項の差の絶対値が 1 でない最初の箇所を A_i,A_{i+1} とする。
    • A_i < A_{i+1} ならば、A_iA_{i+1} の間に、A_i+1,A_i+2,\ldots,A_{i+1}-1 を挿入する。
    • A_i > A_{i+1} ならば、A_iA_{i+1} の間に、A_i-1,A_i-2,\ldots,A_{i+1}+1 を挿入する。
  3. 手順 1 に戻る。

操作が終了したときの数列を出力してください。

制約

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 100
  • A_i \neq A_{i+1}
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

操作が終了したときの数列の各要素を空白区切りで出力せよ。


入力例 1

4
2 5 1 2

出力例 1

2 3 4 5 4 3 2 1 2

最初、数列は (2,5,1,2) です。操作は以下のように行われます。

  • 1 項目の 22 項目の 5 の間に 3,4 を挿入する。数列は (2,3,4,5,1,2) となる。
  • 4 項目の 55 項目の 1 の間に 4,3,2 を挿入する。数列は (2,3,4,5,4,3,2,1,2) となる。

入力例 2

6
3 4 5 6 5 4

出力例 2

3 4 5 6 5 4

一度も挿入が行われないこともあります。

Score : 200 points

Problem Statement

We have a sequence of length N consisting of positive integers: A=(A_1,\ldots,A_N). Any two adjacent terms have different values.

Let us insert some numbers into this sequence by the following procedure.

  1. If every pair of adjacent terms in A has an absolute difference of 1, terminate the procedure.
  2. Let A_i, A_{i+1} be the pair of adjacent terms nearest to the beginning of A whose absolute difference is not 1.
    • If A_i < A_{i+1}, insert A_i+1,A_i+2,\ldots,A_{i+1}-1 between A_i and A_{i+1}.
    • If A_i > A_{i+1}, insert A_i-1,A_i-2,\ldots,A_{i+1}+1 between A_i and A_{i+1}.
  3. Return to step 1.

Print the sequence when the procedure ends.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 100
  • A_i \neq A_{i+1}
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the terms in the sequence when the procedure ends, separated by spaces.


Sample Input 1

4
2 5 1 2

Sample Output 1

2 3 4 5 4 3 2 1 2

The initial sequence is (2,5,1,2). The procedure goes as follows.

  • Insert 3,4 between the first term 2 and the second term 5, making the sequence (2,3,4,5,1,2).
  • Insert 4,3,2 between the fourth term 5 and the fifth term 1, making the sequence (2,3,4,5,4,3,2,1,2).

Sample Input 2

6
3 4 5 6 5 4

Sample Output 2

3 4 5 6 5 4

No insertions may be performed.

E - Cross

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

配点 : 300

問題文

H マス横 W マスのグリッドがあります。グリッドの上から i 行目、左から j 列目のマスを (i, j) と呼びます。
グリッドの各マスには #. のいずれかの文字が書かれています。(i, j) に書かれている文字を C[i][j] とします。また、整数 i, j1 \leq i \leq H1 \leq j \leq W の少なくとも一方を満たさない場合、 C[i][j]. と定義します。

正整数 a, b, n が以下の条件を全て満たす時、(a,b) および (a+d,b+d),(a+d,b-d),(a-d,b+d),(a-d,b-d) (1 \leq d \leq n)4n + 1 マスを (a,b) を中心とするサイズ n のバツ印 と呼びます。

  • C[a][b]# である。
  • 1 \leq d \leq n を満たす整数 d について、 C[a+d][b+d],C[a+d][b-d],C[a-d][b+d],C[a-d][b-d] はいずれも # である。
  • C[a+n+1][b+n+1],C[a+n+1][b-n-1],C[a-n-1][b+n+1],C[a-n-1][b-n-1] のうち少なくとも 1 つは . である。

例えば次の図で示された例では、(2, 2) を中心とするサイズ 1 のバツ印と (3, 7) を中心とするサイズ 2 のバツ印がグリッド上にあります。

image

グリッドにはいくつかのバツ印があります。バツ印を構成するマス以外に # は書かれていません。
また、異なるバツ印を構成するマス同士は頂点を共有しません。以下の 2 つのグリッドは異なるバツ印を構成するマス同士が頂点を共有している例で、このようなグリッドの状態は入力として与えられません。 例えば左のグリッドでは (3, 3)(4, 4) が頂点を共有しているのが条件に反しています。

image2

N = \min(H, W) とします。また、サイズ n のバツ印の個数を S_n とします。S_1, S_2, \dots, S_N を計算してください。

制約

  • 3 \leq H, W \leq 100
  • C[i][j]# または .
  • 異なるバツ印を構成するマス同士は頂点を共有しない
  • H, W は整数

入力

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

H W
C[1][1]C[1][2]\dots C[1][W]
C[2][1]C[2][2]\dots C[2][W]
\vdots
C[H][1]C[H][2]\dots C[H][W]

出力

S_1, S_2, \dots, S_N を空白区切りで出力せよ。


入力例 1

5 9
#.#.#...#
.#...#.#.
#.#...#..
.....#.#.
....#...#

出力例 1

1 1 0 0 0

問題文に書かれた説明の通り、(2, 2) を中心とするサイズ 1 のバツ印と (3, 7) を中心とするサイズ 2 のバツ印が書かれています。


入力例 2

3 3
...
...
...

出力例 2

0 0 0

バツ印が 1 個も書かれていない場合もあります。


入力例 3

3 16
#.#.....#.#..#.#
.#.......#....#.
#.#.....#.#..#.#

出力例 3

3 0 0

入力例 4

15 20
#.#..#.............#
.#....#....#.#....#.
#.#....#....#....#..
........#..#.#..#...
#.....#..#.....#....
.#...#....#...#..#.#
..#.#......#.#....#.
...#........#....#.#
..#.#......#.#......
.#...#....#...#.....
#.....#..#.....#....
........#.......#...
#.#....#....#.#..#..
.#....#......#....#.
#.#..#......#.#....#

出力例 4

5 0 1 0 0 0 1 0 0 0 0 0 0 0 0

Score : 300 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. We denote by (i, j) the cell at the i-th row from the top and j-th column from the left of the grid.
Each cell in the grid has a symbol # or . written on it. Let C[i][j] be the character written on (i, j). For integers i and j such that at least one of 1 \leq i \leq H and 1 \leq j \leq W is violated, we define C[i][j] to be ..

(4n+1) squares, consisting of (a, b) and (a+d,b+d),(a+d,b-d),(a-d,b+d),(a-d,b-d) (1 \leq d \leq n, 1 \leq n), are said to be a cross of size n centered at (a,b) if and only if all of the following conditions are satisfied:

  • C[a][b] is #.
  • C[a+d][b+d],C[a+d][b-d],C[a-d][b+d], and C[a-d][b-d] are all #, for all integers d such that 1 \leq d \leq n,
  • At least one of C[a+n+1][b+n+1],C[a+n+1][b-n-1],C[a-n-1][b+n+1], and C[a-n-1][b-n-1] is ..

For example, the grid in the following figure has a cross of size 1 centered at (2, 2) and another of size 2 centered at (3, 7).

image

The grid has some crosses. No # is written on the cells except for those comprising a cross.
Additionally, no two squares that comprise two different crosses share a corner. The two grids in the following figure are the examples of grids where two squares that comprise different crosses share a corner; such grids are not given as an input. For example, the left grid is invalid because (3, 3) and (4, 4) share a corner.

image2

Let N = \min(H, W), and S_n be the number of crosses of size n. Find S_1, S_2, \dots, S_N.

Constraints

  • 3 \leq H, W \leq 100
  • C[i][j] is # or ..
  • No two different squares that comprise two different crosses share a corner.
  • H and W are integers.

Input

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

H W
C[1][1]C[1][2]\dots C[1][W]
C[2][1]C[2][2]\dots C[2][W]
\vdots
C[H][1]C[H][2]\dots C[H][W]

Output

Print S_1, S_2, \dots, and S_N, separated by spaces.


Sample Input 1

5 9
#.#.#...#
.#...#.#.
#.#...#..
.....#.#.
....#...#

Sample Output 1

1 1 0 0 0

As described in the Problem Statement, there are a cross of size 1 centered at (2, 2) and another of size 2 centered at (3, 7).


Sample Input 2

3 3
...
...
...

Sample Output 2

0 0 0

There may be no cross.


Sample Input 3

3 16
#.#.....#.#..#.#
.#.......#....#.
#.#.....#.#..#.#

Sample Output 3

3 0 0

Sample Input 4

15 20
#.#..#.............#
.#....#....#.#....#.
#.#....#....#....#..
........#..#.#..#...
#.....#..#.....#....
.#...#....#...#..#.#
..#.#......#.#....#.
...#........#....#.#
..#.#......#.#......
.#...#....#...#.....
#.....#..#.....#....
........#.......#...
#.#....#....#.#..#..
.#....#......#....#.
#.#..#......#.#....#

Sample Output 4

5 0 1 0 0 0 1 0 0 0 0 0 0 0 0
F - RANDOM

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

配点 : 300

問題文

#. からなる HW 列の図形 S,T が与えられます。
図形 SH 個の文字列として与えられ、 S_ij 文字目は Sij 列にある要素を表します。 T についても同様です。

S の列を並べ替えて T と等しくできるか判定してください。

但し、図形 X の列を並べ替えるとは、以下の操作を言います。

  • (1,2,\dots,W) の順列 P=(P_1,P_2,\dots,P_W) をひとつ選択する。
  • その後、全ての 1 \le i \le H を満たす整数 i について、以下の操作を同時に行う。
    • 1 \le j \le W を満たす全ての整数 j について同時に、 Xij 列にある要素を iP_j 列にある要素に置き換える。

制約

  • H,W は整数
  • 1 \le H,W
  • 1 \le H \times W \le 4 \times 10^5
  • S_i,T_i#. からなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H
T_1
T_2
\vdots
T_H

出力

ST と等しくできるなら Yes 、 そうでないなら No と出力せよ。


入力例 1

3 4
##.#
##..
#...
.###
..##
...#

出力例 1

Yes

例えば S3,4,2,1 列目をこの順に左から並べ替えた場合、 ST と等しくできます。


入力例 2

3 3
#.#
.#.
#.#
##.
##.
.#.

出力例 2

No

この入力では、 ST と等しくすることができません。


入力例 3

2 1
#
.
#
.

出力例 3

Yes

S=T である場合もあります。


入力例 4

8 7
#..#..#
.##.##.
#..#..#
.##.##.
#..#..#
.##.##.
#..#..#
.##.##.
....###
####...
....###
####...
....###
####...
....###
####...

出力例 4

Yes

Score : 300 points

Problem Statement

You are given patterns S and T consisting of # and ., each with H rows and W columns.
The pattern S is given as H strings, and the j-th character of S_i represents the element at the i-th row and j-th column. The same goes for T.

Determine whether S can be made equal to T by rearranging the columns of S.

Here, rearranging the columns of a pattern X is done as follows.

  • Choose a permutation P=(P_1,P_2,\dots,P_W) of (1,2,\dots,W).
  • Then, for every integer i such that 1 \le i \le H, simultaneously do the following.
    • For every integer j such that 1 \le j \le W, simultaneously replace the element at the i-th row and j-th column of X with the element at the i-th row and P_j-th column.

Constraints

  • H and W are integers.
  • 1 \le H,W
  • 1 \le H \times W \le 4 \times 10^5
  • S_i and T_i are strings of length W consisting of # and ..

Input

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

H W
S_1
S_2
\vdots
S_H
T_1
T_2
\vdots
T_H

Output

If S can be made equal to T, print Yes; otherwise, print No.


Sample Input 1

3 4
##.#
##..
#...
.###
..##
...#

Sample Output 1

Yes

If you, for instance, arrange the 3-rd, 4-th, 2-nd, and 1-st columns of S in this order from left to right, S will be equal to T.


Sample Input 2

3 3
#.#
.#.
#.#
##.
##.
.#.

Sample Output 2

No

In this input, S cannot be made equal to T.


Sample Input 3

2 1
#
.
#
.

Sample Output 3

Yes

It is possible that S=T.


Sample Input 4

8 7
#..#..#
.##.##.
#..#..#
.##.##.
#..#..#
.##.##.
#..#..#
.##.##.
....###
####...
....###
####...
....###
####...
....###
####...

Sample Output 4

Yes
G - Count Subtractions

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

配点 : 400

問題文

正整数 A,B が与えられます。

あなたは、A=B になるまで以下の操作を繰り返します。

  • A,B の大小関係に応じて、次の 2 個のうちどちらかの処理を行う。
    • A > B ならば、AA-B で置き換える。
    • A < B ならば、BB-A で置き換える。

A=B になるまで、操作を何回行うか求めてください。ただし、有限回の操作で A=B になることが保証されます。

制約

  • 1 \le A,B \le 10^{18}
  • 入力はすべて整数

入力

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

A B

出力

答えを出力せよ。


入力例 1

3 8

出力例 1

4

始め、A=3,B=8 です。操作は以下のように行われます。

  • A<B であるため、BB-A=5 で置き換える。A=3,B=5 となる。
  • A<B であるため、BB-A=2 で置き換える。A=3,B=2 となる。
  • A>B であるため、AA-B=1 で置き換える。A=1,B=2 となる。
  • A<B であるため、BB-A=1 で置き換える。A=1,B=1 となる。

よって、操作回数は 4 回です。


入力例 2

1234567890 1234567890

出力例 2

0

入力が 32bit 整数型に収まらないことがあることに注意してください。


入力例 3

1597 987

出力例 3

15

Score : 400 points

Problem Statement

You are given positive integers A and B.

You will repeat the following operation until A=B:

  • compare A and B to perform one of the following two:
    • if A > B, replace A with A-B;
    • if A < B, replace B with B-A.

How many times will you repeat it until A=B? It is guaranteed that a finite repetition makes A=B.

Constraints

  • 1 \le A,B \le 10^{18}
  • All values in the input are integers.

Input

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

A B

Output

Print the answer.


Sample Input 1

3 8

Sample Output 1

4

Initially, A=3 and B=8. You repeat the operation as follows:

  • A<B, so replace B with B-A=5, making A=3 and B=5.
  • A<B, so replace B with B-A=2, making A=3 and B=2.
  • A>B, so replace A with A-B=1, making A=1 and B=2.
  • A<B, so replace B with B-A=1, making A=1 and B=1.

Thus, you repeat it four times.


Sample Input 2

1234567890 1234567890

Sample Output 2

0

Note that the input may not fit into a 32-bit integer type.


Sample Input 3

1597 987

Sample Output 3

15