A - Chocolate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder さんはバレンタインデーの日に N 人の友達にチョコレートを配ることにしました。i 人目 (1 \leq i \leq N) の友達には、大きさ 2^{A_i} \times 2^{A_i} の正方形の板チョコを渡したいです。

ここで、AtCoder さんは大きさ H \times W の長方形の板チョコを 1 枚仕入れました。この板チョコは切れ目によって縦 H 行・横 W 列のマス目状に区切られており、その各マスは大きさ 1 \times 1 の正方形になっています。

板チョコを切れ目に沿っていくつかのピースに分割することにより、友達に渡す板チョコをすべて得ることが可能か判定してください。なお、余るピースがあっても構いません。

制約

  • 1 \leq H \leq 10^9
  • 1 \leq W \leq 10^9
  • 1 \leq N \leq 1000
  • 0 \leq A_i \leq 25 \ (1 \leq i \leq N)
  • 入力はすべて整数

入力

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

H W N
A_1 A_2 \cdots A_N

出力

可能ならば Yes、不可能ならば No と出力してください。


入力例 1

4 4 4
1 0 0 1

出力例 1

Yes

以下の図のように大きさ 4 \times 4 の板チョコを分割することで、大きさ 2 \times 2, 1 \times 1, 1 \times 1, 2 \times 2 のピースを得ることができます。


入力例 2

5 7 6
0 1 0 2 0 1

出力例 2

Yes

以下の図のように大きさ 5 \times 7 の板チョコを分割することで、大きさ 1 \times 1, 2 \times 2, 1 \times 1, 4 \times 4, 1 \times 1, 2 \times 2 のピースを得ることができます。


入力例 3

3 2 7
0 0 0 0 0 0 0

出力例 3

No

大きさ 3 \times 2 の板チョコから、大きさ 1 \times 1 のピースを 7 つ得ることは不可能です。


入力例 4

11 11 2
2 3

出力例 4

No

大きさ 11 \times 11 の板チョコから、大きさ 4 \times 4, 8 \times 8 のピースを両方得ることは不可能です。


入力例 5

777 777 6
8 6 9 1 2 0

出力例 5

Yes

Score: 500 points

Problem Statement

Ms. AtCoder has decided to distribute chocolates to N friends on Valentine's Day. For the i-th friend (1 \leq i \leq N), she wants to give a square chocolate bar of size 2^{A_i} \times 2^{A_i}.

She has procured a rectangular chocolate bar of size H \times W. It is partitioned by lines into a grid of H rows and W columns, each cell being a 1 \times 1 square.

Determine whether it is possible to divide the chocolate bar along the lines into several pieces to obtain all the chocolate bars for her friends. It is fine to have leftover pieces.

Constraints

  • 1 \leq H \leq 10^9
  • 1 \leq W \leq 10^9
  • 1 \leq N \leq 1000
  • 0 \leq A_i \leq 25 \ (1 \leq i \leq N)
  • All input values are integers.

Input

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

H W N
A_1 A_2 \cdots A_N

Output

If the objective is achievable, print Yes; otherwise, print No.


Sample Input 1

4 4 4
1 0 0 1

Sample Output 1

Yes

By dividing a 4 \times 4 chocolate bar as shown in the figure below, you can obtain pieces of size 2 \times 2, 1 \times 1, 1 \times 1, 2 \times 2.


Sample Input 2

5 7 6
0 1 0 2 0 1

Sample Output 2

Yes

By dividing a 5 \times 7 chocolate bar as shown in the figure below, you can obtain pieces of size 1 \times 1, 2 \times 2, 1 \times 1, 4 \times 4, 1 \times 1, 2 \times 2.


Sample Input 3

3 2 7
0 0 0 0 0 0 0

Sample Output 3

No

It is impossible to obtain seven pieces of size 1 \times 1 from a 3 \times 2 chocolate bar.


Sample Input 4

11 11 2
2 3

Sample Output 4

No

It is impossible to obtain both a 4 \times 4 and an 8 \times 8 piece from an 11 \times 11 chocolate bar.


Sample Input 5

777 777 6
8 6 9 1 2 0

Sample Output 5

Yes
B - AtCoder Language

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder 語には L 種類の文字があります。 AtCoder 語の文字からなる N 文字の文字列 s のうち、以下の条件を満たすものは何通りありますか。 答えを 998244353 で割った余りを求めてください。

  • 文字列 s のどの「K 文字の部分列」も異なる。厳密には、文字列 s から K 文字を抜き出し、そのままの順序で連結して K 文字の文字列を得る方法は _N\mathrm{C}_K 通りあるが、それらすべてが異なる文字列を生成する。
_N\mathrm{C}_K とはN 個のものの中から K 個を選ぶ方法の総数を指します。より厳密には、_N\mathrm{C}_KN!K! \times (N-K)! で割った値です。


制約

  • 1 \leq K < N \leq 500000
  • 1 \leq L \leq 10^9
  • 入力はすべて整数

入力

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

N K L

出力

答えを出力してください。


入力例 1

4 3 2

出力例 1

2

AtCoder 語の 1 種類目の文字を a2 種類目の文字を b と表すとき、条件を満たす文字列は ababbaba2 通りとなります。


入力例 2

100 80 26

出力例 2

496798269

条件を満たす文字列はおよそ 10^{86} 通りありますが、ここでは 998244353 で割った余りである 496798269 を出力します。


入力例 3

100 1 26

出力例 3

0

入力例 4

500000 172172 503746693

出力例 4

869120

Score: 500 points

Problem Statement

The AtCoder language has L different characters. How many N-character strings s consisting of characters in the AtCoder language satisfy the following condition? Find the count modulo 998244353.

  • All K-character subsequences of the string s are different. More precisely, there are _N\mathrm{C}_K ways to obtain a K-character string by extracting K characters from the string s and concatenating them without changing the order, and all of these ways must generate different strings.
What is _N\mathrm{C}_K?_N\mathrm{C}_K refers to the total number of ways to choose K from N items. More precisely, _N\mathrm{C}_K is the value of N! divided by K! \times (N-K)!.


Constraints

  • 1 \leq K < N \leq 500000
  • 1 \leq L \leq 10^9
  • All input values are integers.

Input

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

N K L

Output

Print the count modulo 998244353.


Sample Input 1

4 3 2

Sample Output 1

2

If a and b represent the first and second characters in the language, the condition is satisfied by two strings: abab and baba.


Sample Input 2

100 80 26

Sample Output 2

496798269

Approximately 10^{86} strings satisfy the condition, but here we print the count modulo 998244353, which is 496798269.


Sample Input 3

100 1 26

Sample Output 3

0

Sample Input 4

500000 172172 503746693

Sample Output 4

869120
C - Election

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

今年の AtCoder 市長選挙には A 候補と B 候補の 2 人が立候補し、N 人の有権者が投票しました。 投票者にはそれぞれ 1 から N までの番号が付けられており、投票者 i (1 \leq i \leq N)c_i 候補に投票しました。

さて、これから開票作業が行われます。 開票作業では 1 票ずつ票が開けられていき、票が開けられるたびに、現時点での開票結果が以下の 3 つのうちどれであるかが発表されます。

  • 結果 A: 現時点で、A 候補の方が獲得票数が多い。
  • 結果 B: 現時点で、B 候補の方が獲得票数が多い。
  • 結果 C: 現時点で、A 候補と B 候補の獲得票数が同数である。

ここで開票の順番にはルールがあり、投票者 1 以外の票は、投票者の番号の小さい順に開票されなければなりません。 (投票者 1 の票は好きなタイミングで開票してかまいません)

発表される開票結果の列としてあり得るものが何通りあるかを答えてください。

開票結果の列とはi 票目 (1 \leq i \leq N) が開けられたタイミングで報告された結果を s_i (A, B, C のいずれか) とするとき,文字列 s_1 s_2 \dots s_N のことを指します。


制約

  • N2 \leq N \leq 1000000 を満たす整数
  • c_1, c_2, \dots, c_NA または B

入力

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

N
c_1 c_2 \cdots c_N

出力

答えを出力してください。


入力例 1

4
AABB

出力例 1

3

この入力例では、開票が行われる順番として以下の 4 通りが考えられます。

  • 投票者 1 \to 2 \to 3 \to 4 の順に開票が行われる。
  • 投票者 2 \to 1 \to 3 \to 4 の順に開票が行われる。
  • 投票者 2 \to 3 \to 1 \to 4 の順に開票が行われる。
  • 投票者 2 \to 3 \to 4 \to 1 の順に開票が行われる。

開票結果の列は上から順に AAACAAACACACACBC となるため、開票結果の列としてあり得るものは 3 通りです。


入力例 2

4
AAAA

出力例 2

1

どのような順序で開票を行っても、開票結果の列は AAAA となります。


入力例 3

10
BBBAAABBAA

出力例 3

5

入力例 4

172
AABAAAAAABBABAABBBBAABBAAABBABBABABABBAAABAAABAABAABBBBABBBABBABBBBBBBBAAABAAABAAABABBBAABAAAABABBABBABBBBBABAABAABBBABABBAAAABAABABBBABAAAABBBBABBBABBBABAABBBAAAABAAABAAAB

出力例 4

24

Score: 600 points

Problem Statement

This year's AtCoder mayoral election has two candidates, A and B, and N voters have cast their votes. The voters are assigned numbers from 1 to N, and voter i (1 \leq i \leq N) voted for candidate c_i.

Now, the votes will be counted. As each vote is counted, the provisional result will be announced as one of the following three outcomes:

  • Result A: Currently, candidate A has more votes.
  • Result B: Currently, candidate B has more votes.
  • Result C: Currently, candidates A and B have the same number of votes.

There is a rule regarding the order of vote counting: votes from all voters except voter 1 must be counted in ascending order of their voter numbers. (The vote from voter 1 may be counted at any time.)

How many different sequences of provisional results could be announced?

What is a sequence of provisional results?Let s_i be the result (A, B, or C) reported when the i-th vote (1 \leq i \leq N) is counted. The sequence of provisional results refers to the string s_1 s_2 \dots s_N.


Constraints

  • N is an integer such that 2 \leq N \leq 1000000.
  • Each of c_1, c_2, \dots, c_N is A or B.

Input

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

N
c_1 c_2 \cdots c_N

Output

Print the answer.


Sample Input 1

4
AABB

Sample Output 1

3

In this sample input, there are four possible orders in which the votes may be counted:

  • The votes are counted in the order of voter 1 \to 2 \to 3 \to 4.
  • The votes are counted in the order of voter 2 \to 1 \to 3 \to 4.
  • The votes are counted in the order of voter 2 \to 3 \to 1 \to 4.
  • The votes are counted in the order of voter 2 \to 3 \to 4 \to 1.

The sequences of preliminary results for these will be AAAC, AAAC, ACAC, ACBC from top to bottom, so there are three possible sequences of preliminary results.


Sample Input 2

4
AAAA

Sample Output 2

1

No matter the order of counting, the sequence of preliminary results will be AAAA.


Sample Input 3

10
BBBAAABBAA

Sample Output 3

5

Sample Input 4

172
AABAAAAAABBABAABBBBAABBAAABBABBABABABBAAABAAABAABAABBBBABBBABBABBBBBBBBAAABAAABAAABABBBAABAAAABABBABBABBBBBABAABAABBBABABBAAAABAABABBBABAAAABBBBABBBABBBABAABBBAAAABAAABAAAB

Sample Output 4

24
D - Distance Ranking

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

N 次元空間上に N 個の点 p_1, p_2, \dots, p_N を以下の条件を満たすように配置してください。

条件 1 点の座標は 0 以上 10^8 以下の整数で構成される。

条件 2 入力で指定された (A_1, B_1), (A_2, B_2), \dots, (A_{N(N-1)/2}, B_{N(N-1)/2}) について、d(p_{A_1}, p_{B_1}) < d(p_{A_2}, p_{B_2}) < \dots < d(p_{A_{N(N-1)/2}}, p_{B_{N(N-1)/2}}) を満たす。ここで、d(x, y) は点 x, y のユークリッド距離を示す。

なお、本問題の制約下では答えが存在することが証明できます。また、答えが複数通りある場合でも、そのうち 1 つを出力すればよいです。

ユークリッド距離とは n 次元空間上の点 x, y のユークリッド距離は、x の座標を (x_1, x_2, \dots, x_n)y の座標を (y_1, y_2, \dots, y_n) として、\sqrt{(x_1-y_1)^2 + (x_2-y_2)^2 + \dots + (x_n-y_n)^2} と計算されます。

制約

  • 3 \leq N \leq 20
  • 1 \leq A_i < B_i \leq N \ (1 \leq i \leq \frac{N(N-1)}{2})
  • (A_1, B_1), (A_2, B_2), \dots, (A_{N(N-1)/2}, B_{N(N-1)/2}) はすべて異なる

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N(N-1)/2} B_{N(N-1)/2}

出力

p_i (1 \leq i \leq N) の座標を (p_{i, 1}, p_{i, 2}, \dots, p_{i, N}) とするとき、以下の形式で出力してください。

p_{1, 1} p_{1, 2} \cdots p_{1, N}
p_{2, 1} p_{2, 2} \cdots p_{2, N}
\vdots
p_{N, 1} p_{N, 2} \cdots p_{N, N}

入力例 1

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

出力例 1

3 2 0 0
9 1 0 0
1 8 0 0
9 8 0 0

この出力例では座標の第 3、第 4 の成分がすべて 0 なので、以下の 2 次元の図で表すことができます。

d(p_1, p_2) = \sqrt{37}, d(p_1, p_3) = \sqrt{40}, d(p_2, p_4) = \sqrt{49}, d(p_3, p_4) = \sqrt{64}, d(p_1, p_4) = \sqrt{72}, d(p_2, p_3) = \sqrt{113} であり、正しい順番になっています。

Score: 700 points

Problem Statement

Place N points p_1, p_2, \dots, p_N in an N-dimensional space to satisfy the following conditions:

Condition 1 The coordinates of the points consist of integers between 0 and 10^8, inclusive.

Condition 2 For (A_1, B_1), (A_2, B_2), \dots, (A_{N(N-1)/2}, B_{N(N-1)/2}) specified as input, d(p_{A_1}, p_{B_1}) < d(p_{A_2}, p_{B_2}) < \dots < d(p_{A_{N(N-1)/2}}, p_{B_{N(N-1)/2}}) must hold. Here, d(x, y) denotes the Euclidean distance between points x and y.

It can be proved that a solution exists under the constraints of the problem. If multiple solutions exist, just print one of them.

What is Euclidean distance? The Euclidean distance between points x and y in an n-dimensional space, with coordinates (x_1, x_2, \dots, x_n) for x and (y_1, y_2, \dots, y_n) for y, is calculated as \sqrt{(x_1-y_1)^2 + (x_2-y_2)^2 + \dots + (x_n-y_n)^2}.

Constraints

  • 3 \leq N \leq 20
  • 1 \leq A_i < B_i \leq N \ (1 \leq i \leq \frac{N(N-1)}{2})
  • All pairs (A_1, B_1), (A_2, B_2), \dots, (A_{N(N-1)/2}, B_{N(N-1)/2}) are distinct.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N(N-1)/2} B_{N(N-1)/2}

Output

Let (p_{i, 1}, p_{i, 2}, \dots, p_{i, N}) be the coordinates of point p_i. Print your solution in the following format:

p_{1, 1} p_{1, 2} \cdots p_{1, N}
p_{2, 1} p_{2, 2} \cdots p_{2, N}
\vdots
p_{N, 1} p_{N, 2} \cdots p_{N, N}

Sample Input 1

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

Sample Output 1

3 2 0 0
9 1 0 0
1 8 0 0
9 8 0 0

In this sample output, the third and fourth components of the coordinates are all zero, so the solution can be shown in the two-dimensional figure below.

d(p_1, p_2) = \sqrt{37}, d(p_1, p_3) = \sqrt{40}, d(p_2, p_4) = \sqrt{49}, d(p_3, p_4) = \sqrt{64}, d(p_1, p_4) = \sqrt{72}, d(p_2, p_3) = \sqrt{113}, and they are in the correct order.

E - Last 9 Digits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

n^n10^9 で割った余りが X となるような正の整数 n が存在するか判定し、存在する場合は最小のものを求めてください。

Q 個のテストケースが与えられるので、それぞれに対して答えてください。

制約

  • 1 \leq Q \leq 10000
  • 1 \leq X \leq 10^9 - 1
  • X2 の倍数でも 5 の倍数でもない
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられます。ただし、i 個目 (1 \leq i \leq Q) のテストケースにおける X の値を X_i とします。

Q
X_1
X_2
\vdots
X_Q

出力

Q 行にわたって出力してください。i 行目には、i 個目のテストケースに対する答えを出力してください。ただし、条件を満たす n が存在しない場合は答えを -1 とします。


入力例 1

2
27
311670611

出力例 1

3
11

この入力例は 2 個のテストケースからなります。

  • 1 個目:3^3 = 2710^9 で割った余りは 27 なので、n = 3 で条件を満たします。
  • 2 個目:11^{11} = 28531167061110^9 で割った余りは 311670611 なので、n = 11 で条件を満たします。

Score: 800 points

Problem Statement

Determine whether there is a positive integer n such that the remainder of n^n divided by 10^9 is X. If it exists, find the smallest such n.

You will be given Q test cases to solve.

Constraints

  • 1 \leq Q \leq 10000
  • 1 \leq X \leq 10^9 - 1
  • X is neither a multiple of 2 nor a multiple of 5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where X_i is the value of X in the i-th test case (1 \leq i \leq Q):

Q
X_1
X_2
\vdots
X_Q

Output

Print Q lines. The i-th line should contain the answer for the i-th test case. If no n satisfies the condition, the answer should be -1.


Sample Input 1

2
27
311670611

Sample Output 1

3
11

This sample input consists of two test cases.

  • The first case: The remainder of 3^3 = 27 divided by 10^9 is 27, so n = 3 satisfies the condition.
  • The second case: The remainder of 11^{11} = 285311670611 divided by 10^9 is 311670611, so n = 11 satisfies the condition.
F - Walking

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

AtCoder 王国には 2N 個の交差点があり、各交差点には 1 から 2N までの番号が付けられています。 また、AtCoder 王国には以下の 3 種類の一方通行の道路があります。

  • タイプ A: 2 \leq i \leq N について、交差点 2i-3 から交差点 2i-1 へ向かう道路
  • タイプ B: 2 \leq i \leq N について、交差点 2i-2 から交差点 2i へ向かう道路
  • タイプ C: 1 \leq i \leq N について、交差点 2i-1 と交差点 2i を結ぶ向き s_i の道路

ここで、s_iX または Y であり、向き X は交差点 2i-1 から交差点 2i に向かう方向、向き Y は交差点 2i から交差点 2i-1 に向かう方向を指します。

高橋君はウォーキングを何回か行うことで、各 1 \leq i \leq N について、交差点 2i-1 と交差点 2i を結ぶタイプ C の道路の向きを t_i にしたいです。

ウォーキングとは

好きな交差点から出発し、以下の行動を 0 回以上繰り返す。

  • もし現在いる交差点からタイプ C の道路に進めるのであれば、タイプ C の道路を次の交差点まで歩く。
  • 上記以外で、現在いる交差点からタイプ A または B の道路に進めるのであれば、その道路を次の交差点まで歩く。

その後、通ったすべてのタイプ C の道路の向きを反転させる。

最小何回のウォーキングで目的を達成できるかを計算し、最小回数で目的を達成する方法を 1 つ答えてください。 なお、この問題の制約下では有限回で目的を達成できることが証明できます。

制約

  • N1 \leq N \leq 4000 を満たす整数
  • s_1, s_2, \dots, s_NX または Y
  • t_1, t_2, \dots, t_NX または Y

入力

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

N
s_1 s_2 \cdots s_N
t_1 t_2 \cdots t_N

出力

以下の形式で出力してください。 ただし、ウォーキングの回数を X とします。また、i 回目 (1 \leq i \leq X) のウォーキングでは交差点 a_i から出発し、交差点 b_i でウォーキングを終えるものとします。

X
a_1 b_1
a_2 b_2
\vdots
a_X b_X

入力例 1

5
XYXYX
XXYXX

出力例 1

1
2 7

この入力例では、高橋君は以下のようなウォーキングを行います。

  • 交差点 2 から出発する。
  • 交差点 2 から進めるタイプ C の道路はないので、タイプ B の道路を通って交差点 4 まで進む。
  • 交差点 4 から進めるタイプ C の道路はあるので、タイプ C の道路を通って交差点 3 まで進む。
  • 交差点 3 から進めるタイプ C の道路はないので、タイプ A の道路を通って交差点 5 まで進む。
  • 交差点 5 から進めるタイプ C の道路はあるので、タイプ C の道路を通って交差点 6 まで進む。
  • 交差点 6 から進めるタイプ C の道路はないので、タイプ B の道路を通って交差点 8 まで進む。
  • 交差点 8 から進めるタイプ C の道路はあるので、タイプ C の道路を通って交差点 7 まで進む。
  • 交差点 7 でウォーキングを終える。

ここで、このウォーキングでは、以下の 3 本のタイプ C の道路を通っています。

  • 交差点 3 と交差点 4 を結ぶ道路
  • 交差点 5 と交差点 6 を結ぶ道路
  • 交差点 7 と交差点 8 を結ぶ道路

これらの道路の向きが反転するため、i = 1, 2, 3, 4, 5 について交差点 2i-1 と交差点 2i を結ぶ道路の向きは順に XXYXX となり、目的を達成します。


入力例 2

5
XXYYX
XXYYX

出力例 2

0

最初の時点で目的が達成されているため、1 回もウォーキングを行う必要がないことに注意してください。


入力例 3

5
XXXXX
YYYYY

出力例 3

5
1 2
3 4
5 6
7 8
9 10

入力例 4

20
XXXYXYYXXXYXXXXYYXXY
XXYXYYXXYXXYXYXYXYXY

出力例 4

5
14 18
29 38
14 26
5 10
27 35

入力例 5

20
YXYXYXYYYXYYXYYXYXXX
XXXXXYXYYYXYYXXYYYXY

出力例 5

5
29 36
10 38
2 3
4 7
28 40

Score: 1000 points

Problem Statement

The Kingdom of AtCoder has 2N intersections labeled with numbers from 1 to 2N. Additionally, there are three types of one-way roads in the kingdom:

  • Type A: For each 2 \leq i \leq N, there is a road from intersection 2i-3 to intersection 2i-1.
  • Type B: For each 2 \leq i \leq N, there is a road from intersection 2i-2 to intersection 2i.
  • Type C: For each 1 \leq i \leq N, there is a road connecting intersection 2i-1 and intersection 2i with direction s_i.

Here, s_i is X or Y, where direction X means the road goes from intersection 2i-1 to intersection 2i, and direction Y means it goes from intersection 2i to intersection 2i-1.

Takahashi wants to walk several times so that for each 1 \leq i \leq N, the direction of Type-C road connecting intersections 2i-1 and 2i will be t_i.

What is a walk?

Start at any intersection and repeat the following action zero or more times:

  • If it is possible to proceed to a Type-C road from the current intersection, walk along the Type-C road to the next intersection.
  • Otherwise, if it is possible to proceed to a Type-A or B road from the current intersection, walk along that road to the next intersection.

Then, reverse the directions of all Type-C roads that were passed.

Calculate the minimum number of walks needed to achieve the goal and provide one method to achieve the goal in the minimum number of walks. Under the constraints of this problem, it can be proved that the goal can be achieved in a finite number of walks.

Constraints

  • N is an integer such that 1 \leq N \leq 4000.
  • Each of s_1, s_2, \dots, s_N is X or Y.
  • Each of t_1, t_2, \dots, t_N is X or Y.

Input

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

N
s_1 s_2 \cdots s_N
t_1 t_2 \cdots t_N

Output

Print your solution in the following format, where X is the number of walks, and the i-th walk (1 \leq i \leq X) starts at intersection a_i and finishes at intersection b_i.

X
a_1 b_1
a_2 b_2
\vdots
a_X b_X

Sample Input 1

5
XYXYX
XXYXX

Sample Output 1

1
2 7

In this sample input, Takahashi will walk as follows:

  • Start at intersection 2.
  • Since there is no Type-C road to proceed from intersection 2, walk along the Type-B road to intersection 4.
  • Since there is a Type-C road to proceed from intersection 4, walk along the Type-C road to intersection 3.
  • Since there is no Type-C road to proceed from intersection 3, walk along the Type-A road to intersection 5.
  • Since there is a Type-C road to proceed from intersection 5, walk along the Type-C road to intersection 6.
  • Since there is no Type-C road to proceed from intersection 6, walk along the Type-B road to intersection 8.
  • Since there is a Type-C road to proceed from intersection 8, walk along the Type-C road to intersection 7.
  • Finish at intersection 7.

This walk passes through the following three Type-C roads:

  • The road connecting intersection 3 and intersection 4.
  • The road connecting intersection 5 and intersection 6.
  • The road connecting intersection 7 and intersection 8.

The directions of these roads are reversed, so the directions of the roads connecting intersections 2i-1 and 2i for i = 1, 2, 3, 4, 5 are now X, X, Y, X, X, respectively, achieving the goal.


Sample Input 2

5
XXYYX
XXYYX

Sample Output 2

0

Note that the goal is already achieved at the beginning, so there is no need to walk even once.


Sample Input 3

5
XXXXX
YYYYY

Sample Output 3

5
1 2
3 4
5 6
7 8
9 10

Sample Input 4

20
XXXYXYYXXXYXXXXYYXXY
XXYXYYXXYXXYXYXYXYXY

Sample Output 4

5
14 18
29 38
14 26
5 10
27 35

Sample Input 5

20
YXYXYXYYYXYYXYYXYXXX
XXXXXYXYYYXYYXXYYYXY

Sample Output 5

5
29 36
10 38
2 3
4 7
28 40