A - ABC400 Party

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

配点 : 100 点

問題文

ABC400 を記念した式典において、400 人の高橋君を A 行 B 列の長方形状に隙間なく並べようとしています。

正整数 A が与えられるので、このような並べ方ができるような正整数 B の値を出力してください。ただし、そのような正整数 B が存在しない場合には -1 を出力してください。

制約

  • A は 1 以上 400 以下の整数

入力

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

A

出力

問題文の指示に従って B の値あるいは -1 を出力せよ。


入力例 1

10

出力例 1

40

400 人の高橋君を 10 行 40 列の長方形状に並べることができます。


入力例 2

11

出力例 2

-1

入力例 3

400

出力例 3

1

Score : 100 points

Problem Statement

In the ceremony commemorating ABC400, we want to arrange 400 people in a rectangular formation of A rows and B columns without any gaps.

You are given a positive integer A. Print the value of a positive integer B for which such an arrangement is possible. If there is no such positive integer B, print -1.

Constraints

  • A is an integer between 1 and 400, inclusive.

Input

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

A

Output

Print the value of B or -1 as specified by the problem statement.


Sample Input 1

10

Sample Output 1

40

We can arrange 400 people in 10 rows and 40 columns.


Sample Input 2

11

Sample Output 2

-1

Sample Input 3

400

Sample Output 3

1
B - Arithmetic Progression

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

配点 : 100 点

問題文

初項が A、末項が B、公差が D であるような等差数列を出力してください。

なお、そのような等差数列が存在する入力のみが与えられます。

制約

  • 1 \leq A \leq B \leq 100
  • 1\leq D \leq 100
  • 初項が A、末項が B、公差が D であるような等差数列が存在する
  • 入力は全て整数

入力

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

A B D

出力

初項が A、末項が B、公差が D であるような等差数列の項を順に空白区切りで出力せよ。


入力例 1

3 9 2

出力例 1

3 5 7 9

初項が 3、末項が 9、公差が 2 であるような等差数列は (3,5,7,9) です。


入力例 2

10 10 1

出力例 2

10

初項が 10、末項が 10、公差が 1 であるような等差数列は (10) です。

Score: 100 points

Problem Statement

Print an arithmetic sequence with first term A, last term B, and common difference D.

You are only given inputs for which such an arithmetic sequence exists.

Constraints

  • 1 \leq A \leq B \leq 100
  • 1 \leq D \leq 100
  • There is an arithmetic sequence with first term A, last term B, and common difference D.
  • All input values are integers.

Input

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

A B D

Output

Print the terms of the arithmetic sequence with first term A, last term B, and common difference D, in order, separated by spaces.


Sample Input 1

3 9 2

Sample Output 1

3 5 7 9

The arithmetic sequence with first term 3, last term 9, and common difference 2 is (3,5,7,9).


Sample Input 2

10 10 1

Sample Output 2

10

The arithmetic sequence with first term 10, last term 10, and common difference 1 is (10).

C - Postal Card

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

配点 : 200 点

問題文

数字のみからなる長さ 6 の文字列が N 個与えられます。i \, (i = 1, 2, \dots, N) 番目のものを S_i と表します。

さらに、数字のみからなる長さ 3 の文字列が M 個与えられます。j \, (j = 1, 2, \dots, M) 番目のものを T_j と表します。

S_1, S_2, \dots, S_N のうち、末尾 3 文字が T_1, T_2, \dots, T_M のいずれかに一致するものの個数を求めてください。

制約

  • 1 \leq N, M \leq 1000
  • N, M は整数
  • 全ての i = 1, 2, \dots, N に対し、S_i は数字のみからなる長さ 6 の文字列
  • 全ての j = 1, 2, \dots, M に対し、T_j は数字のみからなる長さ 3 の文字列

入力

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

N M
S_1
S_2
\vdots
S_N
T_1
T_2
\vdots
T_M

出力

答えを出力せよ。


入力例 1

3 3
142857
004159
071028
159
287
857

出力例 1

2

S_1 の末尾 3 文字は 857 であり、これは T_3 に一致します。
S_2 の末尾 3 文字は 159 であり、これは T_1 に一致します。
S_3 の末尾 3 文字は 028 であり、これは T_1, T_2, T_3 のいずれにも一致しません。

以上から、答えは 2 です。


入力例 2

5 4
235983
109467
823476
592801
000333
333
108
467
983

出力例 2

3

入力例 3

4 4
000000
123456
987111
000000
000
111
999
111

出力例 3

3

Score : 200 points

Problem Statement

You are given N strings of length six each, consisting of digits. Let S_i be the i-th (i = 1, 2, \dots, N) of them.

You are also given M strings of length three each, consisting of digits. Let T_j be the j-th (j = 1, 2, \dots, M) of them.

Find the number of strings among S_1, S_2, \dots, S_N whose last three characters coincide with one or more of T_1, T_2, \dots, T_M.

Constraints

  • 1 \leq N, M \leq 1000
  • N and M are integers.
  • S_i is a string of length 6 consisting of digits, for all i = 1, 2, \dots, N.
  • T_j is a string of length 3 consisting of digits, for all j = 1, 2, \dots, M.

Input

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

N M
S_1
S_2
\vdots
S_N
T_1
T_2
\vdots
T_M

Output

Print the answer.


Sample Input 1

3 3
142857
004159
071028
159
287
857

Sample Output 1

2

The last three characters of S_1 are 857, which coincide with T_3.
The last three characters of S_2 are 159, which coincide with T_1.
The last three characters of S_3 are 028, which do not coincide with T_1, T_2, or T_3.

Thus, the answer is 2.


Sample Input 2

5 4
235983
109467
823476
592801
000333
333
108
467
983

Sample Output 2

3

Sample Input 3

4 4
000000
123456
987111
000000
000
111
999
111

Sample Output 3

3
D - Looped Rope

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

配点 : 200 点

問題文

縦 H マス、横 W マスのマス目があります。 上から i 行目 (1\le i\le H) 、左から j 列目 (1\le j\le W) のマスをマス (i,j) と呼ぶことにします。

それぞれのマスは白もしくは黒のどちらか 1 色で塗られています。 マスに塗られている色は H 個の文字列 S _ 1,S _ 2,\ldots,S _ H で表され、S _ i\ (1\le i\le H) の j 文字目 (1\le j\le W) が . のとき、マス (i,j) は白で塗られており、S _ i\ (1\le i\le H) の j 文字目 (1\le j\le W) が # のとき、マス (i,j) は黒で塗られています。

マス目が以下の条件を満たすか判定してください。

  • どの黒で塗られたマスについても、上下左右で隣り合うマスのうち黒く塗られているものは 2 つもしくは 4 つである。

ただし、マス (i,j)\ (1\le i\le H,1\le j\le W) とマス (k,l)\ (1\le k\le H,1\le l\le W) は、|i-k|+|j-l|=1 であるとき、かつそのときに限り上下左右で隣り合っているとします。

制約

  • 1\le H\le 20
  • 1\le W\le 20
  • H,W は整数
  • S _ i は . および # からなる長さ W の文字列 (1\le i\le H)

入力

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

H W
S _ 1
S _ 2
\vdots
S _ H

出力

与えられたマス目が条件を満たしているとき Yes を、条件を満たしていないとき No を出力せよ。


入力例 1

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

出力例 1

Yes

例えば、マス (6,3) は黒で塗られており、隣り合っているマス (5,3),(6,2),(6,4),(7,3) のうち、マス (5,3) およびマス (6,4) の 2 マスが黒で塗られているため、条件を満たしています。

他の黒で塗られたどのマスについても条件を満たしているため、Yes を出力してください。


入力例 2

1 2
##

出力例 2

No

マス (1,1) は黒で塗られていますが、隣り合っているマスは 1 つしかないため、条件を満たしていません。

よって、No を出力して下さい。


入力例 3

4 3
...
...
...
...

出力例 3

Yes

黒で塗られているマスがないため、条件を満たしています。

よって、Yes を出力してください。


入力例 4

15 18
##.###..##.##..##.
##.#.##.##.##.####
...##.#.......####
###.###....###.##.
#.##.......#.#....
#..#.##.##.#.#....
#.########.####.##
#.##.##.#....##.##
#......##.........
##.##..#..##..####
.#.#####..#####..#
.#..#...##.#.....#
.#..#.####.#.....#
.##.#.#.#..##..###
..###.###...####..

出力例 4

Yes

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row (1\le i\le H) from the top and the j-th column (1\le j\le W) from the left.

Each cell is painted with one color, white or black. The color painted on the cells is represented by H strings S _ 1,S _ 2,\ldots,S _ H. When the j-th character (1\le j\le W) of S _ i\ (1\le i\le H) is ., cell (i,j) is painted white, and when the j-th character (1\le j\le W) of S _ i\ (1\le i\le H) is #, cell (i,j) is painted black.

Determine whether the grid satisfies the following condition:

  • For every black cell, the number of horizontally or vertically adjacent cells that are painted black is 2 or 4.

Here, cells (i,j)\ (1\le i\le H,1\le j\le W) and (k,l)\ (1\le k\le H,1\le l\le W) are horizontally or vertically adjacent if and only if |i-k|+|j-l|=1.

Constraints

  • 1\le H\le 20
  • 1\le W\le 20
  • H and W are integers.
  • S _ i is a string of length W consisting of . and # (1\le i\le H).

Input

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

H W
S _ 1
S _ 2
\vdots
S _ H

Output

Output Yes if the given grid satisfies the condition, and No if it does not satisfy the condition.


Sample Input 1

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

Sample Output 1

Yes

For example, cell (6,3) is painted black, and among the adjacent cells (5,3),(6,2),(6,4),(7,3), cells (5,3) and (6,4) are painted black, which is 2 cells, so it satisfies the condition.

Every other black cell also satisfies the condition, so output Yes.


Sample Input 2

1 2
##

Sample Output 2

No

Cell (1,1) is painted black, but it has only one adjacent cell, so it does not satisfy the condition.

Therefore, output No.


Sample Input 3

4 3
...
...
...
...

Sample Output 3

Yes

There are no black cells, so the condition is satisfied.

Therefore, output Yes.


Sample Input 4

15 18
##.###..##.##..##.
##.#.##.##.##.####
...##.#.......####
###.###....###.##.
#.##.......#.#....
#..#.##.##.#.#....
#.########.####.##
#.##.##.#....##.##
#......##.........
##.##..#..##..####
.#.#####..#####..#
.#..#...##.#.....#
.#..#.####.#.....#
.##.#.#.#..##..###
..###.###...####..

Sample Output 4

Yes
E - Count Close Pairs

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

配点 : 300 点

問題文

この問題は インタラクティブな問題(あなたが作成したプログラムとジャッジプログラムが標準入出力を介して対話を行う形式の問題)です。

数直線上に、点 1, 2, \ldots, N が この順に 左から右に並んでいます。

最初、あなたには整数 N のみが与えられます。

その後、あなたはジャッジに以下の質問を \bm{2N} 回まで行うことができます。

  • 1 \leq i < j \leq N を満たす整数 i,j を選び、点 i と点 j の距離が 1 以下であるか質問する。

距離が 1 以下の 2 点組の個数、すなわち 1\leq i<j\leq N をみたす整数の組 (i,j) であって、点 i と点 j の距離が 1 以下であるようなものの個数を出力してください。

制約

  • 2 \leq N \leq 10^3
  • N は整数

入出力

最初に、点の個数を表す整数 N を標準入力から受け取ってください。

N

次に、あなたはジャッジに対して問題文中の質問を 2N 回まで繰り返すことができます。

質問は、以下の形式で標準出力に出力してください。 ここで、i,j は 1 \leq i<j \leq N を満たす整数である必要があります。

? i j

これに対する応答として、以下のどちらかが標準入力から与えられます。

Yes
No

ここで、Yes ならば点 i と点 j の距離が 1 以下であることを、No ならば点 i と点 j の距離が 1 より大きいことを表します。

問題の答え X が求まったら、解答を以下の形式で出力してください。 その後、ただちにプログラムを終了してください。

! X

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
  • 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
  • 点の配置および答えはあなたとジャッジの対話の開始時に固定され、あなたが行った質問などに応じて変更されることはありません。

入出力例

以下は、N = 3、点 1,2,3 の座標がそれぞれ 0,0.7,1.5 の場合の入出力例です。(点 1,2,3 の座標は入力として与えられません。)

入力 出力 説明
3 N が与えられます。
? 1 2 点 1,2 の間の距離が 1 以下かをジャッジに質問します。
Yes ジャッジから、点 1,2 の間の距離が 1 以下であるという返答を得ます。
? 1 3 点 1,3 の間の距離が 1 以下かをジャッジに質問します。
No ジャッジから、点 1,3 の間の距離が 1 以下でないという返答を得ます。
? 2 3 点 2,3 の間の距離が 1 以下かをジャッジに質問します。
Yes ジャッジから、点 2,3 の間の距離が 1 以下であるという返答を得ます。
! 2 条件をみたす組の数として 2 を解答します。

質問回数は 3 回であり、特に 2N=6 回以下です。
解答した組の数も正しいため、この後ただちにプログラムを終了することで、正解と判定されます。

Score : 300 points

Problem Statement

This is an interactive problem (in which your program and the judge program communicate via Standard Input and Output).

On a number line, points 1, 2, \ldots, N are arranged from left to right in that order.

Initially, you are given only the integer N.

Then, you can ask the judge the following question at most \bm{2N} times.

  • Choose integers i and j satisfying 1 \leq i < j \leq N, and ask whether the distance between points i and j is at most 1.

Output the number of pairs of points whose distance is at most 1, that is, the number of pairs of integers (i,j) satisfying 1 \leq i < j \leq N such that the distance between points i and j is at most 1.

Constraints

  • 2 \leq N \leq 10^3
  • N is an integer.

Interaction

First, receive the integer N representing the number of points from Standard Input:

N

Then, you can repeat the question described in the problem statement to the judge at most 2N times.

Output the question to Standard Output in the following format, where i and j must be integers satisfying 1 \leq i<j \leq N:

? i j

As a response to this, one of the following will be given from Standard Input:

Yes
No

Here, Yes indicates that the distance between points i and j is at most 1, and No indicates that the distance between points i and j is greater than 1.

Once you have found the answer X to the problem, output your answer in the following format, and then terminate the program immediately:

! X

Notes

  • Each time you output something, insert a newline at the end and flush Standard Output. Otherwise, the judge result may be TLE.
  • The judge result is indeterminate if you output something invalid during the interaction, or if the program terminates prematurely.
  • Terminate the program immediately after outputting your answer. Otherwise, the judge result is indeterminate.
  • The arrangement of the points and the answer are fixed at the start of the interaction between you and the judge, and do not change depending on, for example, the questions you make.

Sample Interaction

Below is an example of interaction where N = 3 and the coordinates of points 1,2,3 are 0, 0.7, 1.5, respectively. (The coordinates of points 1,2,3 are not given as input.)

Input Output Explanation
3 N is given.
? 1 2 Ask the judge whether the distance between points 1 and 2 is at most 1.
Yes Obtain the response from the judge that the distance between points 1 and 2 is at most 1.
? 1 3 Ask the judge whether the distance between points 1 and 3 is at most 1.
No Obtain the response from the judge that the distance between points 1 and 3 is not at most 1.
? 2 3 Ask the judge whether the distance between points 2 and 3 is at most 1.
Yes Obtain the response from the judge that the distance between points 2 and 3 is at most 1.
! 2 Answer 2 as the number of pairs satisfying the condition.

The number of questions is 3, which is in particular at most 2N=6.
Since the answered number of pairs is also correct, the program will be judged as correct if it terminates immediately after this.