A - Weird Function

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

配点 : 100

問題文

関数 ff(x) = x^2 + 2x + 3 と定義します。
整数 t が入力されるので、 f(f(f(t)+t)+f(f(t))) を求めてください。
ただし、答えは 2 \times 10^9 以下の整数であることが保証されます。

制約

  • t0 以上 10 以下の整数である

入力

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

t

出力

答えを整数として出力せよ。


入力例 1

0

出力例 1

1371

答えは以下の手順によって計算されます。

  • f(t) = t^2 + 2t + 3 = 0 \times 0 + 2 \times 0 + 3 = 3
  • f(t)+t = 3 + 0 = 3
  • f(f(t)+t) = f(3) = 3 \times 3 + 2 \times 3 + 3 = 18
  • f(f(t)) = f(3) = 18
  • f(f(f(t)+t)+f(f(t))) = f(18+18) = f(36) = 36 \times 36 + 2 \times 36 + 3 = 1371

入力例 2

3

出力例 2

722502

入力例 3

10

出力例 3

1111355571

Score : 100 points

Problem Statement

Let us define a function f as f(x) = x^2 + 2x + 3.
Given an integer t, find f(f(f(t)+t)+f(f(t))).
Here, it is guaranteed that the answer is an integer not greater than 2 \times 10^9.

Constraints

  • t is an integer between 0 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

t

Output

Print the answer as an integer.


Sample Input 1

0

Sample Output 1

1371

The answer is computed as follows.

  • f(t) = t^2 + 2t + 3 = 0 \times 0 + 2 \times 0 + 3 = 3
  • f(t)+t = 3 + 0 = 3
  • f(f(t)+t) = f(3) = 3 \times 3 + 2 \times 3 + 3 = 18
  • f(f(t)) = f(3) = 18
  • f(f(f(t)+t)+f(f(t))) = f(18+18) = f(36) = 36 \times 36 + 2 \times 36 + 3 = 1371

Sample Input 2

3

Sample Output 2

722502

Sample Input 3

10

Sample Output 3

1111355571
B - Shift

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

配点 : 100

問題文

長さ N の数列 A = (A_1, A_2, \dots, A_N) が与えられます。
あなたは次の操作をちょうど K 回行います。

  • A の先頭の要素を取り除き、A の末尾に 0 を挿入する。

操作を行った後の A の要素をすべて出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq K \leq 100
  • 1 \leq A_i \leq 100
  • 入力される値はすべて整数

入力

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

N K
A_1 A_2 \dots A_N

出力

操作を行った後の A の要素を空白区切りで 1 行に出力せよ。


入力例 1

3 2
2 7 8

出力例 1

8 0 0

操作を行う前は A = (2, 7, 8) です。
操作を 1 回行った時点では A = (7, 8, 0) です。
操作を 2 回行った時点では A = (8, 0, 0) です。
よって (8, 0, 0) が答えです。


入力例 2

3 4
9 9 9

出力例 2

0 0 0

入力例 3

9 5
1 2 3 4 5 6 7 8 9

出力例 3

6 7 8 9 0 0 0 0 0

Score : 100 points

Problem Statement

You are given a sequence A = (A_1, A_2, \dots, A_N) of length N.
You perform the following operation exactly K times:

  • remove the initial element of A and append a 0 to the tail of A.

Print all the elements of A after the operations.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq K \leq 100
  • 1 \leq A_i \leq 100
  • All values in the input are integers.

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the elements of A after the operations in one line, separated by spaces.


Sample Input 1

3 2
2 7 8

Sample Output 1

8 0 0

Before the operations, A = (2, 7, 8).
After performing the operation once, A = (7, 8, 0).
After performing the operation twice, A = (8, 0, 0).
Thus, (8, 0, 0) is the answer.


Sample Input 2

3 4
9 9 9

Sample Output 2

0 0 0

Sample Input 3

9 5
1 2 3 4 5 6 7 8 9

Sample Output 3

6 7 8 9 0 0 0 0 0
C - Prefix and Suffix

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

配点 : 200

問題文

英小文字からなる文字列 S, T が与えられます。S の長さは NT の長さは M です。(N \leq M が制約で保証されています)

ST接頭辞 であるとは、T のはじめ N 文字からなる文字列が S と一致することを言います。
ST接尾辞 であるとは、T の後ろ N 文字からなる文字列が S と一致することを言います。

ST の接頭辞であり、かつ接尾辞でもある場合は 0 を、
ST の接頭辞であるが、接尾辞でない場合は 1 を、
ST の接尾辞であるが、接頭辞でない場合は 2 を、
ST の接頭辞でも接尾辞でもない場合は 3 を出力してください。

制約

  • 1 \leq N \leq M \leq 100
  • S は英小文字からなる長さ N の文字列
  • T は英小文字からなる長さ M の文字列

入力

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

N M
S
T

出力

問題文の指示に従って答えを出力せよ。


入力例 1

3 7
abc
abcdefg

出力例 1

1

ST の接頭辞ですが接尾辞ではありません。よって 1 を出力します。


入力例 2

3 4
abc
aabc

出力例 2

2

ST の接尾辞ですが接頭辞ではありません。


入力例 3

3 3
abc
xyz

出力例 3

3

ST の接頭辞でも接尾辞でもありません。


入力例 4

3 3
aaa
aaa

出力例 4

0

ST が完全に一致する場合もあります。この場合、ST の接頭辞であり、かつ接尾辞でもあります。

Score : 200 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters. The lengths of S and T are N and M, respectively. (The constraints guarantee that N \leq M.)

S is said to be a prefix of T when the first N characters of T coincide S.
S is said to be a suffix of T when the last N characters of T coincide S.

If S is both a prefix and a suffix of T, print 0;
If S is a prefix of T but not a suffix, print 1;
If S is a suffix of T but not a prefix, print 2;
If S is neither a prefix nor a suffix of T, print 3.

Constraints

  • 1 \leq N \leq M \leq 100
  • S is a string of length N consisting of lowercase English letters.
  • T is a string of length M consisting of lowercase English letters.

Input

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

N M
S
T

Output

Print the answer according to the instructions in the problem statement.


Sample Input 1

3 7
abc
abcdefg

Sample Output 1

1

S is a prefix of T but not a suffix, so you should print 1.


Sample Input 2

3 4
abc
aabc

Sample Output 2

2

S is a suffix of T but not a prefix.


Sample Input 3

3 3
abc
xyz

Sample Output 3

3

S is neither a prefix nor a suffix of T.


Sample Input 4

3 3
aaa
aaa

Sample Output 4

0

S and T may coincide, in which case S is both a prefix and a suffix of T.

D - Hit and Blow

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

配点 : 200

問題文

長さ N の整数列 A = (A_1, A_2, \dots, A_N), B = (B_1, B_2, \dots, B_N) が与えられます。
A の要素はすべて異なります。B の要素もすべて異なります。

次の 2 つを出力してください。

  1. A にも B にも含まれ、その位置も一致している整数の個数。言い換えると、A_i = B_i を満たす整数 i の個数。
  2. A にも B にも含まれるが、その位置は異なる整数の個数。言い換えると、A_i = B_j, i \neq j を満たす整数の組 (i, j) の個数。

制約

  • 1 \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • A_1, A_2, \dots, A_N はすべて異なる。
  • B_1, B_2, \dots, B_N はすべて異なる。
  • 入力はすべて整数である。

入力

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

答えを 2 行出力せよ。1 行目には 1. の個数、2 行目には 2. の個数を出力せよ。


入力例 1

4
1 3 5 2
2 3 1 4

出力例 1

1
2

A にも B にも含まれ、その位置も一致している整数は A_2 = B_2 = 31 個です。
A にも B にも含まれるが、その位置は異なる整数は A_1 = B_3 = 1A_4 = B_1 = 22 個です。


入力例 2

3
1 2 3
4 5 6

出力例 2

0
0

1., 2. ともに条件を満たす整数は存在しません。


入力例 3

7
4 8 1 7 9 5 6
3 5 1 7 8 2 6

出力例 3

3
2

Score : 200 points

Problem Statement

You are given integer sequences, each of length N: A = (A_1, A_2, \dots, A_N) and B = (B_1, B_2, \dots, B_N).
All elements of A are different. All elements of B are different, too.

Print the following two values.

  1. The number of integers contained in both A and B, appearing at the same position in the two sequences. In other words, the number of integers i such that A_i = B_i.
  2. The number of integers contained in both A and B, appearing at different positions in the two sequences. In other words, the number of pairs of integers (i, j) such that A_i = B_j and i \neq j.

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • A_1, A_2, \dots, A_N are all different.
  • B_1, B_2, \dots, B_N are all different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Print the answers in two lines: the answer to1. in the first line, and the answer to2. in the second line.


Sample Input 1

4
1 3 5 2
2 3 1 4

Sample Output 1

1
2

There is one integer contained in both A and B, appearing at the same position in the two sequences: A_2 = B_2 = 3.
There are two integers contained in both A and B, appearing at different positions in the two sequences: A_1 = B_3 = 1 and A_4 = B_1 = 2.


Sample Input 2

3
1 2 3
4 5 6

Sample Output 2

0
0

In both 1. and 2., no integer satisfies the condition.


Sample Input 3

7
4 8 1 7 9 5 6
3 5 1 7 8 2 6

Sample Output 3

3
2
E - T-shirts

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

配点 : 300

問題文

AtCoder 社はロゴ入りの T シャツを販売しています。

高橋君の N 日間の予定が 0, 1, 2 のみからなる長さ N の文字列 S で与えられます。
具体的には、1\leq i\leq N をみたす整数 i について、

  • Si 文字目が 0 のとき、i 日目に何の予定も入っていません。
  • Si 文字目が 1 のとき、i 日目に高橋君は食事に行く予定があります。
  • Si 文字目が 2 のとき、i 日目に高橋君は競技プログラミングのイベントに行く予定が入っています。

高橋君は無地の T シャツを M 枚持っており、1 日目の直前の時点ですべて洗濯済みの状態となっています。
これに加えて、次の条件をみたすように行動できるように、高橋君は AtCoder のロゴ入りの T シャツを何枚か購入する事にしました。

  • 食事に行く日には、無地の T シャツ 1 枚またはロゴ入りの T シャツ 1 枚を着用する。
  • 競技プログラミングのイベントに行く日にはロゴ入りの T シャツ 1 枚を着用する。
  • 何の予定もない日には T シャツを着用しない。加えて、その時点で着用済みの T シャツを全て洗濯する。 洗濯した T シャツは翌日から着用できる。
  • 一度着用した T シャツは次に洗濯するまで着用できない。

N 日間のうち予定が入っている日すべてについて、条件をみたす T シャツを着用できるようにするために、高橋君は最低何枚のTシャツを購入する必要があるか求めてください。 新しく T シャツを購入する必要がないならば 0 を出力してください。
ただし、購入した T シャツも 1 日目の直前の時点ですべて洗濯済みの状態で存在するものとします。

制約

  • 1\leq M\leq N\leq 1000
  • S0, 1, 2 のみからなる長さ N の文字列
  • N,M は整数

入力

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

N M
S

出力

問題文の条件をみたすように行動するために 高橋君が購入する必要のある T シャツの枚数の最小値を出力せよ。
新しく購入する必要がないならば 0 を出力せよ。


入力例 1

6 1
112022

出力例 1

2

高橋君がロゴ入りの T シャツを 2 枚購入したとき、次のようにして高橋君は T シャツを着用することができます。

  • 1 日目、高橋君はロゴ入りの T シャツを着用して食事に行きます。
  • 2 日目、高橋君は無地の T シャツを着用して食事に行きます。
  • 3 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
  • 4 日目、高橋君は予定がないため、着用した T シャツをすべて洗濯します。これにより、1,2,3 日目に着用した T シャツを再び着用することが可能になります。
  • 5 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
  • 6 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。

高橋君がロゴ入りの T シャツを 1 枚以下しか購入しなかった場合には、 どのようにしても条件をみたすように T シャツを着用することができません。
よって、2 を出力します。


入力例 2

3 1
222

出力例 2

3

入力例 3

2 1
01

出力例 3

0

高橋君は新しく T シャツを購入する必要はありません。

Score : 300 points

Problem Statement

AtCoder Inc. sells T-shirts with its logo.

You are given Takahashi's schedule for N days as a string S of length N consisting of 0, 1, and 2.
Specifically, for an integer i satisfying 1\leq i\leq N,

  • if the i-th character of S is 0, he has no plan scheduled for the i-th day;
  • if the i-th character of S is 1, he plans to go out for a meal on the i-th day;
  • if the i-th character of S is 2, he plans to attend a competitive programming event on the i-th day.

Takahashi has M plain T-shirts, all washed and ready to wear just before the first day.
In addition, to be able to satisfy the following conditions, he will buy several AtCoder logo T-shirts.

  • On days he goes out for a meal, he will wear a plain or logo T-shirt.
  • On days he attends a competitive programming event, he will wear a logo T-shirt.
  • On days with no plans, he will not wear any T-shirts. Also, he will wash all T-shirts worn at that point. He can wear them again from the next day onwards.
  • Once he wears a T-shirt, he cannot wear it again until he washes it.

Determine the minimum number of T-shirts he needs to buy to be able to wear appropriate T-shirts on all scheduled days during the N days. If he does not need to buy new T-shirts, print 0.
Assume that the purchased T-shirts are also washed and ready to use just before the first day.

Constraints

  • 1\leq M\leq N\leq 1000
  • S is a string of length N consisting of 0, 1, and 2.
  • N and M are integers.

Input

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

N M
S

Output

Print the minimum number of T-shirts Takahashi needs to buy to be able to satisfy the conditions in the problem statement.
If he does not need to buy new T-shirts, print 0.


Sample Input 1

6 1
112022

Sample Output 1

2

If Takahashi buys two logo T-shirts, he can wear T-shirts as follows:

  • On the first day, he wears a logo T-shirt to go out for a meal.
  • On the second day, he wears a plain T-shirt to go out for a meal.
  • On the third day, he wears a logo T-shirt to attend a competitive programming event.
  • On the fourth day, he has no plans, so he washes all the worn T-shirts. This allows him to reuse the T-shirts worn on the first, second, and third days.
  • On the fifth day, he wears a logo T-shirt to attend a competitive programming event.
  • On the sixth day, he wears a logo T-shirt to attend a competitive programming event.

If he buys one or fewer logo T-shirts, he cannot use T-shirts to meet the conditions no matter what. Hence, print 2.


Sample Input 2

3 1
222

Sample Output 2

3

Sample Input 3

2 1
01

Sample Output 3

0

He does not need to buy new T-shirts.

F - Doukasen

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

配点 : 300

問題文

N 本の導火線を一直線に接着したものがあります。左から i 本目の導火線は長さが A_i cm で、 1 秒あたり B_i cm の一定の速さで燃えます。

この導火線の左端と右端から同時に火をつけるとき、 2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 1000
  • 入力は全て整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か(単位を除いて)出力せよ。

想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。


入力例 1

3
1 1
2 1
3 1

出力例 1

3.000000000000000

着火前の導火線の左端から 3 cm の地点で 2 つの火がぶつかります。


入力例 2

3
1 3
2 2
3 1

出力例 2

3.833333333333333

入力例 3

5
3 9
1 2
4 6
1 5
5 3

出力例 3

8.916666666666668

Score : 300 points

Problem Statement

We have N fuses connected in series. The i-th fuse from the left has a length of A_i centimeters and burns at a constant speed of B_i centimeters per second.

Consider igniting this object from left and right ends simultaneously. Find the distance between the position where the two flames will meet and the left end of the object.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the distance between the position where the two flames will meet and the left end of the object, in centimeters (print just the number).

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-5}.


Sample Input 1

3
1 1
2 1
3 1

Sample Output 1

3.000000000000000

The two flames will meet at 3 centimeters from the left end of the object.


Sample Input 2

3
1 3
2 2
3 1

Sample Output 2

3.833333333333333

Sample Input 3

5
3 9
1 2
4 6
1 5
5 3

Sample Output 3

8.916666666666668
G - Together Square

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

配点 : 400

問題文

整数 N が与えられます。以下の条件を満たす N 以下の正整数の組 (i,j) の個数を求めてください。

  • i \times j は平方数である。

制約

  • 1 \le N \le 2 \times 10^5
  • N は整数である。

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

6

(1,1),(1,4),(2,2),(3,3),(4,1),(4,4)6 個が条件を満たします。

(2,3)2 \times 3 =6 が平方数でないため条件を満たしません。


入力例 2

254

出力例 2

896

Score : 400 points

Problem Statement

You are given an integer N. Find the number of pairs (i,j) of positive integers at most N that satisfy the following condition:

  • i \times j is a square number.

Constraints

  • 1 \le N \le 2 \times 10^5
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

6

The six pairs (1,1),(1,4),(2,2),(3,3),(4,1),(4,4) satisfy the condition.

On the other hand, (2,3) does not, since 2 \times 3 =6 is not a square number.


Sample Input 2

254

Sample Output 2

896
H - Bishop 2

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

配点 : 500

コンテスト開催当時はメモリ制限が2GBでしたが、ジャッジ環境が異なるため、現在はメモリ制限を1GBに設定しております。なお、このメモリ制限でもAC出来ることは確認しています。

問題文

ここに、 N \times N のチェス盤があります。このチェス盤の上から i 行目、左から j 列目にあるマスをマス (i,j) と呼びます。
チェス盤の情報は N 個の文字列 S_i として与えられます。
文字列 S_ij 文字目である S_{i,j} には、以下の情報が含まれています。

  • S_{i,j}= . のとき マス (i,j) には何も置かれていない。
  • S_{i,j}= # のとき マス (i,j) には白のポーンが 1 つ置かれている。このポーンを動かしたり取り除いたりすることはできない。

この盤面のマス (A_x,A_y) に、白のビショップを 1 つ置きました。
この白のビショップをチェスのルール (注記参照) に従ってマス (A_x,A_y) からマス (B_x,B_y) に移動させるために必要な最小の手数を求めてください。
ただし、移動できない場合は代わりに -1 を出力してください。

注記

マス (i,j) に置かれている白の ビショップ は、 1 手で以下のルールに従って移動することができます。

  • 各正整数 d について、以下の条件を全て満たせばマス (i+d,j+d) に移動できる。

    • マス (i+d,j+d) が盤内に存在する
    • 全ての正整数 l \le d について、 (i+l,j+l) に白のポーンがない
  • 各正整数 d について、以下の条件を全て満たせばマス (i+d,j-d) に移動できる。

    • マス (i+d,j-d) が盤内に存在する
    • 全ての正整数 l \le d について、 (i+l,j-l) に白のポーンがない
  • 各正整数 d について、以下の条件を全て満たせばマス (i-d,j+d) に移動できる。

    • マス (i-d,j+d) が盤内に存在する
    • 全ての正整数 l \le d について、 (i-l,j+l) に白のポーンがない
  • 各正整数 d について、以下の条件を全て満たせばマス (i-d,j-d) に移動できる。

    • マス (i-d,j-d) が盤内に存在する
    • 全ての正整数 l \le d について、 (i-l,j-l) に白のポーンがない

制約

  • 2 \le N \le 1500
  • 1 \le A_x,A_y \le N
  • 1 \le B_x,B_y \le N
  • (A_x,A_y) \neq (B_x,B_y)
  • S_i. および # からなる N 文字の文字列
  • S_{A_x,A_y}= .
  • S_{B_x,B_y}= .

入力

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

N
A_x A_y
B_x B_y
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5
1 3
3 5
....#
...#.
.....
.#...
#....

出力例 1

3

以下のように移動させることで 3 手でビショップを (1,3) から (3,5) まで移動させることができます。 2 手以内でビショップを (1,3) から (3,5) まで移動させることはできません。

  • (1,3) \rightarrow (2,2) \rightarrow (4,4) \rightarrow (3,5)

入力例 2

4
3 2
4 2
....
....
....
....

出力例 2

-1

どのようにビショップを動かしても (3,2) から (4,2) に移動させることはできません。


入力例 3

18
18 1
1 18
..................
.####.............
.#..#..####.......
.####..#..#..####.
.#..#..###...#....
.#..#..#..#..#....
.......####..#....
.............####.
..................
..................
.####.............
....#..#..#.......
.####..#..#..####.
.#.....####..#....
.####.....#..####.
..........#..#..#.
.............####.
..................

出力例 3

9

Score : 500 points

During the time of the contest, the memory limit was set to 2GB. However, due to a change in the judging environment, the memory limit has now been set to 1GB. Please note that it has been confirmed that solutions can still achieve an Acceptable Completion (AC) within this memory limit.

Problem Statement

We have an N \times N chessboard. Let (i, j) denote the square at the i-th row from the top and j-th column from the left of this board.
The board is described by N strings S_i.
The j-th character of the string S_i, S_{i,j}, means the following.

  • If S_{i,j}= ., the square (i, j) is empty.
  • If S_{i,j}= #, the square (i, j) is occupied by a white pawn, which cannot be moved or removed.

We have put a white bishop on the square (A_x, A_y).
Find the minimum number of moves needed to move this bishop from (A_x, A_y) to (B_x, B_y) according to the rules of chess (see Notes).
If it cannot be moved to (B_x, B_y), report -1 instead.

Notes

A white bishop on the square (i, j) can go to the following positions in one move.

  • For each positive integer d, it can go to (i+d,j+d) if all of the conditions are satisfied.

    • The square (i+d,j+d) exists in the board.
    • For every positive integer l \le d, (i+l,j+l) is not occupied by a white pawn.
  • For each positive integer d, it can go to (i+d,j-d) if all of the conditions are satisfied.

    • The square (i+d,j-d) exists in the board.
    • For every positive integer l \le d, (i+l,j-l) is not occupied by a white pawn.
  • For each positive integer d, it can go to (i-d,j+d) if all of the conditions are satisfied.

    • The square (i-d,j+d) exists in the board.
    • For every positive integer l \le d, (i-l,j+l) is not occupied by a white pawn.
  • For each positive integer d, it can go to (i-d,j-d) if all of the conditions are satisfied.

    • The square (i-d,j-d) exists in the board.
    • For every positive integer l \le d, (i-l,j-l) is not occupied by a white pawn.

Constraints

  • 2 \le N \le 1500
  • 1 \le A_x,A_y \le N
  • 1 \le B_x,B_y \le N
  • (A_x,A_y) \neq (B_x,B_y)
  • S_i is a string of length N consisting of . and #.
  • S_{A_x,A_y}= .
  • S_{B_x,B_y}= .

Input

Input is given from Standard Input in the following format:

N
A_x A_y
B_x B_y
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5
1 3
3 5
....#
...#.
.....
.#...
#....

Sample Output 1

3

We can move the bishop from (1,3) to (3,5) in three moves as follows, but not in two or fewer moves.

  • (1,3) \rightarrow (2,2) \rightarrow (4,4) \rightarrow (3,5)

Sample Input 2

4
3 2
4 2
....
....
....
....

Sample Output 2

-1

There is no way to move the bishop from (3,2) to (4,2).


Sample Input 3

18
18 1
1 18
..................
.####.............
.#..#..####.......
.####..#..#..####.
.#..#..###...#....
.#..#..#..#..#....
.......####..#....
.............####.
..................
..................
.####.............
....#..#..#.......
.####..#..#..####.
.#.....####..#....
.####.....#..####.
..........#..#..#.
.............####.
..................

Sample Output 3

9
I - Shortest Good Path

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

配点 : 500

問題文

N 個の頂点と M 本の辺からなる単純(自己ループおよび多重辺を持たない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結びます。

下記の 2 つの条件をともに満たす整数列 (A_1, A_2, \ldots, A_k) を長さ kパスと呼びます。

  • すべての i = 1, 2, \dots, k について、1 \leq A_i \leq N
  • すべての i = 1, 2, \ldots, k-1 について、頂点 A_i と頂点 A_{i+1} は辺で直接結ばれている。

空列も長さ 0 のパスとみなします。

S = s_1s_2\ldots s_N01 のみからなる長さ N の文字列とします。 パス A = (A_1, A_2, \ldots, A_k) が下記を満たすとき、パス AS に関する良いパスと呼びます。

  • すべての i = 1, 2, \ldots, N について、次を満たす。
    • s_i = 0 ならば、A に含まれる i の個数は偶数である。
    • s_i = 1 ならば、A に含まれる i の個数は奇数である。

S として考えられる文字列(すなわち、01 のみからなる長さ N の文字列)は 2^N 個ありますが、そのすべてにわたる「 S に関する良いパスのうち最短のものの長さ」の総和を出力してください。

この問題の制約下において、01 からなる長さ N のどのような文字列を S として選んでも、S に関する良いパスが少なくとも 1 つ存在することが示せます。

制約

  • 2 \leq N \leq 17
  • N-1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq u_i, v_i \leq N
  • 与えられるグラフは単純かつ連結
  • 入力はすべて整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

答えを出力せよ。


入力例 1

3 2
1 2
2 3

出力例 1

14
  • S = 000 のとき、空列 ()S に関する最短の良いパスであり、その長さは 0 です。
  • S = 100 のとき、(1)S に関する最短の良いパスであり、その長さは 1 です。
  • S = 010 のとき、(2)S に関する最短の良いパスであり、その長さは 1 です。
  • S = 110 のとき、(1, 2)S に関する最短の良いパスであり、その長さは 2 です。
  • S = 001 のとき、(3)S に関する最短の良いパスであり、その長さは 1 です。
  • S = 101 のとき、(1, 2, 3, 2)S に関する最短の良いパスであり、その長さは 4 です。
  • S = 011 のとき、(2, 3)S に関する最短の良いパスであり、その長さは 2 です。
  • S = 111 のとき、(1, 2, 3)S に関する最短の良いパスであり、その長さは 3 です。

よって、求める答えは 0 + 1 + 1 + 2 + 1 + 4 + 2 + 3 = 14 です。


入力例 2

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

出力例 2

108

Score : 500 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. (A graph is said to be simple if it has no multi-edges and no self-loops.)
For i = 1, 2, \ldots, M, the i-th edge connects Vertex u_i and Vertex v_i.

A sequence (A_1, A_2, \ldots, A_k) is said to be a path of length k if both of the following two conditions are satisfied:

  • For all i = 1, 2, \dots, k, it holds that 1 \leq A_i \leq N.
  • For all i = 1, 2, \ldots, k-1, Vertex A_i and Vertex A_{i+1} are directly connected by an edge.

An empty sequence is regarded as a path of length 0.

Let S = s_1s_2\ldots s_N be a string of length N consisting of 0 and 1. A path A = (A_1, A_2, \ldots, A_k) is said to be a good path with respect to S if the following conditions are satisfied:

  • For all i = 1, 2, \ldots, N, it holds that:
    • if s_i = 0, then A has even number of i's.
    • if s_i = 1, then A has odd number of i's.

There are 2^N possible S (in other words, there are 2^N strings of length N consisting of 0 and 1). Find the sum of "the length of the shortest good path with respect to S" over all those S.

Under the Constraints of this problem, it can be proved that, for any string S of length N consisting of 0 and 1, there is at least one good path with respect to S.

Constraints

  • 2 \leq N \leq 17
  • N-1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq u_i, v_i \leq N
  • The given graph is simple and connected.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print the answer.


Sample Input 1

3 2
1 2
2 3

Sample Output 1

14
  • For S = 000, the empty sequence () is the shortest good path with respect to S, whose length is 0.
  • For S = 100, (1) is the shortest good path with respect to S, whose length is 1.
  • For S = 010, (2) is the shortest good path with respect to S, whose length is 1.
  • For S = 110, (1, 2) is the shortest good path with respect to S, whose length is 2.
  • For S = 001, (3) is the shortest good path with respect to S, whose length is 1.
  • For S = 101, (1, 2, 3, 2) is the shortest good path with respect to S, whose length is 4.
  • For S = 011, (2, 3) is the shortest good path with respect to S, whose length is 2.
  • For S = 111, (1, 2, 3) is the shortest good path with respect to S, whose length is 3.

Therefore, the sought answer is 0 + 1 + 1 + 2 + 1 + 4 + 2 + 3 = 14.


Sample Input 2

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

Sample Output 2

108