A - Gothec

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

以下の 5 つの日を五節句と呼びます。

  • 1\color{red}{\mathbf{7}}
  • 33
  • 55
  • 77
  • 99

MD 日が五節句に含まれるならば Yes を、含まれないならば No を出力してください。

制約

  • M, D は整数
  • MD 日はグレゴリオ暦における閏年 (うるうどし) の日付として正しい。

入力

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

M D

出力

MD 日が五節句に含まれるならば Yes を、含まれないならば No を出力せよ。


入力例 1

3 3

出力例 1

Yes

33 日は五節句に含まれるので、Yes を出力します。


入力例 2

1 1

出力例 2

No

11 日は五節句に含まれないので、No を出力します。


入力例 3

4 4

出力例 3

No

入力例 4

11 7

出力例 4

No

Score : 100 points

Problem Statement

The following five days are called the five seasonal festivals (gosekku).

  • January \color{red}{\mathbf{7}}
  • March 3
  • May 5
  • July 7
  • September 9

If month M, day D is one of the five seasonal festivals, output Yes; otherwise, output No.

Constraints

  • M and D are integers.
  • Month M, day D is a valid date in a leap year in the Gregorian calendar.

Input

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

M D

Output

If month M, day D is one of the five seasonal festivals, output Yes; otherwise, output No.


Sample Input 1

3 3

Sample Output 1

Yes

March 3 is one of the five seasonal festivals, so output Yes.


Sample Input 2

1 1

Sample Output 2

No

January 1 is not one of the five seasonal festivals, so output No.


Sample Input 3

4 4

Sample Output 3

No

Sample Input 4

11 7

Sample Output 4

No
B - Mod While Positive

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

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

M の値が 0 でない間以下の操作を繰り返すとき、操作を行う回数を求めてください。

  • NM で割った余りを x とする。M の値を x で置き換える。

なお、この操作を有限回行うことにより M = 0 になることが証明できます。

制約

  • 1 \leq N, M \leq 1000
  • 入力される値はすべて整数

入力

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

N M

出力

答えを出力せよ。


入力例 1

8 5

出力例 1

3

はじめ、N = 8, M = 5 です。

85 で割った余りは 3 であるため、1 回操作を行うと M = 3 となります。

83 で割った余りは 2 であるため、2 回操作を行うと M = 2 となります。

82 で割った余りは 0 であるため、3 回操作を行うと M = 0 となります。

したがって、3 を答えとして出力します。


入力例 2

14 6

出力例 2

2

入力例 3

460 33

出力例 3

5

Score : 100 points

Problem Statement

You are given positive integers N and M.

If the following operation is repeated while the value of M is not 0, find the number of operations performed.

  • Let x be the remainder when N is divided by M. Replace the value of M with x.

It can be proved that M becomes 0 after a finite number of operations.

Constraints

  • 1 \leq N, M \leq 1000
  • All input values are integers.

Input

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

N M

Output

Output the answer.


Sample Input 1

8 5

Sample Output 1

3

Initially, N = 8 and M = 5.

The remainder when 8 is divided by 5 is 3, so M = 3 after one operation.

The remainder when 8 is divided by 3 is 2, so M = 2 after two operations.

The remainder when 8 is divided by 2 is 0, so M = 0 after three operations.

Thus, output 3 as the answer.


Sample Input 2

14 6

Sample Output 2

2

Sample Input 3

460 33

Sample Output 3

5
C - Same Name

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の人がいます。i \, (1 \leq i \leq N) 人目の人の姓は S_i、名は T_i です。

同姓同名であるような人の組が存在するか、すなわち 1 \leq i \lt j \leq N かつ S_i=S_j かつ T_i=T_j を満たすような整数対 (i,j) が存在するか判定してください。

制約

  • 2 \leq N \leq 1000
  • N は整数
  • S_i,T_i は英小文字のみからなる長さ 1 以上 10 以下の文字列

入力

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

N
S_1 T_1
S_2 T_2
\hspace{0.6cm}\vdots
S_N T_N

出力

同姓同名であるような人の組が存在するなら Yes を、存在しないなら No を出力せよ。


入力例 1

3
tanaka taro
sato hanako
tanaka taro

出力例 1

Yes

1 人目の人と 3 人目の人が同姓同名です。


入力例 2

3
saito ichiro
saito jiro
saito saburo

出力例 2

No

同姓同名であるような人の組は存在しません。


入力例 3

4
sypdgidop bkseq
bajsqz hh
ozjekw mcybmtt
qfeysvw dbo

出力例 3

No

Score : 200 points

Problem Statement

There are N people. The family name and given name of the i-th person (1 \leq i \leq N) are S_i and T_i, respectively.

Determine whether there is a pair of people with the same family and given names. In other words, determine whether there is a pair of integers (i,j) such that 1 \leq i \lt j \leq N, S_i=S_j, and T_i=T_j.

Constraints

  • 2 \leq N \leq 1000
  • N is an integer.
  • Each of S_i and T_i is a string of length between 1 and 10 (inclusive) consisting of English lowercase letters.

Input

Input is given from Standard Input in the following format:

N
S_1 T_1
S_2 T_2
\hspace{0.6cm}\vdots
S_N T_N

Output

If there is a pair of people with the same family and given names, print Yes; otherwise, print No.


Sample Input 1

3
tanaka taro
sato hanako
tanaka taro

Sample Output 1

Yes

The first and third persons have the same family and given names.


Sample Input 2

3
saito ichiro
saito jiro
saito saburo

Sample Output 2

No

No two persons have the same family and given names.


Sample Input 3

4
sypdgidop bkseq
bajsqz hh
ozjekw mcybmtt
qfeysvw dbo

Sample Output 3

No
D - Coloring Matrix

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

各要素が 0 あるいは 1 である NN 列の行列 A, B が与えられます。
Ai 行目 j 列目の要素を A_{i,j}Bi 行目 j 列目の要素を B_{i,j} で表します。
A を適切に回転することで、 A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできるか判定してください。
ただし、A を回転するとは、以下の操作を好きな回数(0 回でもよい)繰り返すことをいいます。

  • 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について同時に A_{i,j}A_{N + 1 - j,i} で置き換える

制約

  • 1 \leq N \leq 100
  • A, B の各要素は 01 のいずれか
  • 入力はすべて整数

入力

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}

出力

A を適切に回転することで、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできる場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

出力例 1

Yes

はじめ、A

0 1 1
1 0 0
0 1 0

です。
1 回操作を行うと、A

0 1 0
1 0 1 
0 0 1

となります。
もう 1 度操作を行うと、A

0 1 0
0 0 1
1 1 0

となります。
このとき、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているので、Yes を出力します。


入力例 2

2
0 0
0 0
1 1
1 1

出力例 2

Yes

入力例 3

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

出力例 3

No

Score : 200 points

Problem Statement

You are given N-by-N matrices A and B where each element is 0 or 1.
Let A_{i,j} and B_{i,j} denote the element at the i-th row and j-th column of A and B, respectively.
Determine whether it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1.
Here, to rotate A is to perform the following operation zero or more times:

  • for every pair of integers (i, j) such that 1 \leq i, j \leq N, simultaneously replace A_{i,j} with A_{N + 1 - j,i}.

Constraints

  • 1 \leq N \leq 100
  • Each element of A and B is 0 or 1.
  • All values in the input are integers.

Input

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}

Output

If it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, print Yes; otherwise, print No.


Sample Input 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

Sample Output 1

Yes

Initially, A is :

0 1 1
1 0 0
0 1 0

After performing the operation once, A is :

0 1 0
1 0 1 
0 0 1

After performing the operation once again, A is :

0 1 0
0 0 1
1 1 0

Here, B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, so you should print Yes.


Sample Input 2

2
0 0
0 0
1 1
1 1

Sample Output 2

Yes

Sample Input 3

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

Sample Output 3

No
E - X drawing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

上下左右に広がる N\times N のマス目があり、最初全てのマスは白く塗られています。このマス目の上から i 行目、左から j 列目のマスを (i,j) で表します。

高橋君は 1 以上 N 以下の整数 A, B を持っており、次のような操作を行います。

  • \max(1-A,1-B)\leq k\leq \min(N-A,N-B) をみたす全ての整数 k について、(A+k,B+k) を黒く塗る。
  • \max(1-A,B-N)\leq k\leq \min(N-A,B-1) をみたす全ての整数 k について、(A+k,B-k) を黒く塗る。

この操作を行った後のマス目について、P\leq i\leq Q かつ R\leq j\leq S をみたす各マス (i,j) がそれぞれ何色で塗られているか求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • 1 \leq A \leq N
  • 1 \leq B \leq N
  • 1 \leq P \leq Q \leq N
  • 1 \leq R \leq S \leq N
  • (Q-P+1)\times(S-R+1)\leq 3\times 10^5
  • 入力は全て整数である。

入力

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

N A B
P Q R S

出力

Q-P+1 行出力せよ。
各行は #. のみからなる長さ S-R+1 の文字列であり、 i 行目の文字列の j 番目の文字が # であることは (P+i-1,R+j-1) が黒く塗られていることを、 . であることは (P+i-1,R+j-1) が白く塗られていることをさす。


入力例 1

5 3 2
1 5 1 5

出力例 1

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

1 つめの操作で (2,1), (3,2), (4,3), (5,4)4 マスが、 2 つめの操作で (4,1), (3,2), (2,3), (1,4)4 マスが黒く塗られます。
よって、P=1, Q=5, R=1, S=5 より、上のように出力します。


入力例 2

5 3 3
4 5 2 5

出力例 2

#.#.
...#

操作によって、 (1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5)9 マスが 黒く塗られます。
P=4, Q=5, R=2, S=5 より、上のように出力します。


入力例 3

1000000000000000000 999999999999999999 999999999999999999
999999999999999998 1000000000000000000 999999999999999998 1000000000000000000

出力例 3

#.#
.#.
#.#

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

Score : 300 points

Problem Statement

There is an N\times N grid with horizontal rows and vertical columns, where all squares are initially painted white. Let (i,j) denote the square at the i-th row and j-th column.

Takahashi has integers A and B, which are between 1 and N (inclusive). He will do the following operations.

  • For every integer k such that \max(1-A,1-B)\leq k\leq \min(N-A,N-B), paint (A+k,B+k) black.
  • For every integer k such that \max(1-A,B-N)\leq k\leq \min(N-A,B-1), paint (A+k,B-k) black.

In the grid after these operations, find the color of each square (i,j) such that P\leq i\leq Q and R\leq j\leq S.

Constraints

  • 1 \leq N \leq 10^{18}
  • 1 \leq A \leq N
  • 1 \leq B \leq N
  • 1 \leq P \leq Q \leq N
  • 1 \leq R \leq S \leq N
  • (Q-P+1)\times(S-R+1)\leq 3\times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B
P Q R S

Output

Print Q-P+1 lines.
Each line should contain a string of length S-R+1 consisting of # and .. The j-th character of the string in the i-th line should be # to represent that (P+i-1, R+j-1) is painted black, and . to represent that (P+i-1, R+j-1) is white.


Sample Input 1

5 3 2
1 5 1 5

Sample Output 1

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

The first operation paints the four squares (2,1), (3,2), (4,3), (5,4) black, and the second paints the four squares (4,1), (3,2), (2,3), (1,4) black.
Thus, the above output should be printed, since P=1, Q=5, R=1, S=5.


Sample Input 2

5 3 3
4 5 2 5

Sample Output 2

#.#.
...#

The operations paint the nine squares (1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5).
Thus, the above output should be printed, since P=4, Q=5, R=2, S=5.


Sample Input 3

1000000000000000000 999999999999999999 999999999999999999
999999999999999998 1000000000000000000 999999999999999998 1000000000000000000

Sample Output 3

#.#
.#.
#.#

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

F - Various Kagamimochi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 個の餅が小さいほうから順に並んでいます。 i 番目 (1\leq i\leq N) の餅の大きさは A _ i です。

2 つの餅 A,B の大きさをそれぞれ a,b としたとき、ab の半分以下であるとき、かつそのときに限り、餅 A を餅 B の上に乗せて鏡餅を 1 つ作ることができます。

N 個の餅から 2 つの餅を選び、一方をもう一方の上に乗せることで鏡餅を 1 つ作ります。

何種類の鏡餅を作ることができるか求めてください。

ただし、鏡餅を構成する餅の大きさが同じでも、少なくとも一方が異なる餅であれば別の種類の鏡餅として数えます。

制約

  • 2\leq N\leq 5\times 10 ^ 5
  • 1\leq A _ i\leq 10 ^ 9\ (1\leq i\leq N)
  • A _ i\leq A _ {i+1}\ (1\leq i\lt N)
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \cdots A _ N

出力

作ることができる鏡餅の種類数を出力せよ。


入力例 1

6
2 3 4 4 7 10

出力例 1

8

与えられた餅の大きさは以下のようになっています。

このとき、以下の 8 種類の鏡餅を作ることができます。

大きさ 4 の餅の上に大きさ 2 の餅を乗せた鏡餅や、大きさ 10 の餅の上に大きさ 4 の餅を乗せた鏡餅は 2 種類あることに注意してください。


入力例 2

3
387 388 389

出力例 2

0

鏡餅を 1 種類も作ることができない場合もあります。


入力例 3

32
1 2 4 5 8 10 12 16 19 25 33 40 50 64 87 101 149 175 202 211 278 314 355 405 412 420 442 481 512 582 600 641

出力例 3

388

Score : 300 points

Problem Statement

There are N mochi (rice cakes) arranged in ascending order of size. The size of the i-th mochi (1 \leq i \leq N) is A_i.

Given two mochi A and B, with sizes a and b respectively, you can make one kagamimochi (a stacked rice cake) by placing mochi A on top of mochi B if and only if a is at most half of b.

You choose two mochi out of the N mochi, and place one on top of the other to form one kagamimochi.

Find how many different kinds of kagamimochi can be made.

Two kagamimochi are distinguished if at least one of the mochi is different, even if the sizes of the mochi are the same.

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^9 \ (1 \leq i \leq N)
  • A_i \leq A_{i+1} \ (1 \leq i < N)
  • All input values are integers.

Input

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

N
A_1 A_2 \cdots A_N

Output

Print the number of different kinds of kagamimochi that can be made.


Sample Input 1

6
2 3 4 4 7 10

Sample Output 1

8

The sizes of the given mochi are as follows:

In this case, you can make the following eight kinds of kagamimochi:

Note that there are two kinds of kagamimochi where a mochi of size 4 is topped by a mochi of size 2, and two kinds where a mochi of size 10 is topped by a mochi of size 4.


Sample Input 2

3
387 388 389

Sample Output 2

0

It is possible that you cannot make any kagamimochi.


Sample Input 3

32
1 2 4 5 8 10 12 16 19 25 33 40 50 64 87 101 149 175 202 211 278 314 355 405 412 420 442 481 512 582 600 641

Sample Output 3

388
G - Least Unbalanced

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N を正整数とします。長さ 2^N の非負整数列 A=(A_1, A_2, \dots, A_{2^N})アンバランスさ を次の操作によって得られる非負整数値として定義します。

  • はじめ、X=0 とする。
  • 次の一連の操作を N 回行う。
    • X\max(X, \max(A) - \min(A)) に更新する。ここで \max(A) および \min(A) は数列 A の最大値と最小値を意味する。
    • 先頭から順に 2 つずつ要素を組にして、それらの和を並べることでできる長さが A の半分の数列を、新たな A とする。すなわち、A \gets (A_1 + A_2, A_3 + A_4, \dots, A_{\vert A \vert - 1} + A_{\vert A \vert}) とする。
  • 最終的な X をアンバランスさとする。

例えば N=2, A=(6, 8, 3, 5) は以下の手順によりアンバランスさが 6 であるとわかります。

  • はじめ、X=0 である。
  • 1 回目の一連の操作は次の通りである。
    • X\max(X, \max(A) - \min(A)) = \max(0, 8 - 3) = 5 に更新する。
    • A(6+8, 3+5) = (14, 8) とする。
  • 2 回目の一連の操作は次の通りである。
    • X\max(X, \max(A) - \min(A)) = \max(5, 14 - 8) = 6 に更新する。
    • A(14 + 8) = (22) とする。
  • 最終的に X=6 となる。

非負整数 K が与えられます。長さ 2^N の非負整数列であって総和が K であるものの中で、アンバランスさが最小値を取る数列を構成してください。

制約

  • 1 \leq N \leq 20
  • 0 \leq K \leq 10^9
  • N, K は整数

入力

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

N K

出力

アンバランスさが最小値を取る数列を B=(B_1,B_2,\dots,B_{2^N}) とする。B のアンバランスさを U とする。この時、以下の形式で答えを出力せよ。

U
B_1 B_2 \dots B_{2^N}

答えが複数ある場合は、どれを出力しても正答とみなされる。


入力例 1

1 11

出力例 1

1
5 6

(5, 6) はアンバランスさが 1 の数列で、これが条件を満たす数列のうちアンバランスさが最小の数列です。


入力例 2

3 56

出力例 2

0
7 7 7 7 7 7 7 7

Score : 400 points

Problem Statement

Let N be a positive integer. Define the imbalance of a sequence A=(A_1, A_2, \dots, A_{2^N}) of non-negative integers of length 2^N as the non-negative integer value obtained by the following operation:

  • Initially, set X=0.
  • Perform the following series of operations N times:
    • Update X to \max(X, \max(A) - \min(A)), where \max(A) and \min(A) denote the maximum and minimum values of sequence A, respectively.
    • Form a new sequence of half the length by pairing elements from the beginning two by two and arranging their sums. That is, set A \gets (A_1 + A_2, A_3 + A_4, \dots, A_{\vert A \vert - 1} + A_{\vert A \vert}).
  • The final value of X is the imbalance.

For example, when N=2, A=(6, 8, 3, 5), the imbalance is 6 through the following steps:

  • Initially, X=0.
  • The first series of operations is as follows:
    • Update X to \max(X, \max(A) - \min(A)) = \max(0, 8 - 3) = 5.
    • Set A to (6+8, 3+5) = (14, 8).
  • The second series of operations is as follows:
    • Update X to \max(X, \max(A) - \min(A)) = \max(5, 14 - 8) = 6.
    • Set A to (14 + 8) = (22).
  • Finally, X=6.

You are given a non-negative integer K. Among all sequences of non-negative integers of length 2^N with sum K, construct a sequence that minimizes the imbalance.

Constraints

  • 1 \leq N \leq 20
  • 0 \leq K \leq 10^9
  • N and K are integers.

Input

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

N K

Output

Let B=(B_1,B_2,\dots,B_{2^N}) be a sequence with minimum imbalance. Let U be the imbalance of B. Output a solution in the following format:

U
B_1 B_2 \dots B_{2^N}

If there are multiple solutions, any of them will be considered correct.


Sample Input 1

1 11

Sample Output 1

1
5 6

(5, 6) is a sequence with imbalance 1, which is the minimum imbalance among sequences satisfying the condition.


Sample Input 2

3 56

Sample Output 2

0
7 7 7 7 7 7 7 7
H - Replace

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

正整数 N および、英小文字からなる長さ N の文字列 S,T が与えられます。

以下の操作を繰り返し(0 回でも良い)行うことで、ST と一致させることが可能かどうか判定してください。 可能な場合は、そのために必要な操作回数の最小値も求めてください。

  • 英小文字 x,y を選び、S に含まれる 全てx をそれぞれ y で置き換える。

制約

  • 1\leq N \leq 2\times 10^5
  • N は整数
  • S,T はそれぞれ英小文字からなる長さ N の文字列

入力

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

N
S
T

出力

ST と一致させることが可能ならばそのために必要な操作回数の最小値を、不可能ならば -1 を出力せよ。


入力例 1

6
afbfda
bkckbb

出力例 1

4

以下のように操作を 4 回行うことで、ST と一致させることができます。

  1. x= b, y= c として操作を行う。S= afcfda となる。
  2. x= a, y= b として操作を行う。S= bfcfdb となる。
  3. x= f, y= k として操作を行う。S= bkckdb となる。
  4. x= d, y= b として操作を行う。S= bkckbb となり、T と一致する。

3 回以下の操作で ST と一致させることはできないため、必要な操作回数の最小値は 4 です。


入力例 2

4
abac
abac

出力例 2

0

ST が既に一致しているため、操作を行う必要はありません。


入力例 3

4
abac
abrc

出力例 3

-1

どのように操作を繰り返し行っても、ST と一致させることはできません。


入力例 4

4
abac
bcba

出力例 4

4

Score : 500 points

Problem Statement

You are given a positive integer N and two strings S and T, each of length N and consisting of lowercase English letters.

Determine whether it is possible to make S identical to T by repeating the operation below any number of times (possibly zero). If it is possible, also find the minimum number of operations required.

  • Choose two lowercase English letters x, y and replace every occurrence of x in S with y.

Constraints

  • 1\leq N \leq 2\times 10^5
  • N is an integer.
  • Each of S and T is a string of length N, consisting of lowercase English letters.

Input

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

N
S
T

Output

If it is possible to make S identical to T, print the minimum number of operations required. Otherwise, print -1.


Sample Input 1

6
afbfda
bkckbb

Sample Output 1

4

By performing the operation four times in the following way, you can make S identical to T:

  1. Choose x= b and y= c. S becomes afcfda.
  2. Choose x= a and y= b. S becomes bfcfdb.
  3. Choose x= f and y= k. S becomes bkckdb.
  4. Choose x= d and y= b. S becomes bkckbb, which is identical to T.

It cannot be done with fewer than four operations, so the minimum number of operations required is 4.


Sample Input 2

4
abac
abac

Sample Output 2

0

S and T are already identical, so no operations are required.


Sample Input 3

4
abac
abrc

Sample Output 3

-1

No matter how you repeat the operation, it is impossible to make S identical to T.


Sample Input 4

4
abac
bcba

Sample Output 4

4
I - ABCBAC

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

長さ N の文字列 S および整数 i\ (0\leq i\leq N) に対して、f_i(S) を、

  • S の先頭 i 文字
  • S を反転した文字列
  • S の末尾 N-i 文字

をこの順に連結した文字列と定義します。 例えば、S= abci=2 のとき、f_i(S)= abcbac です。

長さ 2N の文字列 T が与えられます。 f_i(S)=T を満たす長さ N の文字列 S と整数 i\ (0\leq i\leq N) の組を 1 つ見つけてください。 そのような S,i の組が存在しない場合は、それを報告してください。

制約

  • 1\leq N \leq 10^6
  • N は整数
  • T は英小文字からなる長さ 2N の文字列

入力

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

N 
T

出力

条件を満たす S,i の組が存在しないならば、-1 と出力せよ。 存在するならば、S,i を改行区切りで出力せよ。 条件を満たす S,i の組が複数存在する場合は、そのどれを出力しても良い。


入力例 1

3
abcbac

出力例 1

abc
2

問題文中に書いた通り、S= abci=2 とすると f_i(S)= abcbac となって T に一致するため、abc2 を出力します。


入力例 2

4
abababab

出力例 2

abab
1

S= ababi=3 としても条件を満たします。


入力例 3

3
agccga

出力例 3

cga
0

S= agci=3 としても条件を満たします。


入力例 4

4
atcodeer

出力例 4

-1

条件を満たす S,i の組が存在しない場合は -1 を出力してください。

Score : 500 points

Problem Statement

For a string S of length N and an integer i\ (0\leq i\leq N), let us define the string f_i(S) as the concatenation of:

  • the first i characters of S,
  • the reversal of S, and
  • the last (N-i) characters of S,

in this order. For instance, if S= abc and i=2, we have f_i(S)= abcbac.

You are given a string T of length 2N. Find a pair of a string S of length N and an integer i\ (0\leq i\leq N) such that f_i(S)=T. If no such pair of S and i exists, report that fact.

Constraints

  • 1\leq N \leq 10^6
  • N is an integer.
  • T is a string of length 2N consisting of lowercase English letters.

Input

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

N 
T

Output

If no pair of S and i satisfies the condition, print -1. Otherwise, print S and i, separated by a newline. If multiple pairs of S and i satisfy the condition, you may print any of them.


Sample Input 1

3
abcbac

Sample Output 1

abc
2

As mentioned in the problem statement, if S= abc and i=2, we have f_i(S)= abcbac, which equals T, so you should print abc and 2.


Sample Input 2

4
abababab

Sample Output 2

abab
1

S= abab and i=3 also satisfy the condition.


Sample Input 3

3
agccga

Sample Output 3

cga
0

S= agc and i=3 also satisfy the condition.


Sample Input 4

4
atcodeer

Sample Output 4

-1

If no pair of S and i satisfies the condition, print -1.