C - Number Box

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

配点 : 200

問題文

正整数 N が与えられます。

NN 列のマス目があり、上から i 行目、左から j 列目のマスには数字 A_{i,j} が書かれています。

このマス目は上下および左右がつながっているものとします。つまり以下が全て成り立ちます。

  • (1,i) の上のマスは (N,i) であり、(N,i) の下のマスは (1,i) である。(1\le i\le N)
  • (i,1) の左のマスは (i,N) であり、(i,N) の右のマスは (i,1) である。(1\le i\le N)

高橋君は、上下左右および斜めの 8 方向のうちいずれかを初めに選びます。そして、好きなマスから決めた方向に 1 マス移動することを N-1 回繰り返します。

高橋君は N 個のマス上を移動することになりますが、高橋君が通ったマスに書かれている数字を左から通った順番に並べた整数としてあり得る最大のものを求めてください。

制約

  • 1 \le N \le 10
  • 1 \le A_{i,j} \le 9
  • 入力はすべて整数。

入力

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

出力

答えを出力せよ。


入力例 1

4
1161
1119
7111
1811

出力例 1

9786

高橋君が上から 2 行目、左から 4 列目のマスから出発し、右下に進むことで、通ったマスに書かれた数字を並べ 9786 を作ることができます。 9786 より大きい値を作ることはできないため、9786 が解です。


入力例 2

10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111

出力例 2

1111111111

32bit整数型に答えが収まるとは限らないことに注意してください。

Score : 200 points

Problem Statement

You are given a positive integer N.

We have a grid with N rows and N columns, where the square at the i-th row from the top and j-th column from the left has a digit A_{i,j} written on it.

Assume that the upper and lower edges of this grid are connected, as well as the left and right edges. In other words, all of the following holds.

  • (N,i) is just above (1,i), and (1,i) is just below (N,i). (1\le i\le N).
  • (i,N) is just to the left of (i,1), and (i,1) is just to the right of (i,N). (1\le i\le N).

Takahashi will first choose one of the following eight directions: up, down, left, right, and the four diagonal directions. Then, he will start on a square of his choice and repeat moving one square in the chosen direction N-1 times.

In this process, Takahashi visits N squares. Find the greatest possible value of the integer that is obtained by arranging the digits written on the squares visited by Takahashi from left to right in the order visited by him.

Constraints

  • 1 \le N \le 10
  • 1 \le A_{i,j} \le 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

Output

Print the answer.


Sample Input 1

4
1161
1119
7111
1811

Sample Output 1

9786

If Takahashi starts on the square at the 2-nd row from the top and 4-th column from the left and goes down and to the right, the integer obtained by arranging the digits written on the visited squares will be 9786. It is impossible to make a value greater than 9786, so the answer is 9786.


Sample Input 2

10
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111
1111111111

Sample Output 2

1111111111

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

D - TaK Code

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

配点 : 200

問題文

高橋君は 2 次元コード TaK Code を考案しました。以下の条件を全て満たすものが TaK Code です。

  • 9 マス 横 9 マスの領域である
  • 左上及び右下の縦 3 マス 横 3 マスの領域に含まれる計 18 マスは全て黒である
  • 左上及び右下の縦 3 マス 横 3 マスの領域に八方位で隣接する計 14 マスは全て白である

TaK Code を回転させることはできません。

N マス、横 M マスのグリッドがあります。 グリッドの状態は N 個の長さ M の文字列 S_1,\ldots,S_N で与えられ、上から i 行目左から j 列目のマスは S_ij 文字目が # のとき黒、. のとき白です。

グリッドに完全に含まれる縦 9 マス横 9 マスの領域で、TaK Code の条件を満たす箇所を全て求めてください。

制約

  • 9 \leq N,M \leq 100
  • N,M は整数である
  • S_i. および # のみからなる長さ M の文字列である

入力

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

N M
S_1
\vdots
S_N

出力

上から i 行目左から j 列目のマスを左上とする縦 9 マス 横 9 マスの領域が TaK Code の条件を満たす (i,j) の組全てを辞書順の昇順で 1 行に 1 組ずつ、i,j をこの順に空白区切りで出力せよ。
(i,j) の組の辞書順の昇順とは、i の昇順、i が等しい組は j の昇順にすることである。


入力例 1

19 18
###......###......
###......###......
###..#...###..#...
..............#...
..................
..................
......###......###
......###......###
......###......###
.###..............
.###......##......
.###..............
............###...
...##.......###...
...##.......###...
.......###........
.......###........
.......###........
........#.........

出力例 1

1 1
1 10
7 7
10 2

TaK Code は以下のものです。# が黒マス、. が白マス、? が白黒どちらでもよいマスを表します。

###.?????
###.?????
###.?????
....?????
?????????
?????....
?????.###
?????.###
?????.###

入力で与えられたグリッドの上から 10 マス目左から 2 列目のマスを左上とする縦 9 マス 横 9 マスの領域は以下のようになっており、TaK Code の条件を満たします。

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

入力例 2

9 21
###.#...........#.###
###.#...........#.###
###.#...........#.###
....#...........#....
#########...#########
....#...........#....
....#.###...###.#....
....#.###...###.#....
....#.###...###.#....

出力例 2

1 1

入力例 3

18 18
######............
######............
######............
######............
######............
######............
..................
..................
..................
..................
..................
..................
............######
............######
............######
............######
............######
............######

出力例 3


TaK Code の条件を満たす箇所が 1 つもないこともあります。

Score : 200 points

Problem Statement

Takahashi invented Tak Code, a two-dimensional code. A TaK Code satisfies all of the following conditions:

  • It is a region consisting of nine horizontal rows and nine vertical columns.
  • All the 18 cells in the top-left and bottom-right three-by-three regions are black.
  • All the 14 cells that are adjacent (horizontally, vertically, or diagonally) to the top-left or bottom-right three-by-three region are white.

It is not allowed to rotate a TaK Code.

You are given a grid with N horizontal rows and M vertical columns. The state of the grid is described by N strings, S_1,\ldots, and S_N, each of length M. The cell at the i-th row from the top and j-th column from the left is black if the j-th character of S_i is #, and white if it is ..

Find all the nine-by-nine regions, completely contained in the grid, that satisfy the conditions of a TaK Code.

Constraints

  • 9 \leq N,M \leq 100
  • N and M are integers.
  • S_i is a string of length M consisting of . and #.

Input

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

N M
S_1
\vdots
S_N

Output

For all pairs (i,j) such that the nine-by-nine region, whose top-left cell is at the i-th row from the top and j-th columns from the left, satisfies the conditions of a TaK Code, print a line containing i, a space, and j in this order.
The pairs must be sorted in lexicographical ascending order; that is, i must be in ascending order, and within the same i, j must be in ascending order.


Sample Input 1

19 18
###......###......
###......###......
###..#...###..#...
..............#...
..................
..................
......###......###
......###......###
......###......###
.###..............
.###......##......
.###..............
............###...
...##.......###...
...##.......###...
.......###........
.......###........
.......###........
........#.........

Sample Output 1

1 1
1 10
7 7
10 2

A TaK Code looks like the following, where # is a black cell, . is a white cell, and ? can be either black or white.

###.?????
###.?????
###.?????
....?????
?????????
?????....
?????.###
?????.###
?????.###

In the grid given by the input, the nine-by-nine region, whose top-left cell is at the 10-th row from the top and 2-nd column from the left, satisfies the conditions of a TaK Code, as shown below.

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

Sample Input 2

9 21
###.#...........#.###
###.#...........#.###
###.#...........#.###
....#...........#....
#########...#########
....#...........#....
....#.###...###.#....
....#.###...###.#....
....#.###...###.#....

Sample Output 2

1 1

Sample Input 3

18 18
######............
######............
######............
######............
######............
######............
..................
..................
..................
..................
..................
..................
............######
............######
............######
............######
............######
............######

Sample Output 3


There may be no region that satisfies the conditions of TaK Code.

E - Knight Fork

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

配点 : 300

問題文

xy 座標平面上の 2 つの格子点 (x_1, y_1), (x_2, y_2) からの距離がともに \sqrt{5} である格子点は存在しますか?

注記

xy 座標平面上にある点のうち、x 座標と y 座標がともに整数である点を格子点と呼びます。
また、xy 平面上の 2(a, b), (c, d) の距離をユークリッド距離 \sqrt{(a - c)^2 + (b-d)^2} として定義します。

参考として、xy 平面の (0, 0) の上に黒丸を、(0, 0) からの距離が \sqrt{5} となる格子点の上に白丸を書いた図は以下のようになります。(x,y いずれかが整数となる部分に目盛り線を引いています。)

image

制約

  • -10^9 \leq x_1 \leq 10^9
  • -10^9 \leq y_1 \leq 10^9
  • -10^9 \leq x_2 \leq 10^9
  • -10^9 \leq y_2 \leq 10^9
  • (x_1, y_1) \neq (x_2, y_2)
  • 入力はすべて整数である。

入力

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

x_1 y_1 x_2 y_2

出力

条件を満たす格子点が存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

0 0 3 3

出力例 1

Yes
  • (2,1)(x_1, y_1) の距離は \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5}
  • (2,1)(x_2, y_2) の距離は \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5}
  • (2, 1) は格子点

なので点 (2, 1) は条件を満たします。よって Yes を出力します。
なお、点 (1, 2) も条件を満たすことが同様にして確認できます。


入力例 2

0 1 2 3

出力例 2

No

条件を満たす格子点は存在しません。よって No を出力します。


入力例 3

1000000000 1000000000 999999999 999999999

出力例 3

Yes

(10^9 + 1, 10^9 - 2) および点 (10^9 - 2, 10^9 + 1)が条件を満たします。

Score : 300 points

Problem Statement

On an xy-coordinate plane, is there a lattice point whose distances from two lattice points (x_1, y_1) and (x_2, y_2) are both \sqrt{5}?

Notes

A point on an xy-coordinate plane whose x and y coordinates are both integers is called a lattice point.
The distance between two points (a, b) and (c, d) is defined to be the Euclidean distance between them, \sqrt{(a - c)^2 + (b-d)^2}.

The following figure illustrates an xy-plane with a black circle at (0, 0) and white circles at the lattice points whose distances from (0, 0) are \sqrt{5}. (The grid shows where either x or y is an integer.)

image

Constraints

  • -10^9 \leq x_1 \leq 10^9
  • -10^9 \leq y_1 \leq 10^9
  • -10^9 \leq x_2 \leq 10^9
  • -10^9 \leq y_2 \leq 10^9
  • (x_1, y_1) \neq (x_2, y_2)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

If there is a lattice point satisfying the condition, print Yes; otherwise, print No.


Sample Input 1

0 0 3 3

Sample Output 1

Yes
  • The distance between points (2,1) and (x_1, y_1) is \sqrt{(0-2)^2 + (0-1)^2} = \sqrt{5};
  • the distance between points (2,1) and (x_2, y_2) is \sqrt{(3-2)^2 + (3-1)^2} = \sqrt{5};
  • point (2, 1) is a lattice point,

so point (2, 1) satisfies the condition. Thus, Yes should be printed.
One can also assert in the same way that (1, 2) also satisfies the condition.


Sample Input 2

0 1 2 3

Sample Output 2

No

No lattice point satisfies the condition, so No should be printed.


Sample Input 3

1000000000 1000000000 999999999 999999999

Sample Output 3

Yes

Point (10^9 + 1, 10^9 - 2) and point (10^9 - 2, 10^9 + 1) satisfy the condition.

F - Takahashi Gets Lost

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

配点 : 250

問題文

HW 列のグリッドがあります。

グリッドの各マスはのどちらかであり、 その情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で与えられます。 上から i 行目、左から j 列目のマスを (i, j) で表すと、 S_ij 文字目が . のときマス (i, j) は陸であり、# のときマス (i, j) は海です。

ここで、グリッドの外周のマス(すなわち、i = 1i = Hj = 1j = W のうち少なくとも 1 個以上を満たすマス (i, j) )については、すべて海であることが制約として保証されます。

高橋君が乗った宇宙船が、グリッド上のあるマスに不時着してしまいました。 その後、高橋君は LRUD のみからなる長さ N の文字列 T で表される手順に沿って、グリッド上を N 回移動しました。 i = 1, 2, \ldots, N について、Ti 文字目は i 回目の移動の内容を下記の通り表します。

  • L のとき、左に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j-1) である。
  • R のとき、右に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j+1) である。
  • U のとき、上に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i-1, j) である。
  • D のとき、下に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i+1, j) である。

高橋君の移動経路上のマス(不時着したマスおよび現在いるマスを含む)はいずれも海でないことがわかっています。 高橋君が現在いるマスとしてあり得るものの個数を出力してください。

制約

  • H, W, N は整数
  • 3 \leq H, W \leq 500
  • 1 \leq N \leq 500
  • TLRUD のみからなる長さ N の文字列
  • S_i.# のみからなる長さ W の文字列
  • 高橋君が現在いるマスとしてあり得るものが少なくとも 1 個存在する。
  • グリッドの外周のマスはすべて海である。

入力

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

H W N
T
S_1
S_2
\vdots
S_H

出力

答えを出力せよ。


入力例 1

6 7 5
LULDR
#######
#...#.#
##...##
#.#...#
#...#.#
#######

出力例 1

2

下記の 2 つの場合がありえるため、高橋君が現在いるマスとしてあり得るものは (3, 4)(4, 5)2 個です。

  • マス (3, 5) に不時着し、(3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4) と移動した場合
  • マス (4, 6) に不時着し、(4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5) と移動した場合

入力例 2

13 16 9
ULURDLURD
################
##..##.#..####.#
###.#..#.....#.#
#..##..#####.###
#...#..#......##
###.##.#..#....#
##.#####....##.#
###.###.#.#.#..#
######.....##..#
#...#.#.######.#
##..###..#..#.##
#...#.#.#...#..#
################

出力例 2

6

Score: 250 points

Problem Statement

There is a grid with H rows and W columns.

Each cell of the grid is land or sea, which is represented by H strings S_1, S_2, \ldots, S_H of length W. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left, and (i, j) is land if the j-th character of S_i is ., and (i, j) is sea if the character is #.

The constraints guarantee that all cells on the perimeter of the grid (that is, the cells (i, j) that satisfy at least one of i = 1, i = H, j = 1, j = W) are sea.

Takahashi's spaceship has crash-landed on a cell in the grid. Afterward, he moved N times on the grid following the instructions represented by a string T of length N consisting of L, R, U, and D. For i = 1, 2, \ldots, N, the i-th character of T describes the i-th move as follows:

  • L indicates a move of one cell to the left. That is, if he is at (i, j) before the move, he will be at (i, j-1) after the move.
  • R indicates a move of one cell to the right. That is, if he is at (i, j) before the move, he will be at (i, j+1) after the move.
  • U indicates a move of one cell up. That is, if he is at (i, j) before the move, he will be at (i-1, j) after the move.
  • D indicates a move of one cell down. That is, if he is at (i, j) before the move, he will be at (i+1, j) after the move.

It is known that all cells along his path (including the cell where he crash-landed and the cell he is currently on) are not sea. Print the number of cells that could be his current position.

Constraints

  • H, W, and N are integers.
  • 3 \leq H, W \leq 500
  • 1 \leq N \leq 500
  • T is a string of length N consisting of L, R, U, and D.
  • S_i is a string of length W consisting of . and #.
  • There is at least one cell that could be Takahashi's current position.
  • All cells on the perimeter of the grid are sea.

Input

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

H W N
T
S_1
S_2
\vdots
S_H

Output

Print the answer.


Sample Input 1

6 7 5
LULDR
#######
#...#.#
##...##
#.#...#
#...#.#
#######

Sample Output 1

2

The following two cases are possible, so there are two cells that could be Takahashi's current position: (3, 4) and (4, 5).

  • He crash-landed on cell (3, 5) and moved (3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4).
  • He crash-landed on cell (4, 6) and moved (4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5).

Sample Input 2

13 16 9
ULURDLURD
################
##..##.#..####.#
###.#..#.....#.#
#..##..#####.###
#...#..#......##
###.##.#..#....#
##.#####....##.#
###.###.#.#.#..#
######.....##..#
#...#.#.######.#
##..###..#..#.##
#...#.#.#...#..#
################

Sample Output 2

6
G - Ghost Ants

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

配点 : 350

問題文

数直線上に 1 から N の番号がつけられた N 匹の蟻がいます。 蟻 i (1 \leq i \leq N) ははじめ座標 X_i にいて、正負どちらかの方向を向いています。はじめに全ての蟻は相異なる座標にいます。各蟻が向いている方向は長さ N01 文字列 S で表され、S_i0 のとき蟻 i は負の方向を向いており、 1 のとき蟻 i は正の方向を向いています。

現在を時刻 0 とし、時刻 (T+0.1) までの (T+0.1) 単位時間にわたって、N 匹の蟻がそれぞれの向いている方向に向かって単位時間あたり 1 の速さで移動します。 複数の蟻が同じ座標に到達すると、それらの蟻はすれ違い、方向や速度を変えずに通り過ぎます。 (T+0.1) 単位時間が経過したとき、すべての蟻は停止します。

1 \leq i < j \leq N を満たし、今から時刻 (T+0.1) までに蟻 i と蟻 j がすれ違う整数の組 (i,j) の個数を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^{5}
  • 1 \leq T \leq 10^{9}
  • S01 からなる長さ N の文字列
  • -10^{9} \leq X_i \leq 10^{9} (1 \leq i \leq N)
  • X_i \neq X_j (1 \leq i < j \leq N)
  • N,T,X_i (1 \leq i \leq N) は整数

入力

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

N T
S
X_1 X_2 ... X_N

出力

答えを出力せよ。


入力例 1

6 3
101010
-5 -1 0 1 2 4

出力例 1

5

以下の 5 つの蟻の組み合わせがすれ違います。

  • 3 と蟻 4 が時刻 0.5 にすれ違う。
  • 5 と蟻 6 が時刻 1 にすれ違う。
  • 1 と蟻 2 が時刻 2 にすれ違う。
  • 3 と蟻 6 が時刻 2 にすれ違う。
  • 1 と蟻 4 が時刻 3 にすれ違う。

これ以外の蟻の組み合わせはすれ違うことはないため、5 を出力します。


入力例 2

13 656320850
0100110011101
-900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366

出力例 2

14

Score : 350 points

Problem Statement

There are N ants on a number line, labeled 1 to N. Ant i (1 \leq i \leq N) starts at coordinate X_i and faces either a positive or negative direction. Initially, all ants are at distinct coordinates. The direction each ant is facing is represented by a binary string S of length N, where ant i is facing the negative direction if S_i is 0 and the positive direction if S_i is 1.

Let the current time be 0, and the ants move in their respective directions at a speed of 1 unit per unit time for (T+0.1) units of time until time (T+0.1). If multiple ants reach the same coordinate, they pass through each other without changing direction or speed. After (T+0.1) units of time, all ants stop.

Find the number of pairs (i, j) such that 1 \leq i < j \leq N and ants i and j pass each other from now before time (T+0.1).

Constraints

  • 2 \leq N \leq 2 \times 10^{5}
  • 1 \leq T \leq 10^{9}
  • S is a string of length N consisting of 0 and 1.
  • -10^{9} \leq X_i \leq 10^{9} (1 \leq i \leq N)
  • X_i \neq X_j (1 \leq i < j \leq N)
  • N, T, and X_i (1 \leq i \leq N) are integers.

Input

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

N T
S
X_1 X_2 ... X_N

Output

Print the answer.


Sample Input 1

6 3
101010
-5 -1 0 1 2 4

Sample Output 1

5

The following five pairs of ants pass each other:

  • Ant 3 and ant 4 pass each other at time 0.5.
  • Ant 5 and ant 6 pass each other at time 1.
  • Ant 1 and ant 2 pass each other at time 2.
  • Ant 3 and ant 6 pass each other at time 2.
  • Ant 1 and ant 4 pass each other at time 3.

No other pairs of ants pass each other, so print 5.


Sample Input 2

13 656320850
0100110011101
-900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366

Sample Output 2

14