A - 484558

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

0123456789 に加えて 10,11,12,13,14,15 に対応する数字として ABCDEF を使う 16 進表記では、0 以上 255 以下の整数は 1 桁または 2 桁になります。
例えば、01216 進表記では 0C1 桁になり、9925516 進表記では 63FF2 桁になります。

0 以上 255 以下の整数 N を、必要に応じて先頭に 0 を加えることでちょうど 216 進表記に変換してください。

注記

英大文字と英小文字は区別されます。特に、16 進表記の数字として ABCDEF の代わりに abcdef を使うことは出来ません

制約

  • 0 \leq N \leq 255
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

99

出力例 1

63

9916 進表記で 63 です。


入力例 2

12

出力例 2

0C

1216 進表記で C です。
要求されているのはちょうど 2 桁の 16 進表記に変換することなので、C の先頭に 0 を加えた 0C が答えです。


入力例 3

0

出力例 3

00

入力例 4

255

出力例 4

FF

Score : 100 points

Problem Statement

In the hexadecimal system, where the digits ABCDEF corresponding to 10,11,12,13,14, and 15 are used in addition to 0123456789, every integer between 0 and 255 is represented as a 1- or 2-digit numeral.
For example, 0 and 12 are represented as 1-digit hexadecimal numerals 0 and C; 99 and 255 are represented as 2-digit hexadecimals 63 and FF.

Given an integer N between 0 and 255, convert it to an exactly two-digit hexadecimal numeral, prepending leading 0s if necessary.

Notes

The judge is case-sensitive. Specifically, you cannot use abcdef as hexadecimal digits instead of ABCDEF.

Constraints

  • 0 \leq N \leq 255
  • N is an integer.

Input

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

N

Output

Print the answer.


Sample Input 1

99

Sample Output 1

63

99 is represented as 63 in hexadecimal.


Sample Input 2

12

Sample Output 2

0C

12 is represented as C in hexadecimal.
Since we ask you to convert it to a two-digit hexadecimal numeral, the answer is 0C, where 0 is prepended to C.


Sample Input 3

0

Sample Output 3

00

Sample Input 4

255

Sample Output 4

FF
B - Maintain Multiple Sequences

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

整数からなる数列が N 個あります。
i \, (1 \leq i \leq N) 番目の数列は L_i 項からなり、i 番目の数列の第 j \, (1 \leq j \leq L_i) 項 は a_{i, j} です。

Q 個のクエリが与えられます。k \, (1 \leq k \leq Q) 番目のクエリでは、整数 s_k, t_k が与えられるので、s_k 番目の数列の第 t_k 項を求めてください。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • L_i \geq 1 \, (1 \leq i \leq N)
  • \sum_{i=1}^N L_i \leq 2 \times 10^5
  • 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
  • 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
  • 入力は全て整数

入力

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

N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots 
s_Q t_Q

出力

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


入力例 1

2 2
3 1 4 7
2 5 9
1 3
2 1

出力例 1

7
5

1 番目の数列は (1, 4, 7)2 番目の数列は (5, 9) です。
それぞれのクエリに対する答えは次のようになります。

  • 1 番目の数列の第 3 項は 7 です。
  • 2 番目の数列の第 1 項は 5 です。

入力例 2

3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4

出力例 2

128
1
26535
901

Score : 200 points

Problem Statement

There are N sequences of integers.
The i-th (1 \leq i \leq N) sequence has L_i terms; the j-th (1 \leq j \leq L_i) term of the i-th sequence is a_{i, j}.

You are given Q queries. For the k-th (1 \leq k \leq Q) query, given integers s_k and t_k, find the t_k-th term of the s_k-th sequence.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • L_i \geq 1 \, (1 \leq i \leq N)
  • \sum_{i=1}^N L_i \leq 2 \times 10^5
  • 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
  • 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
  • All values in the input are integers.

Input

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

N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots 
s_Q t_Q

Output

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


Sample Input 1

2 2
3 1 4 7
2 5 9
1 3
2 1

Sample Output 1

7
5

The 1-st sequence is (1, 4, 7) and the 2-nd is (5, 9).
The answer to each query is as follows:

  • The 3-rd term of the 1-st sequence is 7.
  • The 1-st term of the 2-nd sequence is 5.

Sample Input 2

3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4

Sample Output 2

128
1
26535
901
C - Manga

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 300

問題文

高橋君は全 10^9 巻の漫画『すぬけ君』を読むことにしました。
初め、高橋君は『すぬけ君』の単行本を N 冊持っています。i 番目の単行本は a_i 巻です。

高橋君は『すぬけ君』を読み始める前に限り次の操作を 0 回以上何度でも繰り返せます。

  • 現在持っている単行本が 1 冊以下の場合、何もしない。そうでない場合、現在持っている単行本から 2 冊を選んで売り、代わりに好きな巻を選んで 1 冊買う

その後、高橋君は『すぬけ君』を 1 巻、2 巻、3 巻、\ldots と順番に読みます。ただし、次に読むべき巻を持っていない状態になった場合、(他の巻を持っているかどうかに関わらず)その時点で『すぬけ君』を読むのをやめます。

高橋君が『すぬけ君』を最大で何巻まで読めるかを求めてください。ただし、1 巻を読めない場合の答えは 0 とします。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

6
1 2 4 6 7 271

出力例 1

4

『すぬけ君』を読み始める前に「7 巻と 271 巻を選んで売り、代わりに 3 巻を選んで 1 冊買う」という内容で操作をすると、高橋君は 1 巻、2 巻、3 巻、4 巻、6 巻を 1 冊ずつ持っている状態になります。
この状態から『すぬけ君』を読み始めると、高橋君は 1 巻、2 巻、3 巻、4 巻を読み、続いて 5 巻を読もうとしますが持っていないためこの時点で『すぬけ君』を読むのをやめます。
操作の方法を変えても 5 巻を読むことは出来ないため、4 が答えです。


入力例 2

10
1 1 1 1 1 1 1 1 1 1

出力例 2

5

高橋君は同じ巻を 2 冊以上持っているかもしれません。
この入力に対しては以下のように 4 回操作をしてから『すぬけ君』を読み始めることで 5 巻まで読むことが出来、これが最大です。

  • 1 巻を 2 冊選んで売り、代わりに 2 巻を選んで 1 冊買う
  • 1 巻を 2 冊選んで売り、代わりに 3 巻を選んで 1 冊買う
  • 1 巻を 2 冊選んで売り、代わりに 4 巻を選んで 1 冊買う
  • 1 巻を 2 冊選んで売り、代わりに 5 巻を選んで 1 冊買う

入力例 3

1
5

出力例 3

0

高橋君は 1 巻を読めません。

Score : 300 points

Problem Statement

Takahashi is going to read a manga series "Snuke-kun" in 10^9 volumes.
Initially, Takahashi has N books of this series. The i-th book is Volume a_i.

Takahashi may repeat the following operation any number of (possibly zero) times only before he begins to read:

  • Do nothing if he has 1 or less books; otherwise, sell two of the books he has and buy one book of any volume instead.

Then, Takahashi reads Volume 1, Volume 2, Volume 3, \ldots, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).

Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be 0.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq a_i \leq 10^9
  • All values in the input are integers.

Input

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

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

6
1 2 4 6 7 271

Sample Output 1

4

Before he begins to read the series, he may do the following operation: "sell books of Volumes 7 and 271 and buy one book of Volume 3 instead." Then, he has one book each of Volumes 1, 2, 3, 4, and 6.
If he then begins to read the series, he reads Volumes 1, 2, 3, and 4, then tries to read Volume 5. However, he does not have one, so he stops reading at this point.
Regardless of the choices in the operation, he cannot read Volume 5, so the answer is 4.


Sample Input 2

10
1 1 1 1 1 1 1 1 1 1

Sample Output 2

5

Takahashi may have multiple books of the same volume.
For this input, if he performs the following 4 operations before he begins to read the series, he can read up to Volume 5, which is the maximum:

  • Sell two books of Volume 1 and buy a book of Volume 2 instead.
  • Sell two books of Volume 1 and buy a book of Volume 3 instead.
  • Sell two books of Volume 1 and buy a book of Volume 4 instead.
  • Sell two books of Volume 1 and buy a book of Volume 5 instead.

Sample Input 3

1
5

Sample Output 3

0

Takahashi cannot read Volume 1.

D - Flip and Adjust

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

両面に整数が書かれたカードが N 枚あり、i \, (1 \leq i \leq N) 枚目のカードの表には a_i が、裏には b_i が書かれています。

あなたは、それぞれのカードについて、表を上に向けて置くか裏を上に向けて置くかを自由に決めることができます。

上に向けられた面に書かれた整数の総和がちょうど S となるようにカードを置くことができるか判定し、可能ならそのようなカードの置き方の一例を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq S \leq 10000
  • 1 \leq a_i, b_i \leq 100 \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N S
a_1 b_1
\vdots
a_N b_N

出力

まず、上に向けられた面に書かれた整数の総和がちょうど S となるようにカードを置くことができるならば Yes を、できなければ No を出力し、改行せよ。

さらに、そのようにカードを置くことができるならば、カードの置き方を表す H および T のみからなる長さ N の文字列を出力せよ。
この文字列の i \, (1 \leq i \leq N) 文字目は、i 枚目のカードを表を上に向けて置くなら H、裏を上に向けて置くなら T でなければならない。
条件を満たすカードの置き方が複数考えられる場合、どれを出力しても正解となる。


入力例 1

3 11
1 4
2 3
5 7

出力例 1

Yes
THH

例えば次のように置くことで、上に向けられた面に書かれた整数の総和がちょうど S (= 11) となります。

  • 1 枚目は表、2 枚目は裏、3 枚目は裏を上に向けて置く。
  • 1 枚目は裏、2 枚目は表、3 枚目は表を上に向けて置く。

よって、HTTTHH といった出力が正解となります。


入力例 2

5 25
2 8
9 3
4 11
5 1
12 6

出力例 2

No

上に向けられた面に書かれた整数の総和がちょうど S (= 25) となるようにカードを置くことはできません。

Score : 400 points

Problem Statement

There are N cards with an integer written on each side. Card i (1 \leq i \leq N) has an integer a_i written on the front and an integer b_i written on the back.

You may choose whether to place each card with its front or back side visible.

Determine if you can place the cards so that the sum of the visible integers exactly equals S. If possible, find a placement of the cards to realize it.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq S \leq 10000
  • 1 \leq a_i, b_i \leq 100 \, (1 \leq i \leq N)
  • All values in the input are integers.

Input

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

N S
a_1 b_1
\vdots
a_N b_N

Output

First, print Yes if you can make the sum of the visible integers exactly equal S, and No otherwise, followed by a newline.

Additionally, if such a placement is possible, print a string of length N consisting of H and T that represents the placement of the cards to realize it.
The i-th (1 \leq i \leq N) character should be H if the i-th card should be placed with its front side visible, and T with its back.
If there are multiple possible placements to realize the sum, printing any of them is accepted.


Sample Input 1

3 11
1 4
2 3
5 7

Sample Output 1

Yes
THH

For example, the following placements make the sum of the visible integers exactly equal S (= 11):

  • Place the 1-st card with its front side visible, 2-nd with its back, and 3-rd with its back.
  • Place the 1-st card with its back side visible, 2-nd with its front, and 3-rd with its front.

Therefore, outputs like HTT and THH are accepted.


Sample Input 2

5 25
2 8
9 3
4 11
5 1
12 6

Sample Output 2

No

You cannot place the cards so that the sum of the visible integers exactly equals S (= 25).

E - Subsequence Path

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1, \dots, N と番号づけられた N 個の都市と、1, \dots, M と番号づけられた M 本の道があります。
全ての道は一方通行であり、道 i \, (1 \leq i \leq M) を通ると、都市 A_i から都市 B_i へ移動することができます。また、道 i の長さは C_i です。

1 以上 M 以下の整数からなる長さ K の数列 E = (E_1, \dots, E_K) が与えられます。都市 1 から都市 N までいくつかの道を使って移動する方法であって、以下の条件を満たすものを良い経路と呼びます。

  • 通る道の番号を通った順番に並べた列は、E の部分列である。

なお、部分列とは、数列から 0 個以上の要素を削除し、残った要素を元の順序で並べて得られる数列のことを指します。

全ての良い経路における、通る道の長さの合計の最小値を求めてください。
ただし、良い経路が存在しない場合は、そのことを報告してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M, K \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
  • 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
  • 1 \leq E_i \leq M \, (1 \leq i \leq K)
  • 入力は全て整数

入力

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

N M K
A_1 B_1 C_1
\vdots
A_M B_M C_M
E_1 \ldots E_K

出力

全ての良い経路における、通る道の長さの合計の最小値を出力せよ。ただし、良い経路が存在しないならば、-1 を出力せよ。


入力例 1

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

出力例 1

4

良い経路として考えられるのは次の二つです。

  • 4 を使う。通る道の長さの合計は 5 である。
  • 1, 2 をこの順で使う。通る道の長さの合計は 2 + 2 = 4 である。

よって、求める最小値は 4 です。


入力例 2

3 2 3
1 2 1
2 3 1
2 1 1

出力例 2

-1

良い経路は存在しません。


入力例 3

4 4 5
3 2 2
1 3 5
2 4 7
3 4 10
2 4 1 4 3

出力例 3

14

Score : 500 points

Problem Statement

There are N towns numbered 1, \dots, N, and M roads numbered 1, \dots, M.
Every road is directed; road i (1 \leq i \leq M) leads you from Town A_i to Town B_i. The length of road i is C_i.

You are given a sequence E = (E_1, \dots, E_K) of length K consisting of integers between 1 and M. A way of traveling from town 1 to town N using roads is called a good path if:

  • the sequence of the roads' numbers arranged in the order used in the path is a subsequence of E.

Note that a subsequence of a sequence is a sequence obtained by removing 0 or more elements from the original sequence and concatenating the remaining elements without changing the order.

Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, report that fact.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M, K \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
  • 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
  • 1 \leq E_i \leq M \, (1 \leq i \leq K)
  • All values in the input are integers.

Input

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

N M K
A_1 B_1 C_1
\vdots
A_M B_M C_M
E_1 \ldots E_K

Output

Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, print -1.


Sample Input 1

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

Sample Output 1

4

There are two possible good paths as follows:

  • Using road 4. In this case, the sum of the length of the used road is 5.
  • Using road 1 and 2 in this order. In this case, the sum of the lengths of the used roads is 2 + 2 = 4.

Therefore, the desired minimum value is 4.


Sample Input 2

3 2 3
1 2 1
2 3 1
2 1 1

Sample Output 2

-1

There is no good path.


Sample Input 3

4 4 5
3 2 2
1 3 5
2 4 7
3 4 10
2 4 1 4 3

Sample Output 3

14
F - XOR on Grid Path

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

NN 列のマス目があり、上から i \, (1 \leq i \leq N) 行目、左から j \, (1 \leq j \leq N) 列目のマスを (i, j) と表します。
マス (i, j) には非負整数 a_{i, j} が書かれています。

マス (i, j) にいるとき、マス (i+1, j), (i, j+1) のいずれかに移動することができます。ただし、マス目の外に出るような移動はできません。

マス (1, 1) から移動を繰り返してマス (N, N) にたどり着く方法であって、通ったマス(マス (1, 1), (N, N) を含む)に書かれた整数の排他的論理和が 0 となるようなものの総数を求めてください。

排他的論理和とは 整数 a, b の排他的論理和 a \oplus b は、以下のように定義されます。
  • a \oplus b を二進表記した際の 2^k \, (k \geq 0) の位の数は、a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります(二進表記すると 011 \oplus 101 = 110)。
一般に k 個の整数 p_1, \dots, p_k の排他的論理和は (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k) と定義され、これは p_1, \dots, p_k の順番によらないことが証明できます。

制約

  • 2 \leq N \leq 20
  • 0 \leq a_{i, j} \lt 2^{30} \, (1 \leq i, j \leq N)
  • 入力は全て整数

入力

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

N
a_{1, 1} \ldots a_{1, N}
\vdots
a_{N, 1} \ldots a_{N, N}

出力

答えを出力せよ。


入力例 1

3
1 5 2
7 0 5
4 2 3

出力例 1

2

次の二通りの方法が条件を満たします。

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

入力例 2

2
1 2
2 1

出力例 2

0

入力例 3

10
1 0 1 0 0 1 0 0 0 1
0 0 0 1 0 1 0 1 1 0
1 0 0 0 1 0 1 0 0 0
0 1 0 0 0 1 1 0 0 1
0 0 1 1 0 1 1 0 1 0
1 0 0 0 1 0 0 1 1 0
1 1 1 0 0 0 1 1 0 0
0 1 1 0 0 1 1 0 1 0
1 0 1 1 0 0 0 0 0 0
1 0 1 1 0 0 1 1 1 0

出力例 3

24307

Score : 500 points

Problem Statement

There is a grid with N rows and N columns. We denote by (i, j) the square at the i-th (1 \leq i \leq N) row from the top and j-th (1 \leq j \leq N) column from the left.
Square (i, j) has a non-negative integer a_{i, j} written on it.

When you are at square (i, j), you can move to either square (i+1, j) or (i, j+1). Here, you are not allowed to go outside the grid.

Find the number of ways to travel from square (1, 1) to square (N, N) such that the exclusive logical sum of the integers written on the squares visited (including (1, 1) and (N, N)) is 0.

What is the exclusive logical sum? The exclusive logical sum a \oplus b of two integers a and b is defined as follows.
  • The 2^k's place (k \geq 0) in the binary notation of a \oplus b is 1 if exactly one of the 2^k's places in the binary notation of a and b is 1; otherwise, it is 0.
For example, 3 \oplus 5 = 6 (In binary notation: 011 \oplus 101 = 110).
In general, the exclusive logical sum of k integers p_1, \dots, p_k is defined as (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k). We can prove that it is independent of the order of p_1, \dots, p_k.

Constraints

  • 2 \leq N \leq 20
  • 0 \leq a_{i, j} \lt 2^{30} \, (1 \leq i, j \leq N)
  • All values in the input are integers.

Input

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

N
a_{1, 1} \ldots a_{1, N}
\vdots
a_{N, 1} \ldots a_{N, N}

Output

Print the answer.


Sample Input 1

3
1 5 2
7 0 5
4 2 3

Sample Output 1

2

The following two ways satisfy the condition:

  • (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3);
  • (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3).

Sample Input 2

2
1 2
2 1

Sample Output 2

0

Sample Input 3

10
1 0 1 0 0 1 0 0 0 1
0 0 0 1 0 1 0 1 1 0
1 0 0 0 1 0 1 0 0 0
0 1 0 0 0 1 1 0 0 1
0 0 1 1 0 1 1 0 1 0
1 0 0 0 1 0 0 1 1 0
1 1 1 0 0 0 1 1 0 0
0 1 1 0 0 1 1 0 1 0
1 0 1 1 0 0 0 0 0 0
1 0 1 1 0 0 1 1 1 0

Sample Output 3

24307
G - Access Counter

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君は自身が運営しているWebサイトにアクセスカウンターを設置することにしました。
彼のWebサイトに対して発生するアクセスの様子は以下のように記述されます。

  • i=0,1,2,\ldots,23 に対し、毎日 i 時ちょうどにアクセスが発生する可能性がある。
    • c_i=T の場合、高橋君が X パーセントの確率でアクセスする。
    • c_i=A の場合、青木君が Y パーセントの確率でアクセスする。
    • 高橋君や青木君がアクセスするかどうかは毎回独立に決まる。
  • これ以外のアクセスは発生しない。

また、高橋君はアクセスカウンターを設置してから N 回目のアクセスが自身によるものではない方が好ましいと考えています。

高橋君がアクセスカウンターを設置したのがある日の 0 時直前の時、設置してから N 回目のアクセスが青木君によるものになる確率を \mod 998244353 で求めてください。

注記

求める確率は必ず有限値かつ有理数となることが証明できます。また、この問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • 1 \leq X,Y \leq 99
  • c_iT または A
  • N,X,Y は整数

入力

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

N X Y
c_0 c_1 \ldots c_{23}

出力

答えを出力せよ。


入力例 1

1 50 50
ATATATATATATATATATATATAT

出力例 1

665496236

高橋君がアクセスカウンターを設置してから 1 回目のアクセスが青木君によるものになる確率は \frac{2}{3} です。


入力例 2

271 95 1
TTTTTTTTTTTTTTTTTTTTTTTT

出力例 2

0

青木君によるアクセスが存在しません。


入力例 3

10000000000000000 62 20
ATAATTATATTTAAAATATTATAT

出力例 3

744124544

Score : 600 points

Problem Statement

Takahashi has decided to put a web counter on his webpage.
The accesses to his webpage are described as follows:

  • For i=0,1,2,\ldots,23, there is a possible access at i o'clock every day:
    • If c_i=T, Takahashi accesses the webpage with a probability of X percent.
    • If c_i=A, Aoki accesses the webpage with a probability of Y percent.
    • Whether or not Takahashi or Aoki accesses the webpage is determined independently every time.
  • There is no other access.

Also, Takahashi believes it is preferable that the N-th access since the counter is put is not made by Takahashi himself.

If Takahashi puts the counter right before 0 o'clock of one day, find the probability, modulo 998244353, that the N-th access is made by Aoki.

Notes

We can prove that the sought probability is always a finite rational number. Moreover, under the constraints of this problem, when the value is represented as \frac{P}{Q} with two coprime integers P and Q, we can prove that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

  • 1 \leq N \leq 10^{18}
  • 1 \leq X,Y \leq 99
  • c_i is T or A.
  • N, X, and Y are integers.

Input

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

N X Y
c_0 c_1 \ldots c_{23}

Output

Print the answer.


Sample Input 1

1 50 50
ATATATATATATATATATATATAT

Sample Output 1

665496236

The 1-st access since Takahashi puts the web counter is made by Aoki with a probability of \frac{2}{3}.


Sample Input 2

271 95 1
TTTTTTTTTTTTTTTTTTTTTTTT

Sample Output 2

0

There is no access by Aoki.


Sample Input 3

10000000000000000 62 20
ATAATTATATTTAAAATATTATAT

Sample Output 3

744124544
Ex - General General

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

T 個のテストケースについて以下の問題を解いてください。

xy 座標平面上の原点 (0,0) に駒が置かれています。あなたは次の操作を 0 回以上何度でも行うことが出来ます。

  • 1 \leq i \leq 8 かつ s_i= 1 を満たす整数 i を選ぶ。現在駒が置かれている座標を (x,y) とした時、
    • i=1 ならば駒を (x+1,y) に移動させる。
    • i=2 ならば駒を (x+1,y+1) に移動させる。
    • i=3 ならば駒を (x,y+1) に移動させる。
    • i=4 ならば駒を (x-1,y+1) に移動させる。
    • i=5 ならば駒を (x-1,y) に移動させる。
    • i=6 ならば駒を (x-1,y-1) に移動させる。
    • i=7 ならば駒を (x,y-1) に移動させる。
    • i=8 ならば駒を (x+1,y-1) に移動させる。

あなたの目的は駒を (A,B) に移動させることです。
目的を達成するために必要な操作回数の最小値を求めてください。ただし、目的を達成することが不可能な場合は代わりに -1 を出力してください。

制約

  • 1 \leq T \leq 10^4
  • -10^9 \leq A,B \leq 10^9
  • s_i0 または 1
  • T,A,B は整数

入力

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

ただし、\mathrm{case}_ii 番目のテストケースを表す。

各テストケースは以下の形式で与えられる。

A B s_1 s_2 s_3 s_4 s_5 s_6 s_7 s_8

出力

全体で T 行出力せよ。
i 行目には i 番目のテストケースに対する答えを出力せよ。


入力例 1

7
5 3 10101010
5 3 01010101
5 3 11111111
5 3 00000000
0 0 11111111
0 1 10001111
-1000000000 1000000000 10010011

出力例 1

8
5
5
-1
0
-1
1000000000

Score : 600 points

Problem Statement

Solve the following problem for T test cases.

A piece is placed at the origin (0, 0) on an xy-plane. You may perform the following operation any number of (possibly zero) times:

  • Choose an integer i such that 1 \leq i \leq 8 and s_i= 1. Let (x, y) be the current coordinates where the piece is placed.
    • If i=1, move the piece to (x+1,y).
    • If i=2, move the piece to (x+1,y+1).
    • If i=3, move the piece to (x,y+1).
    • If i=4, move the piece to (x-1,y+1).
    • If i=5, move the piece to (x-1,y).
    • If i=6, move the piece to (x-1,y-1).
    • If i=7, move the piece to (x,y-1).
    • If i=8, move the piece to (x+1,y-1).

Your objective is to move the piece to (A, B).
Find the minimum number of operations needed to achieve the objective. If it is impossible, print -1 instead.

Constraints

  • 1 \leq T \leq 10^4
  • -10^9 \leq A,B \leq 10^9
  • s_i is 0 or 1.
  • T, A, and B are integers.

Input

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Here, \mathrm{case}_i denotes the i-th test case.

Each test case is given in the following format:

A B s_1 s_2 s_3 s_4 s_5 s_6 s_7 s_8

Output

Print T lines in total.
The i-th line should contain the answer to the i-th test case.


Sample Input 1

7
5 3 10101010
5 3 01010101
5 3 11111111
5 3 00000000
0 0 11111111
0 1 10001111
-1000000000 1000000000 10010011

Sample Output 1

8
5
5
-1
0
-1
1000000000