A - Two Lucky Numbers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

AtCoder さんは新聞で、今日のラッキーナンバーが正の整数 A で、明日のラッキーナンバーが正の整数 B であることを知りました。

ここで、次の条件を両方とも満たす正の整数 x を「超ラッキーな数」ということにしました。

  • x を十進法で書いたときに、連続する部分文字列として A が現れる
  • 2x を十進法で書いたときに、連続する部分文字列として B が現れる

実は、本問題の制約の範囲内では、10^{18} 未満の超ラッキーな数が必ず存在します。これを 1 つ探してみてください。

制約

  • 1 \leq A < 10^8
  • 1 \leq B < 10^8
  • A, B の先頭に余分な 0 は現れない
  • 入力はすべて整数

入力

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

A
B

出力

10^{18} 未満の超ラッキーな数を 1 つ出力してください。ただし、答えが複数通りあり得る場合は、そのうちどれを出力しても構いません。


入力例 1

13
62

出力例 1

131

例えば x = 131 は超ラッキーな数です。なぜなら、

  • x = 131 の部分文字列として 13 が現れる(12 文字目)
  • 2x = 262 の部分文字列として 62 が現れる(23 文字目)

からです。

それ以外にも、例えば 3138135135797531 などが超ラッキーな数であり、これらを出力しても正解になります。


入力例 2

69120
824

出力例 2

869120

例えば x = 869120 は超ラッキーな数です。なぜなら、

  • x = 869120 の部分文字列として 69120 が現れる(26 文字目)
  • 2x = 1738240 の部分文字列として 824 が現れる(46 文字目)

からです。

最小の超ラッキーな数は 69120 ですが、18 桁以下の超ラッキーな数ならどれを出力してもよいことにご注意ください。


入力例 3

6283185
12566370

出力例 3

6283185

x = 6283185 のとき、xA が、2xB がそのまま現れます。このようなときも、x は超ラッキーな数になります。

Score : 300 points

Problem Statement

Mr. AtCoder reads in the newspaper that today's lucky number is a positive integer A and tomorrow's is a positive integer B.

Here, he defines a positive integer x that satisfies both of the following conditions as a super-lucky number.

  • The decimal notation of x contains A as a contiguous substring.
  • The decimal notation of 2x contains B as a contiguous substring.

Actually, under the Constraints of this problem, there is always a super-lucky number less than 10^{18}. Find one such number.

Constraints

  • 1 \leq A < 10^8
  • 1 \leq B < 10^8
  • A and B have no leading 0s.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print one super-lucky number less than 10^{18}. If multiple solutions exist, you may print any of them.


Sample Input 1

13
62

Sample Output 1

131

One super-lucky number is x = 131, because:

  • x = 131 contains 13 as a substring. (1-st through 2-nd characters)
  • 2x = 262 contains 62 as a substring. (2-nd through 3-rd characters)

Some other super-lucky numbers are 313, 8135, and 135797531, which would also be accepted.


Sample Input 2

69120
824

Sample Output 2

869120

One super-lucky number is x = 869120, because:

  • x = 869120 contains 69120 as a substring. (2-nd through 6-th characters)
  • 2x = 1738240 contains 824 as a substring. (4-th through 6-th characters)

The smallest super-lucky number is 69120, but note that any lucky number with at most 18 digits would be accepted.


Sample Input 3

6283185
12566370

Sample Output 3

6283185

When x = 6283185, x is A itself, and 2x is B itself. In such a case too, x is a super-lucky number.

B - Grid Repainting 4

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

H \times W のマス目で表されるキャンバスがあり、上から i (1 \leq i \leq H) 行目、左から j (1 \leq j \leq W) 列目のマスを (i, j) と表します。

最初、マス (i, j) の状態は以下のようになっています。

  • c_{i, j}=1 のとき:色 1 で塗られている
  • c_{i, j}=2 のとき:色 2 で塗られている
  • c_{i, j}=3 のとき:色 3 で塗られている
  • c_{i, j}=4 のとき:色 4 で塗られている
  • c_{i, j}=5 のとき:色 5 で塗られている
  • c_{i, j}=. のとき:まだ塗られていない

上下左右に隣り合うマスが同じ色にならないように、まだ塗られていないマスを色 1, 2, 3, 4, 5 のいずれかで塗る方法を 1 つ構成してください。ただし、既に塗られたマスを別の色で塗り替えることはできません。

制約

  • 1 \leq H, W \leq 700
  • c_{i, j}12345. のいずれか
  • まだ塗られていないマスが 1 つ以上存在する
  • 条件を満たす塗り方は必ず 1 つ以上存在する

入力

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

H W
c_{1, 1}c_{1, 2}\ldotsc_{1, W}
c_{2, 1}c_{2, 2}\ldotsc_{2, W}
 :
c_{H, 1}c_{H, 2}\ldotsc_{H, W}

出力

マスの塗り方を以下の形式で出力してください。

ただし、d_{i, j} はすべてのマスを塗り終わった後のマス (i, j) の色とします。(12345 のいずれかでなければなりません)

d_{1, 1}d_{1, 2}\ldotsd_{1, W}
d_{2, 1}d_{2, 2}\ldotsd_{2, W}
 :
d_{H, 1}d_{H, 2}\ldotsd_{H, W}

条件を満たす塗り方が複数存在する場合、そのうちどれを出力しても構いません。


入力例 1

3 3
...
...
...

出力例 1

132
313
541

出力例 1 は、以下の塗り方に対応しています。


入力例 2

5 7
1.2.3.4
.5.1.2.
3.4.5.1
.2.3.4.
5.1.2.3

出力例 2

1425314
2531425
3142531
4253142
5314253

出力例 2 は、以下の塗り方に対応しています。


入力例 3

1 1
.

出力例 3

4

Score : 300 points

Problem Statement

We have a canvas represented as a H \times W grid. Let (i, j) denote the square at the i-th row (1 \leq i \leq H) from the top and j-th column (1 \leq j \leq W) from the left.

Initially, (i, j) is in the following state.

  • If c_{i, j}=1: painted in Color 1.
  • If c_{i, j}=2: painted in Color 2.
  • If c_{i, j}=3: painted in Color 3.
  • If c_{i, j}=4: painted in Color 4.
  • If c_{i, j}=5: painted in Color 5.
  • If c_{i, j}=.: not yet painted.

Create a way to paint each unpainted square in Color 1, 2, 3, 4, or 5 so that no two horizontally or vertically adjacent squares have the same color. It is not allowed to change the color of a square that is already painted.

Constraints

  • 1 \leq H, W \leq 700
  • c_{i, j} is 1, 2, 3, 4, 5, or ..
  • At least one square is unpainted.
  • There is at least one way to paint the squares under the condition.

Input

Input is given from Standard Input in the following format:

H W
c_{1, 1}c_{1, 2}\ldotsc_{1, W}
c_{2, 1}c_{2, 2}\ldotsc_{2, W}
 :
c_{H, 1}c_{H, 2}\ldotsc_{H, W}

Output

Print a way to paint the squares in the following format. Here, d_{i, j} should be the color of (i, j) after painting the squares (it should be 1, 2, 3, 4, or 5).

d_{1, 1}d_{1, 2}\ldotsd_{1, W}
d_{2, 1}d_{2, 2}\ldotsd_{2, W}
 :
d_{H, 1}d_{H, 2}\ldotsd_{H, W}

If there are multiple ways to paint the squares under the condition, you may print any of them.


Sample Input 1

3 3
...
...
...

Sample Output 1

132
313
541

Sample Output 1 corresponds to the following coloring.


Sample Input 2

5 7
1.2.3.4
.5.1.2.
3.4.5.1
.2.3.4.
5.1.2.3

Sample Output 2

1425314
2531425
3142531
4253142
5314253

Sample Output 2 corresponds to the following coloring.


Sample Input 3

1 1
.

Sample Output 3

4
C - Zero XOR

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

机の上に N 枚のクッキーがあります。クッキーの表面にはそれぞれ正の整数 A_1, A_2, \dots, A_N が書かれており、これらはすべて異なります。

このクッキーを使って 2 人でゲームを行います。このゲームでは、各プレイヤーは次の行動を交互に行います。

机にあるクッキーを 1 枚選んで食べる。
その際に、机に残ったクッキーに書かれた整数の \mathrm{XOR}0 になったならば、そのプレイヤーは勝利し、ゲームは終了する。

あなたは E869120 君に対戦を申し込みました。あなたは先手で、E869120 君は後手です。さて、両者が最適に行動したときに、あなたは E869120 君に勝ちますか?

\mathrm{XOR} とは

整数 A, B のビット単位 XOR、A\ \mathrm{XOR}\ B は、以下のように定義されます。

  • A\ \mathrm{XOR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{XOR}\ 5 = 6 となります (二進表記すると: 011\ \mathrm{XOR}\ 101 = 110)。
一般に、k 個の整数 p_1, p_2, p_3, \dots, p_k のビット単位 XOR は (\dots ((p_1\ \mathrm{XOR}\ p_2)\ \mathrm{XOR}\ p_3)\ \mathrm{XOR}\ \dots\ \mathrm{XOR}\ p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。特に k = 0 の場合、\mathrm{XOR}0 となります。

制約

  • 1 \leq N \leq 400000
  • 1 \leq A_i \leq 10^9 \ (1 \leq i \leq N)
  • A_1, A_2, \dots, A_N はすべて異なる
  • A_1, A_2, \dots, A_N\mathrm{XOR}0 ではない
  • 入力はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

両者が最適に行動したときにあなたが勝つなら Win、負けるなら Lose と出力してください。


入力例 1

6
9 14 11 3 5 8

出力例 1

Lose

この例では、あなたがどんな方法を使っても、E869120 君が最適に行動し続ければ負けてしまいます。

例えば、最初に 11 が書かれたクッキーを食べるとしましょう。すると、次に E869120 君が 9 が書かれたクッキーを食べることで、残ったクッキーに書かれた数 14, 3, 5, 8\mathrm{XOR}0 になるので、E869120 君が勝ちます。

それ以外の行動をとっても、最終的には E869120 君が勝ちます。


入力例 2

1
131

出力例 2

Win

この例では、あなたは最初のターンで 131 が書かれたクッキーを食べることしかできません。すると、机の上からクッキーがなくなるので、残ったクッキーに書かれた数の \mathrm{XOR}0 になります。したがって、E869120 君が何もできないまま、あなたが勝ちます。


入力例 3

8
12 23 34 45 56 78 89 98

出力例 3

Win

Score : 600 points

Problem Statement

There are N cookies on a desk. Each cookie has a positive integer on its surface: A_1, A_2, \dots, A_N, which are all different.

Let us play a game with two players using these cookies. In this game, the players alternately do the following action.

Choose a cookie on the desk and eat it. Then, if the \mathrm{XOR} of the integers written on the remaining cookies on the desk becomes 0, the player wins, and the game ends.

You have asked the boy E869120 to play this game with you. You go first, and E869120 goes second. Are you going to win if both players play optimally?

What is \mathrm{XOR}?

The bitwise \mathrm{XOR} of integers A and B, A\ \mathrm{XOR}\ B, is defined as follows:

  • When A\ \mathrm{XOR}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
For example, we have 3\ \mathrm{XOR}\ 5 = 6 (in base two: 011\ \mathrm{XOR}\ 101 = 110).
Generally, the bitwise \mathrm{XOR} of k integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1\ \mathrm{XOR}\ p_2)\ \mathrm{XOR}\ p_3)\ \mathrm{XOR}\ \dots\ \mathrm{XOR}\ p_k). We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots p_k. Particularly, when k = 0, the \mathrm{XOR} is 0.

Constraints

  • 1 \leq N \leq 400000
  • 1 \leq A_i \leq 10^9 \ (1 \leq i \leq N)
  • A_1, A_2, \dots, A_N are all different.
  • The \mathrm{XOR} of A_1, A_2, \dots, A_N is not 0.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

If you are going to win if both players play optimally, print Win; if you are going to lose, print Lose.


Sample Input 1

6
9 14 11 3 5 8

Sample Output 1

Lose

In this case, regardless of what choices you make, you will lose if E869120 keeps playing optimally.

For example, let us say you first eat the cookie with 11 written on it. Then, E869120 will eat the cookie with 9 so that the \mathrm{XOR} of the numbers written on the remaining cookies, 14, 3, 5, 8, is 0, making him win.

Even if you make other choices, E869120 will win eventually.


Sample Input 2

1
131

Sample Output 2

Win

In this case, your only choice in your first turn is to eat the cookie with 131. Then, there is no more cookie on the desk, making the \mathrm{XOR} of the numbers written on the remaining cookies 0. Therefore, before E869120 gets to do anything, you win.


Sample Input 3

8
12 23 34 45 56 78 89 98

Sample Output 3

Win
D - AtArcher

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

りんごさんはアーチェリーの大会「AtArcher」に出場しました。

AtArcher では、数直線上に表される的に N 本の矢を撃って合計得点を競います。的の中心は座標 0 であり、矢が当たった位置に応じて以下のように得点が定められています。

  • i = 0, 1, \dots, M-1 に対して、中心からの距離が r_i から r_{i+1} までの場所に当てると s_i 点を獲得し、中心からの距離が r_M より大きい場所に当てると 0 点を獲得する。境界に当たった場合は高い方の得点になる。
  • 中心から近いほど高得点が得られるようになっている。すなわち、次を満たす。
    • 0 = r_0 \lt r_1 \lt \cdots \lt r_{M-1} \lt r_M
    • s_0 \gt s_1 \gt \cdots \gt s_{M-1} \gt 0

例えば、r = (0, 2, 7, 9), s = (100, 70, 30) の場合、得点は下図のようになります。

さらに、AtArcher では「どの 2 本の矢も距離 D 以上の間隔を空ける」という特殊ルールがあります。これに違反した場合は失格となり、全体の得点が 0 点になります。

さて、りんごさんが全ての矢を撃ち終わった時点で、最大何点獲得できるでしょう?

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq D \leq 10^6
  • 0 = r_0 \lt r_1 \lt \cdots \lt r_{M-1} \lt r_M \leq 10^{11}
  • 10^{11} \geq s_0 \gt s_1 \gt \cdots \gt s_{M-1} \gt 0
  • 入力はすべて整数

入力

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

N M D
r_0 r_1 \cdots r_{M-1} r_M
s_0 s_1 \cdots s_{M-1}

出力

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


入力例 1

3 3 3
0 2 7 9
100 70 30

出力例 1

270

この入力例は問題文中の例に対応していますが、D = 3 となっています。

例えば、N = 3 本の矢が座標 -6, -2, 1 に当たると、それぞれ 70, 100, 100 点を獲得します。このとき合計得点は 270 点となり、実現可能なものとしては最大です。

なお、すべての矢を 100 点のエリアに当てて 300 点を取ることはできません。なぜなら、どの 2 本の矢も距離 D = 3 以上の間隔を空けなければ、失格で 0 点になるからです。


入力例 2

3 3 8
0 2 7 9
100 70 30

出力例 2

200

この入力例も問題文中の例に対応していますが、D = 8 となっています。

例えば、N = 3 本の矢が座標 -7, 1, 9 に当たると、それぞれ 70, 100, 30 点を獲得します。このとき合計得点は 200 点となり、実現可能なものとしては最大です。


入力例 3

7 5 47
0 10 40 100 160 220
50 25 9 6 3

出力例 3

111

例えば、下図のように矢を当てると、合計得点は 111 点となり、これが最大です。


入力例 4

100 1 5
0 7
100000000000

出力例 4

300000000000

N = 100 本の矢を当てることができますが、失格にならないためには、得点が入るゾーンに 3 本までしか入れることができません。


入力例 5

15 10 85
0 122 244 366 488 610 732 854 976 1098 1220
10 9 8 7 6 5 4 3 2 1

出力例 5

119

Score : 600 points

Problem Statement

Ringo is participating in an archery contest AtArcher.

In AtArcher, a participant shoots N arrows at a target on a number line to compete for the total score. The center of the target is at coordinate 0. Based on where the arrow hits, the score of the shot is defined as follows.

  • For i = 0, 1, \dots, M-1, if the arrow hits a point whose distance from the center of the target is between r_i and r_{i+1}, the score is s_i. If the distance is greater than r_M, the score is 0. If the arrow hits the boundary, the higher score is applied.
  • The closer to the center, the higher the score is. In other words, the following is satisfied.
    • 0 = r_0 \lt r_1 \lt \cdots \lt r_{M-1} \lt r_M
    • s_0 \gt s_1 \gt \cdots \gt s_{M-1} \gt 0

For example, the figure below shows the score for a shot when r = (0, 2, 7, 9), s = (100, 70, 30).

Additionally, AtArcher has one special rule: the distance between any two arrows must always be at least D. Violating this rule disqualifies the participant, making the total score 0.

What is the maximum total score that Ringo can get from all the shots?

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq D \leq 10^6
  • 0 = r_0 \lt r_1 \lt \cdots \lt r_{M-1} \lt r_M \leq 10^{11}
  • 10^{11} \geq s_0 \gt s_1 \gt \cdots \gt s_{M-1} \gt 0
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M D
r_0 r_1 \cdots r_{M-1} r_M
s_0 s_1 \cdots s_{M-1}

Output

Print the answer.


Sample Input 1

3 3 3
0 2 7 9
100 70 30

Sample Output 1

270

This sample input corresponds to the case in the Problem Statement, with D = 3.

For example, if the N = 3 arrows hit the coordinates -6, -2, 1, they score 70, 100, 100, respectively, for a total score of 270, which is the maximum achievable.

Note that you cannot hit the 100-point area with all the arrows to score 300, because the distance between any two arrows must be at least D = 3, or you will be disqualified and score 0.


Sample Input 2

3 3 8
0 2 7 9
100 70 30

Sample Output 2

200

This sample input corresponds to the case in the Problem Statement, with D = 8.

For example, if the N = 3 arrows hit the coordinates -7, 1, 9, they score 70, 100, 30, respectively, for a total score of 200, which is the maximum achievable.


Sample Input 3

7 5 47
0 10 40 100 160 220
50 25 9 6 3

Sample Output 3

111

For example, if you shoot the arrows as shown in the following figure, you will score 111 in total, which is the maximum.


Sample Input 4

100 1 5
0 7
100000000000

Sample Output 4

300000000000

You can shoot N = 100 arrows, but in order to avoid disqualification, at most 3 arrows can hit the area with a positive score.


Sample Input 5

15 10 85
0 122 244 366 488 610 732 854 976 1098 1220
10 9 8 7 6 5 4 3 2 1

Sample Output 5

119
E - Christmas Wreath

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君は、N 個のボールと \frac{N(N-1)}{2} 個のロープからなるクリスマス飾りを持っています。ボールには 1 から N までの番号が付けられており、どの 2 つの相異なるボールについても、ちょうど 1 つのロープで結ばれています。

彼は、それぞれのロープを赤・青・白のいずれかの色で点灯させることにしました。

見栄えを良くするため、以下の条件をすべて満たすようにしたいです。

条件1 赤・青・白で点灯されているロープの数は、すべて同数である。

条件2 整数 a, b, c (1 \leq a < b < c \leq N) であって、以下の 3 つのロープの色がすべて異なるものは存在しない。

  • ボール ab を結ぶロープ
  • ボール bc を結ぶロープ
  • ボール ac を結ぶロープ

条件を満たす点灯の方法を 1 つ構成してください。このような方法が存在しない場合、その旨を出力してください。

制約

  • 3 \leq N \leq 50
  • N は整数

入力

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

N

出力

条件を満たす点灯の方法が存在しない場合は、No と出力してください。

存在する場合は、以下の形式で出力してください。

Yes
c_{1,2}c_{1,3}c_{1,4}\ldotsc_{1,N}
c_{2,3}c_{2,4}\ldotsc_{2,N}
 :
c_{N-1,N}

ただし、文字 c_{i, j} (1 \leq i < j \leq N) は以下の通りにしてください。

  • ボール ij を結ぶロープを赤にするとき:c_{i, j} = R
  • ボール ij を結ぶロープを青にするとき:c_{i, j} = B
  • ボール ij を結ぶロープを白にするとき:c_{i, j} = W

入力例 1

4

出力例 1

No

N=4 では、条件を満たす点灯の方法が存在しないため、No と出力すれば正解となります。

Yes のときの出力の一例も以下に示しておきますが、このケースでは不正解となります。これは、条件2(a, b, c) = (1, 2, 3) を選ぶと、ボール a, b を結ぶロープは赤、ボール b, c を結ぶロープは白、ボール a, c を結ぶロープは青と、すべて異なるからです。

Yes
RBW
WB
R

Score : 600 points

Problem Statement

Takahashi has Christmas decoration consisting of N balls and \frac{N(N-1)}{2} ropes. The balls are numbered 1 to N, and for any two different balls, there is exactly one rope that connects them.

He decides to light up each rope in red, blue, or white.

For better appearance, he wants to satisfy all of the following conditions.

Condition 1 the numbers of ropes lighted in red, blue, and white are all equal.

Condition 2 there is no triple of integers a, b, c (1 \leq a < b < c \leq N) such that all of the following three ropes have different colors:

  • the rope connecting a and b,
  • the rope connecting b and c,
  • the rope connecting a and c.

Create a way to light up the ropes to satisfy the conditions. If there is no such way, report so.

Constraints

  • 3 \leq N \leq 50
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If there is no way to light up the ropes to satisfy the conditions, print No.

If such a way exists, print it in the following format:

Yes
c_{1,2}c_{1,3}c_{1,4}\ldotsc_{1,N}
c_{2,3}c_{2,4}\ldotsc_{2,N}
 :
c_{N-1,N}

Here, the character c_{i, j} (1 \leq i < j \leq N) should be the following:

  • c_{i, j} = R when lighting up the rope connecting Balls i and j red,
  • c_{i, j} = B when lighting up the rope connecting Balls i and j blue,
  • c_{i, j} = W when lighting up the rope connecting Balls i and j white.

Sample Input 1

4

Sample Output 1

No

For N=4, there is no way to light up the ropes to satisfy the conditions, so the output No is correct.

Below is an example of an output in the Yes case, which is incorrect in this case. This is because, for (a, b, c) = (1, 2, 3) in Condition 2, the rope connecting a, b is red, the rope connecting b, c is white, and the rope connecting a, c is blue, all of which have different colors.

Yes
RBW
WB
R
F - ARC Stamp

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

A, R, C からなる文字列 S から始めて、「連続する三文字を選んで ARC に上書きする」操作を K 回以下行ったところ、文字列 T が得られました。

さて、最初の文字列 S として何通りがあり得るでしょうか?これを 998244353 で割った余りを求めてください。

制約

  • 3 \leq |T| \leq 5000
  • 0 \leq K \leq 10000
  • TA, R, C からなる文字列

入力

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

T
K

出力

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


入力例 1

ARCCARC
1

出力例 1

53

例えば、最初の文字列 S が次のようなとき、1 回以下の操作で文字列 T = ARCCARC を得ることができます。

  • S = ARCCARC のとき:操作を行わないでも文字列 ARCCARC が得られる.
  • S = CRACARC のとき:S1, 2, 3 文字目を選んで ARC に上書きすると、文字列 ARCCARC が得られる。
  • S = ARCCCCC のとき:S5, 6, 7 文字目を選んで ARC に上書きすると、文字列 ARCCARC が得られる。

上に挙げたもの以外にもたくさんあり、S としてあり得るものは全部で 53 通りです。


入力例 2

ARARCRCA
5

出力例 2

2187

最初の文字列 SAAAAAAAA のとき、例えば次のような 5 回以下の操作で文字列 T = ARARCRCA を得ることができます。

  • ステップ 1:まず、3, 4, 5 文字目を選んで ARC に上書きすると、文字列 AAARCAAA が得られる。
  • ステップ 2:次に、5, 6, 7 文字目を選んで ARC に上書きすると、文字列 AAARARCA が得られる。
  • ステップ 3:次に、1, 2, 3 文字目を選んで ARC に上書きすると、文字列 ARCRARCA が得られる。
  • ステップ 4:最後、3, 4, 5 文字目を選んで ARC に上書きすると、文字列 ARARCRCA が得られる。

それ以外にも S としてあり得るものはたくさんあり、全部で 2187 通りです。


入力例 3

AARCRRARCC
0

出力例 3

1

0 回の操作で文字列 T = AARCRRARCC を得られるのは、最初の時点で S = T のとき、すなわち S = AARCRRARCC1 通りしかありません。


入力例 4

AAAAARRRRRCCCCC
131

出力例 4

1

この入力例では、S としてあり得るものは AAAAARRRRRCCCCC1 通りだけです。


入力例 5

CAARCACRAAARARARCRCRARCARARCRRARC
9

出力例 5

797833187

最初の文字列 S としてあり得るものは 320236026147 通りあるので、これを 998244353 で割った余りである 797833187 を出力してください。

Score : 1000 points

Problem Statement

On a string S consisting of A, R, and C, we did the following operation at most K times: choose three consecutive characters and overwrite them with ARC. The result is the string T.

How many strings are there that could be the initial string S? Find this count modulo 998244353.

Constraints

  • 3 \leq |T| \leq 5000
  • 0 \leq K \leq 10000
  • T is a string consisting of A, R, and C.

Input

Input is given from Standard Input in the following format:

T
K

Output

Print the answer.


Sample Input 1

ARCCARC
1

Sample Output 1

53

Below are some examples of the initial string S from which we can get the string T = ARCCARC with at most 1 operation.

  • S = ARCCARC: we can choose to do nothing to get ARCCARC.
  • S = CRACARC: we can choose the 1-st, 2-nd, 3-rd characters and overwrite them with ARC to get ARCCARC.
  • S = ARCCCCC: we can choose the 5-th, 6-th, 7-th characters and overwrite them with ARC to get ARCCARC.

There are many other strings that could be S, for a total of 53.


Sample Input 2

ARARCRCA
5

Sample Output 2

2187

If the intial string S is AAAAAAAA, one way to get T = ARARCRCA with at most 5 operations is as follows.

  • Step 1: choose the 3-rd, 4-th, 5-th characters and overwrite them with ARC to get the string AAARCAAA.
  • Step 2: choose the 5-th, 6-th, 7-th characters and overwrite them with ARC to get the string AAARARCA.
  • Step 3: choose the 1-st, 2-nd, 3-rd characters and overwrite them with ARC to get the string ARCRARCA.
  • Step 4: choose the 3-rd, 4-th, 5-th characters and overwrite them with ARC to get the string ARARCRCA.

There are many other strings that could be S, for a total of 2187.


Sample Input 3

AARCRRARCC
0

Sample Output 3

1

We can get T = AARCRRARCC with 0 operations in only one case in which S = T from the beginning, or S = AARCRRARCC.


Sample Input 4

AAAAARRRRRCCCCC
131

Sample Output 4

1

In this case, there is just one string that could be S: AAAAARRRRRCCCCC.


Sample Input 5

CAARCACRAAARARARCRCRARCARARCRRARC
9

Sample Output 5

797833187

There are 320236026147 strings that could be S, so print this count modulo 998244353, which is 797833187.