A - AtCoder Quiz 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

AtCoder で定期的に開催されている、国際的な権威があるコンテストである AtCoder Grand Contest (以下、AGC) は今までに 54 回開催されてきました。

みなさんがちょうど参加している 230 回目の ABC である ABC230 と同様に、 当初は N 回目の AGC のコンテスト名には N3 桁になるようにゼロ埋めした数が割り振られていました。( 1 回目の AGC は AGC001, 2 回目の AGC は AGC002, ...)

ところが、最新の 54 回目の AGC のコンテスト名は AGC055 で、回数より 1 大きい数が割り振られています。これは、AGC042 が社会情勢の影響で中止されて欠番となったため、42 回目以降に開催されたコンテストでは開催された回数より 1 大きい数が割り振られているからです。(入出力例にある説明も参考にしてください。)

さて、ここで問題です。整数 N が与えられるので、N 回目に開催された AGC のコンテスト名を AGCXXX の形式で出力してください。ここで、XXX にはゼロ埋めがなされた 3 桁の数が入ります。

制約

  • 1 \leq N \leq 54
  • N は整数である。

入力

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

N

出力

N 回目に開催された AGC のコンテスト名を所定の形式で出力せよ。


入力例 1

42

出力例 1

AGC043

問題文にある通り、 42 回目以降の AGC には回数より 1 大きい数が割り振られています。
よって 42 回目の AGC のコンテスト名は AGC043 になります。


入力例 2

19

出力例 2

AGC019

41 回目以前の AGC は回数と同じ数が割り振られています。
よって AGC019 が答えとなります。


入力例 3

1

出力例 3

AGC001

問題文にある通り、 1 回目の AGC のコンテスト名は AGC001 です。
数が 3 桁になるようにゼロ埋めを行う必要があるのに注意してください。


入力例 4

50

出力例 4

AGC051

Score : 100 points

Problem Statement

AtCoder Grand Contest (AGC), a regularly held contest with a world authority, has been held 54 times.

Just like the 230-th ABC ― the one you are in now ― is called ABC230, the N-th AGC is initially named with a zero-padded 3-digit number N. (The 1-st AGC is AGC001, the 2-nd AGC is AGC002, ...)

However, the latest 54-th AGC is called AGC055, where the number is one greater than 54. Because AGC042 is canceled and missing due to the social situation, the 42-th and subsequent contests are assigned numbers that are one greater than the numbers of contests held. (See also the explanations at Sample Inputs and Outputs.)

Here is the problem: given an integer N, print the name of the N-th AGC in the format AGCXXX, where XXX is the zero-padded 3-digit number.

Constraints

  • 1 \leq N \leq 54
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the name of the N-th AGC in the specified format.


Sample Input 1

42

Sample Output 1

AGC043

As explained in Problem Statement, the 42-th and subsequent AGCs are assigned numbers that are one greater than the numbers of contests.
Thus, the 42-th AGC is named AGC043.


Sample Input 2

19

Sample Output 2

AGC019

The 41-th and preceding AGCs are assigned numbers that are equal to the numbers of contests.
Thus, the answer is AGC019.


Sample Input 3

1

Sample Output 3

AGC001

As mentioned in Problem Statement, the 1-st AGC is named AGC001.
Be sure to pad the number with zeros into a 3-digit number.


Sample Input 4

50

Sample Output 4

AGC051
B - World Cup

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

あるスポーツ大会は西暦年を 4 で割った余りが 2 である年の 6 月に開催されます。
現在が西暦 Y 年の 1 月である時、このスポーツ大会が次に開催されるのは西暦何年になるかを求めてください。

制約

  • 2000 \leq Y \leq 3000
  • Y は整数

入力

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

Y

出力

答えを出力せよ。


入力例 1

2022

出力例 1

2022

20224 で割った余りが 2 なので、現在が西暦 2022 年の 1 月である時、次の開催は同年の 6 月です。


入力例 2

2023

出力例 2

2026

入力例 3

3000

出力例 3

3002

Score : 100 points

Problem Statement

A sport event is held in June of every year whose remainder when divided by 4 is 2.
Suppose that it is now January of the year Y. In what year will this sport event be held next time?

Constraints

  • 2000 \leq Y \leq 3000
  • Y is an integer.

Input

Input is given from Standard Input in the following format:

Y

Output

Print the answer.


Sample Input 1

2022

Sample Output 1

2022

The remainder when 2022 is divided by 4 is 2, so if it is now January of 2022, the next games will be held in June of the same year.


Sample Input 2

2023

Sample Output 2

2026

Sample Input 3

3000

Sample Output 3

3002
C - Right Triangle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

xy 平面上に、同一直線上にない 3A(x_A,y_A), B(x_B,y_B),C(x_C,y_C) があります。三角形 ABC が直角三角形であるかどうか判定してください。

制約

  • -1000\leq x_A,y_A,x_B,y_B,x_C,y_C\leq 1000
  • 3A,B,C は同一直線上にない
  • 入力は全て整数

入力

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

x_A y_A
x_B y_B
x_C y_C

出力

三角形 ABC が直角三角形であるならば Yes を、そうでないならば No を出力せよ。


入力例 1

0 0
4 0
0 3

出力例 1

Yes

三角形 ABC は直角三角形です。


入力例 2

-4 3
2 1
3 4

出力例 2

Yes

三角形 ABC は直角三角形です。


入力例 3

2 4
-3 2
1 -2

出力例 3

No

三角形 ABC は直角三角形ではありません。

Score : 200 points

Problem Statement

In the xy-plane, there are three points A(x_A, y_A), B(x_B, y_B), and C(x_C, y_C) that are not collinear. Determine whether the triangle ABC is a right triangle.

Constraints

  • -1000 \leq x_A, y_A, x_B, y_B, x_C, y_C \leq 1000
  • The three points A, B, and C are not collinear.
  • All input values are integers.

Input

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

x_A y_A
x_B y_B
x_C y_C

Output

Print Yes if the triangle ABC is a right triangle, and No otherwise.


Sample Input 1

0 0
4 0
0 3

Sample Output 1

Yes

The triangle ABC is a right triangle.


Sample Input 2

-4 3
2 1
3 4

Sample Output 2

Yes

The triangle ABC is a right triangle.


Sample Input 3

2 4
-3 2
1 -2

Sample Output 3

No

The triangle ABC is not a right triangle.

D - Line Sensor

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
各マスの状態は文字 C_{i,j} で表されます。C_{i,j}. ならば (i, j) には何も置かれておらず、 # ならば箱が 1 個置かれています。

1 \leq j \leq W を満たす整数 j に対して、整数 X_j を次のように定義します。

  • j 列目に置かれている箱の個数。言い換えると、C_{i,j}# であるような整数 i の個数。

X_1, X_2, \dots, X_W をすべて求めてください。

制約

  • 1 \leq H \leq 1000
  • 1 \leq W \leq 1000
  • H, W は整数
  • C_{i, j}. または #

入力

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

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}

出力

X_1, X_2, \dots, X_W を以下の形式に従って出力せよ。

X_1 X_2 \dots X_W

入力例 1

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

出力例 1

1 2 0 3

1 列目の箱が置かれているマスは (1, 1)1 ヵ所です。よって X_1 = 1 です。
2 列目の箱が置かれているマスは (2, 2), (3, 2)2 ヵ所です。よって X_2 = 2 です。
3 列目の箱が置かれているマスは存在しません。よって X_3 = 0 です。
4 列目の箱が置かれているマスは (1, 4), (2, 4), (3, 4)3 ヵ所です。よって X_4 = 3 です。
よって (X_1, X_2, X_3, X_4) = (1, 2, 0, 3) が答えとなります。


入力例 2

3 7
.......
.......
.......

出力例 2

0 0 0 0 0 0 0

箱が置かれているマスが存在しない場合もあります。


入力例 3

8 3
.#.
###
.#.
.#.
.##
..#
##.
.##

出力例 3

2 7 4

入力例 4

5 47
.#..#..#####..#...#..#####..#...#...###...#####
.#.#...#.......#.#...#......##..#..#...#..#....
.##....#####....#....#####..#.#.#..#......#####
.#.#...#........#....#......#..##..#...#..#....
.#..#..#####....#....#####..#...#...###...#####

出力例 4

0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3

Score : 200 points

Problem Statement

There is a grid with H rows from top to bottom and W columns from left to right. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
The squares are described by characters C_{i,j}. If C_{i,j} is ., (i, j) is empty; if it is #, (i, j) contains a box.

For integers j satisfying 1 \leq j \leq W, let the integer X_j defined as follows.

  • X_j is the number of boxes in the j-th column. In other words, X_j is the number of integers i such that C_{i,j} is #.

Find all of X_1, X_2, \dots, X_W.

Constraints

  • 1 \leq H \leq 1000
  • 1 \leq W \leq 1000
  • H and W are integers.
  • C_{i, j} is . or #.

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 X_1, X_2, \dots, X_W in the following format:

X_1 X_2 \dots X_W

Sample Input 1

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

Sample Output 1

1 2 0 3

In the 1-st column, one square, (1, 1), contains a box. Thus, X_1 = 1.
In the 2-nd column, two squares, (2, 2) and (3, 2), contain a box. Thus, X_2 = 2.
In the 3-rd column, no squares contain a box. Thus, X_3 = 0.
In the 4-th column, three squares, (1, 4), (2, 4), and (3, 4), contain a box. Thus, X_4 = 3.
Therefore, the answer is (X_1, X_2, X_3, X_4) = (1, 2, 0, 3).


Sample Input 2

3 7
.......
.......
.......

Sample Output 2

0 0 0 0 0 0 0

There may be no square that contains a box.


Sample Input 3

8 3
.#.
###
.#.
.#.
.##
..#
##.
.##

Sample Output 3

2 7 4

Sample Input 4

5 47
.#..#..#####..#...#..#####..#...#...###...#####
.#.#...#.......#.#...#......##..#..#...#..#....
.##....#####....#....#####..#.#.#..#......#####
.#.#...#........#....#......#..##..#...#..#....
.#..#..#####....#....#####..#...#...###...#####

Sample Output 4

0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3
E - Count ABC Again

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

長さ N の文字列 S が与えられます。クエリが Q 個与えられるので、順番に処理してください。

i 番目のクエリは以下の通りです。

  • 整数 X_i と文字 C_i が与えられるので、 SX_i 番目の文字を C_i に置き換える。その後、 S に文字列 ABC が部分文字列として何箇所含まれるかを出力する。

ここで、 S部分文字列 とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ababc の部分文字列ですが、acabc の部分文字列ではありません。

制約

  • 3\le N\le 2\times 10^5
  • 1\le Q\le 2\times 10^5
  • S は英大文字からなる長さ N の文字列
  • 1\le X_i\le N
  • C_i は英大文字

入力

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

N Q
S
X_1 C_1
X_2 C_2
\vdots
X_Q C_Q

出力

Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。


入力例 1

7 4
ABCDABC
4 B
3 A
5 C
4 G

出力例 1

2
1
1
0

各クエリを処理した後の S は次のようになります。

  • 1 つ目のクエリを処理後: S= ABCBABC となる。この中に ABC は部分文字列として 2 回登場する。
  • 2 つ目のクエリを処理後: S= ABABABC となる。この中に ABC は部分文字列として 1 回登場する。
  • 3 つ目のクエリを処理後: S= ABABCBC となる。この中に ABC は部分文字列として 1 回登場する。
  • 4 つ目のクエリを処理後: S= ABAGCBC となる。この中に ABC は部分文字列として 0 回登場する。

入力例 2

3 3
ABC
1 A
2 B
3 C

出力例 2

1
1
1

クエリの処理前と処理後で S が変わらない場合もあります。


入力例 3

15 10
BBCCBCACCBACACA
9 C
11 B
5 B
11 B
4 A
8 C
8 B
5 B
7 B
14 B

出力例 3

0
0
0
0
1
1
2
2
1
1

Score : 350 points

Problem Statement

You are given a string S of length N. You are also given Q queries, which you should process in order.

The i-th query is as follows:

  • Given an integer X_i and a character C_i, replace the X_i-th character of S with C_i. Then, print the number of times the string ABC appears as a substring in S.

Here, a substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

  • 3 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • S is a string of length N consisting of uppercase English letters.
  • 1 \le X_i \le N
  • C_i is an uppercase English letter.

Input

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

N Q
S
X_1 C_1
X_2 C_2
\vdots
X_Q C_Q

Output

Print Q lines. The i-th line (1 \le i \le Q) should contain the answer to the i-th query.


Sample Input 1

7 4
ABCDABC
4 B
3 A
5 C
4 G

Sample Output 1

2
1
1
0

After processing each query, S becomes as follows.

  • After the first query: S= ABCBABC. In this string, ABC appears twice as a substring.
  • After the second query: S= ABABABC. In this string, ABC appears once as a substring.
  • After the third query: S= ABABCBC. In this string, ABC appears once as a substring.
  • After the fourth query: S= ABAGCBC. In this string, ABC appears zero times as a substring.

Sample Input 2

3 3
ABC
1 A
2 B
3 C

Sample Output 2

1
1
1

There are cases where S does not change through processing a query.


Sample Input 3

15 10
BBCCBCACCBACACA
9 C
11 B
5 B
11 B
4 A
8 C
8 B
5 B
7 B
14 B

Sample Output 3

0
0
0
0
1
1
2
2
1
1
F - Large Queue

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

空の整数列 A=() があります。クエリが Q 個与えられるので、与えられた順に処理してください。クエリは以下の 2 種類です。

  • タイプ 1 : 1 c x の形式で与えられる。A の末尾に xc 個追加する。
  • タイプ 2 : 2 k の形式で与えられる。A の先頭 k 要素を削除し、削除した k 個の整数の総和を出力する。このとき、k はその時点での A の長さ以下であることが保証される。

制約

  • 1 \leq Q \leq 2 \times 10^{5}
  • タイプ 1 のクエリにおいて、 1 \leq c \leq 10^{9}
  • タイプ 1 のクエリにおいて、 1 \leq x \leq 10^{9}
  • タイプ 2 のクエリにおいて、その時点での A の長さを n として、 1 \leq k \leq \min(10^{9},n)
  • 入力はすべて整数

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ただし、 \text{query}_ii 個目のクエリを表し、以下のいずれかの形式である。

1 c x
2 k

出力

タイプ 2 のクエリの個数を q として、q 行出力せよ。i 行目には、i 個目のタイプ 2 のクエリに対する答えを出力せよ。


入力例 1

5
1 2 3
1 4 5
2 3
1 6 2
2 5

出力例 1

11
19
  • 1 個目のクエリ: A の末尾に 32 個追加する。このとき、A=(3,3) となる。
  • 2 個目のクエリ: A の末尾に 54 個追加する。このとき、A=(3,3,5,5,5,5) となる。
  • 3 個目のクエリ: A の先頭 3 要素を削除する。このとき、削除した 3 個の整数の総和は 3+3+5=11 となるため 11 と出力する。削除後、A=(5,5,5) となる。
  • 4 個目のクエリ: A の末尾に 26 個追加する。このとき、A=(5,5,5,2,2,2,2,2,2) となる。
  • 5 個目のクエリ: A の先頭 5 要素を削除する。このとき、削除した 5 個の整数の総和は 5+5+5+2+2=19 となるため 19 と出力する。削除後、A=(2,2,2,2) となる。

入力例 2

10
1 75 22
1 81 72
1 2 97
1 84 82
1 2 32
1 39 57
2 45
1 40 16
2 32
2 42

出力例 2

990
804
3024

入力例 3

10
1 160449218 954291757
2 17217760
1 353195922 501899080
1 350034067 910748511
1 824284691 470338674
2 180999835
1 131381221 677959980
1 346948152 208032501
1 893229302 506147731
2 298309896

出力例 3

16430766442004320
155640513381884866
149721462357295680

Score : 300 points

Problem Statement

There is an empty integer sequence A=(). You are given Q queries, and you need to process them in the given order. There are two types of queries:

  • Type 1: Given in the format 1 c x. Add c copies of x to the end of A.
  • Type 2: Given in the format 2 k. Remove the first k elements from A and output the sum of the removed k integers. It is guaranteed that k is at most the length of A at that time.

Constraints

  • 1 \leq Q \leq 2 \times 10^{5}
  • In type 1 queries, 1 \leq c \leq 10^{9}.
  • In type 1 queries, 1 \leq x \leq 10^{9}.
  • In type 2 queries, letting n be the length of A at that time, 1 \leq k \leq \min(10^{9},n).
  • All input values are integers.

Input

The input is given from standard input in the following format:

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

where \text{query}_i represents the i-th query and is in one of the following formats:

1 c x
2 k

Output

Let q be the number of type 2 queries. Output q lines. The i-th line should contain the answer to the i-th type 2 query.


Sample Input 1

5
1 2 3
1 4 5
2 3
1 6 2
2 5

Sample Output 1

11
19
  • 1st query: Add 2 copies of 3 to the end of A. Then, A=(3,3).
  • 2nd query: Add 4 copies of 5 to the end of A. Then, A=(3,3,5,5,5,5).
  • 3rd query: Remove the first 3 elements from A. Then, the sum of the removed 3 integers is 3+3+5=11, so output 11. After removal, A=(5,5,5).
  • 4th query: Add 6 copies of 2 to the end of A. Then, A=(5,5,5,2,2,2,2,2,2).
  • 5th query: Remove the first 5 elements from A. Then, the sum of the removed 5 integers is 5+5+5+2+2=19, so output 19. After removal, A=(2,2,2,2).

Sample Input 2

10
1 75 22
1 81 72
1 2 97
1 84 82
1 2 32
1 39 57
2 45
1 40 16
2 32
2 42

Sample Output 2

990
804
3024

Sample Input 3

10
1 160449218 954291757
2 17217760
1 353195922 501899080
1 350034067 910748511
1 824284691 470338674
2 180999835
1 131381221 677959980
1 346948152 208032501
1 893229302 506147731
2 298309896

Sample Output 3

16430766442004320
155640513381884866
149721462357295680
G - Takahashi the Wall Breaker

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は魚屋にウナギを買いに行こうとしています。

高橋君の住む町は縦 H マス横 W マスからなるグリッド状の区画に分かれており、 各区画は道か壁かのいずれかです。
以下、上から i 番目 (1\leq i \leq H) かつ左から j 番目 (1\leq j\leq W) の区画を区画 (i,j) で表します。
各区画の情報は H 個の長さ W の文字列 S_1,S_2,\ldots,S_H で与えられ、 具体的には S_ij 文字目 (1\leq i \leq H,1\leq j\leq W). のとき区画 (i,j) が道であることを、 S_ij 文字目が # のとき区画 (i,j) が壁であることを表します。

高橋君は次の 2 種類の行動を好きな順番で繰り返し行うことができます。

  • 上下左右に隣接する、町の中の区画であって、道であるようなものに移動する。
  • 上下左右の方向を一つ決め、その方向に前蹴りを行う。
    高橋君が前蹴りを行うと、現在いる区画からその方向に 1 つ前の区画および 2 つ前の区画について、その区画が壁ならば道に変えることができる。
    ここで、1 つ前の区画または 2 つ前の区画が町の外である場合でも前蹴りを行うことができるが、町の外が変化することはない。

高橋君は最初、区画 (A,B) におり、区画 (C,D) にある魚屋まで移動したいです。
ここで、高橋君の最初にいる区画および魚屋のある区画は道であることが保証されます。
高橋君が魚屋にたどり着くために必要な 前蹴りの回数 の最小値を求めてください。

制約

  • 1\leq H\leq 1000
  • 1\leq W\leq 1000
  • S_i., # のみからなる長さ W の文字列
  • 1\leq A,C\leq H
  • 1\leq B,D\leq W
  • (A,B)\neq (C,D)
  • H,W,A,B,C,D は整数
  • 高橋君の最初にいる区画および魚屋のある区画は道である。

入力

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

H W
S_1
S_2
\vdots
S_H
A B C D

出力

高橋君が魚屋にたどり着くために必要な 前蹴りの回数 の最小値を出力せよ。


入力例 1

10 10
..........
#########.
#.......#.
#..####.#.
##....#.#.
#####.#.#.
.##.#.#.#.
###.#.#.#.
###.#.#.#.
#.....#...
1 1 7 1

出力例 1

1

高橋君は最初、区画 (1,1) にいます。
道である区画への移動を繰り返して区画 (7,4) まで移動することができます。
区画 (7,4) において左方向に対して前蹴りを行うと区画 (7,3) と区画 (7,2) が壁から道に変わります。
その後、道である区画(道に変わった区画を含む)への移動を繰り返して、区画 (7,1) にある魚屋に移動することができます。

このとき、前蹴りを行った回数は 1 回であり、前蹴りを行わずに魚屋へ移動することは不可能なため、1 を出力します。


入力例 2

2 2
.#
#.
1 1 2 2

出力例 2

1

高橋君は最初、区画 (1,1) にいます。
右方向に対して前蹴りを行うと、区画 (1,2) を壁から道に変えることができます。
区画 (1,1) から右方向に 2 つ前のマスは町の外であるため、変化しません。
その後、高橋君は区画 (1,2) へ移動し、区画 (2,2) にある魚屋へ移動することができます。

このとき、前蹴りを行った回数は 1 回であり、前蹴りを行わずに魚屋へ移動することは不可能なため、1 を出力します。


入力例 3

1 3
.#.
1 1 1 3

出力例 3

1

前蹴りを行う際、それにより道に変えられ得る区画の中に魚屋のある区画が含まれていても問題ありません。 具体的には魚屋のある区画はもともと道であるため変化せず、特に前蹴りによって魚屋が壊れることはありません。


入力例 4

20 20
####################
##...##....###...###
#.....#.....#.....##
#..#..#..#..#..#..##
#..#..#....##..#####
#.....#.....#..#####
#.....#..#..#..#..##
#..#..#.....#.....##
#..#..#....###...###
####################
####################
##..#..##...###...##
##..#..#.....#.....#
##..#..#..#..#..#..#
##..#..#..#..#..#..#
##.....#..#..#..#..#
###....#..#..#..#..#
#####..#.....#.....#
#####..##...###...##
####################
3 3 18 18

出力例 4

3

Score : 400 points

Problem Statement

Takahashi is about to go buy eel at a fish shop.

The town where he lives is divided into a grid of H rows and W columns. Each cell is either a road or a wall.
Let us denote the cell at the i-th row from the top (1\leq i \leq H) and the j-th column from the left (1\leq j \leq W) as cell (i,j).
Information about each cell is given by H strings S_1,S_2,\ldots,S_H, each of length W. Specifically, if the j-th character of S_i (1\leq i \leq H,1\leq j\leq W) is ., cell (i,j) is a road; if it is #, cell (i,j) is a wall.

He can repeatedly perform the following two types of actions in any order:

  • Move to an adjacent cell (up, down, left, or right) that is within the town and is a road.
  • Choose one of the four directions (up, down, left, or right) and perform a front kick in that direction.
    When he performs a front kick, for each of the cells at most 2 steps away in that direction from the cell he is currently in, if that cell is a wall, it becomes a road.
    If some of the cells at most 2 steps away are outside the town, a front kick can still be performed, but anything outside the town does not change.

He starts in cell (A,B), and he wants to move to the fish shop in cell (C,D).
It is guaranteed that both the cell where he starts and the cell with the fish shop are roads.
Find the minimum number of front kicks he needs in order to reach the fish shop.

Constraints

  • 1\leq H\leq 1000
  • 1\leq W\leq 1000
  • Each S_i is a string of length W consisting of . and #.
  • 1\leq A,C\leq H
  • 1\leq B,D\leq W
  • (A,B)\neq (C,D)
  • H, W, A, B, C, and D are integers.
  • The cell where Takahashi starts and the cell with the fish shop are roads.

Input

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

H W
S_1
S_2
\vdots
S_H
A B C D

Output

Print the minimum number of front kicks needed for Takahashi to reach the fish shop.


Sample Input 1

10 10
..........
#########.
#.......#.
#..####.#.
##....#.#.
#####.#.#.
.##.#.#.#.
###.#.#.#.
###.#.#.#.
#.....#...
1 1 7 1

Sample Output 1

1

Takahashi starts in cell (1,1).
By repeatedly moving to adjacent road cells, he can reach cell (7,4).
If he performs a front kick to the left from cell (7,4), cells (7,3) and (7,2) turn from walls to roads.
Then, by continuing to move through road cells (including those that have become roads), he can reach the fish shop in cell (7,1).

In this case, the number of front kicks performed is 1, and it is impossible to reach the fish shop without performing any front kicks, so print 1.


Sample Input 2

2 2
.#
#.
1 1 2 2

Sample Output 2

1

Takahashi starts in cell (1,1).
When he performs a front kick to the right, cell (1,2) turns from a wall to a road.
The cell two steps to the right of (1,1) is outside the town, so it does not change.
Then, he can move to cell (1,2) and then to the fish shop in cell (2,2).

In this case, the number of front kicks performed is 1, and it is impossible to reach the fish shop without performing any front kicks, so print 1.


Sample Input 3

1 3
.#.
1 1 1 3

Sample Output 3

1

When performing a front kick, it is fine if the fish shop’s cell is within the cells that could be turned into a road. Specifically, the fish shop’s cell is a road from the beginning, so it remains unchanged; particularly, the shop is not destroyed by the front kick.


Sample Input 4

20 20
####################
##...##....###...###
#.....#.....#.....##
#..#..#..#..#..#..##
#..#..#....##..#####
#.....#.....#..#####
#.....#..#..#..#..##
#..#..#.....#.....##
#..#..#....###...###
####################
####################
##..#..##...###...##
##..#..#.....#.....#
##..#..#..#..#..#..#
##..#..#..#..#..#..#
##.....#..#..#..#..#
###....#..#..#..#..#
#####..#.....#.....#
#####..##...###...##
####################
3 3 18 18

Sample Output 4

3
H - K-colinear Line

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

座標平面上の N 個の点が与えられます。 1\leq i\leq N について、i 番目の点の座標は (X_i, Y_i) です。

座標平面上の直線であって、N 個の点のうち K 個以上の点を通るものの個数を求めてください。
ただし、そのようなものが無数に存在する場合は Infinity を出力してください。

制約

  • 1 \leq K \leq N \leq 300
  • \lvert X_i \rvert, \lvert Y_i \rvert \leq 10^9
  • i\neq j ならば X_i\neq X_j または Y_i\neq Y_j
  • 入力はすべて整数

入力

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

N K
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

与えられた N 個の点のうち K 個以上の点を通る直線の数を出力せよ。ただし、そのようなものが無数に存在する場合は Infinity を出力せよ。


入力例 1

5 2
0 0
1 0
0 1
-1 0
0 -1

出力例 1

6

x=0, y=0, y=x\pm 1, y=-x\pm 16 本の直線が条件をみたします。
例えば、x=0 は、1, 3, 5 番目の 3 個の点を通ります。

よって、6 を出力します。


入力例 2

1 1
0 0

出力例 2

Infinity

原点を通る直線は無数に存在します。 よって、Infinity を出力します。

Score : 500 points

Problem Statement

You are given N points in the coordinate plane. For each 1\leq i\leq N, the i-th point is at the coordinates (X_i, Y_i).

Find the number of lines in the plane that pass K or more of the N points.
If there are infinitely many such lines, print Infinity.

Constraints

  • 1 \leq K \leq N \leq 300
  • \lvert X_i \rvert, \lvert Y_i \rvert \leq 10^9
  • X_i\neq X_j or Y_i\neq Y_j, if i\neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the number of lines in the plane that pass K or more of the N points, or Infinity if there are infinitely many such lines.


Sample Input 1

5 2
0 0
1 0
0 1
-1 0
0 -1

Sample Output 1

6

The six lines x=0, y=0, y=x\pm 1, and y=-x\pm 1 satisfy the requirement.
For example, x=0 passes the first, third, and fifth points.

Thus, 6 should be printed.


Sample Input 2

1 1
0 0

Sample Output 2

Infinity

Infinitely many lines pass the origin.

Thus, Infinity should be printed.

I - SSttrriinngg in StringString

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

長さ n の文字列 X に対して、Xk 回繰り返して得られる文字列を f(X,k) と表記し、X1 文字目、2 文字目、\dotsn 文字目を k 回ずつこの順に繰り返して得られる文字列を g(X,k) と表記します。 例えば、X= abc のとき、f(X,2)= abcabcg(X,3)= aaabbbccc です。 また、任意の文字列 X に対して、f(X,0)g(X,0) は共に空文字列です。

正整数 N および文字列 S,T が与えられます。 g(T,k)f(S,N) の(連続とは限らない)部分列であるような最大の非負整数 k を求めてください。 なお、定義より、g(T,0) は常に f(S,N) の部分列であることに注意してください。

部分列とは 文字列 X の(連続とは限らない)部分列とは、X から 0 個以上の文字を取り除いた後、残りの文字を元の順序で連結して得られる文字列のことをいいます。 例えば、acatcoder (空文字列)などはどれも atcoder の部分列ですが、taatcoder の部分列ではありません。

制約

  • N は整数
  • 1\leq N\leq 10^{12}
  • S, T は英小文字からなる長さ 1 以上 10^5 以下の文字列

入力

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

N
S
T

出力

g(T,k)f(S,N) の(連続とは限らない)部分列であるような最大の非負整数 k を出力せよ。


入力例 1

3
abc
ab

出力例 1

2

f(S,3)= abcabcabc です。 g(T,2)= aabbf(S,3) の部分列ですが、g(T,3)= aaabbbf(S,3) の部分列ではないので、2 を出力します。


入力例 2

3
abc
arc

出力例 2

0

入力例 3

1000000000000
kzazkakxkk
azakxk

出力例 3

344827586207

Score: 525 points

Problem Statement

For a string X of length n, let f(X,k) denote the string obtained by repeating k times the string X, and g(X,k) denote the string obtained by repeating k times the first character, the second character, \dots, the n-th character of X, in this order. For example, if X= abc, then f(X,2)= abcabc, and g(X,3)= aaabbbccc. Also, for any string X, both f(X,0) and g(X,0) are empty strings.

You are given a positive integer N and strings S and T. Find the largest non-negative integer k such that g(T,k) is a (not necessarily contiguous) subsequence of f(S,N). Note that g(T,0) is always a subsequence of f(S,N) by definition.

What is a subsequence? A (not necessarily contiguous) subsequence of a string X is a string obtained by removing zero or more characters from X and then concatenating the remaining elements without changing the order. For example, ac, atcoder, and (empty string) are all subsequences of atcoder, but ta is not.

Constraints

  • N is an integer.
  • 1\leq N\leq 10^{12}
  • S and T are strings consisting of lowercase English letters with lengths between 1 and 10^5, inclusive.

Input

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

N
S
T

Output

Print the largest non-negative integer k such that g(T,k) is a (not necessarily contiguous) subsequence of f(S,N).


Sample Input 1

3
abc
ab

Sample Output 1

2

We have f(S,3)= abcabcabc. g(T,2)= aabb is a subsequence of f(S,3), but g(T,3)= aaabbb is not, so print 2.


Sample Input 2

3
abc
arc

Sample Output 2

0

Sample Input 3

1000000000000
kzazkakxkk
azakxk

Sample Output 3

344827586207