A - Shampoo

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

配点 : 100

問題文

高橋君の家には、高橋君、高橋君の父、高橋君の母の 3 人が住んでおり、全員が毎晩風呂で髪を洗います。
風呂には、高橋君の父、高橋君の母、高橋君の順に入り、それぞれシャンプーを A,B,C ミリリットル使います。

今朝の時点で、ボトルには V ミリリットルのシャンプーが残っていました。このまま補充しない時、初めてシャンプーが不足するのは誰が使おうとした時ですか?

制約

  • 1 \leq V,A,B,C \leq 10^5
  • 入力に含まれる値は全て整数である

入力

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

V A B C

出力

初めてシャンプーが不足するのが、高橋君の父が使おうとしたときならば F、高橋君の母が使おうとしたときならば M、高橋君が使おうとしたときならば T を出力せよ。


入力例 1

25 10 11 12

出力例 1

T

シャンプーは 25 ミリリットル残っています。

  • まず高橋君の父が 10 ミリリットル使い、残りは 15 ミリリットルになります。
  • 次に高橋君の母が 11 ミリリットル使い、残りは 4 ミリリットルになります。
  • 最後に高橋君が 12 ミリリットル使おうとしますが、4 ミリリットルしか残っておらず、不足しています。

入力例 2

30 10 10 10

出力例 2

F

シャンプーは 30 ミリリットル残っています。

  • まず高橋君の父が 10 ミリリットル使い、残りは 20 ミリリットルになります。
  • 次に高橋君の母が 10 ミリリットル使い、残りは 10 ミリリットルになります。
  • 続いて高橋君が 10 ミリリットル使い、残りは 0 ミリリットルになります。
  • 翌日、高橋君の父が 10 ミリリットル使おうとしますが、0 ミリリットルしか残っておらず、不足しています。

入力例 3

100000 1 1 1

出力例 3

M

Score : 100 points

Problem Statement

Three people live in Takahashi's house: Takahashi, his father, and his mother. All of them wash their hair in the bathroom each night.
His father, his mother, and Takahashi take a bath in this order and use A, B, and C milliliters of shampoo, respectively.

This morning, the bottle contained V milliliters of shampoo. Without refilling, who will be the first to run short of shampoo to wash their hair?

Constraints

  • 1 \leq V,A,B,C \leq 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

V A B C

Output

If the first person to run short of shampoo to wash their hair is Takahashi's father, print F; if it is Takahashi's mother, print M; if it is Takahashi, print T.


Sample Input 1

25 10 11 12

Sample Output 1

T

Now, they have 25 milliliters of shampoo.

  • First, Takahashi's father uses 10 milliliters, leaving 15.
  • Next, Takahashi's mother uses 11 milliliters, leaving 4.
  • Finally, Takahashi tries to use 12 milliliters and runs short of shampoo since only 4 is remaining.

Sample Input 2

30 10 10 10

Sample Output 2

F

Now, they have 30 milliliters of shampoo.

  • First, Takahashi's father uses 10 milliliters, leaving 20.
  • Next, Takahashi's mother uses 10 milliliters, leaving 10.
  • Then, Takahashi uses 10 milliliters, leaving 0.
  • Next day, Takahashi's father tries to use 10 milliliters and runs short of shampoo since only 0 is remaining.

Sample Input 3

100000 1 1 1

Sample Output 3

M
B - Hit and Blow

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

配点 : 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
C - Collision 2

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

配点 : 300

問題文

xy 座標平面上に N 人の人がいます。人 i(X_i, Y_i) にいます。すべての人は異なる地点にいます。

L, R からなる長さ N の文字列 S があります。
iS_i = R ならば右向きに、S_i = L ならば左向きに、一斉に同じ速度で歩き始めます。ここで、右は x 軸の正の向き、左は x 軸の負の向きです。

たとえば (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL の場合は次の図のように動きます。

image

反対の向きに歩いている人同士が同じ地点に来ることを「衝突」と呼びます。すべての人が歩き続けたとき、衝突は発生しますか?

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j) である。
  • X_i, Y_i はすべて整数である。
  • SL および R からなる長さ N の文字列である。

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

出力

衝突が発生するならば Yes を、発生しないならば No を出力せよ。


入力例 1

3
2 3
1 1
4 1
RRL

出力例 1

Yes

この入力は問題文にある例と同じケースです。
すべての人が歩き続けると人 2 と人 3 が衝突します。よって Yes を出力します。


入力例 2

2
1 1
2 1
RR

出力例 2

No

1 と人 2 は同じ向きに歩いているので衝突することはありません。


入力例 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

出力例 3

Yes

Score : 300 points

Problem Statement

There are N people in an xy-plane. Person i is at (X_i, Y_i). The positions of all people are different.

We have a string S of length N consisting of L and R.
If S_i = R, Person i is facing right; if S_i = L, Person i is facing left. All people simultaneously start walking in the direction they are facing. Here, right and left correspond to the positive and negative x-direction, respectively.

For example, the figure below shows the movement of people when (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL.

image

We say that there is a collision when two people walking in opposite directions come to the same position. Will there be a collision if all people continue walking indefinitely?

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
  • All X_i and Y_i are integers.
  • S is a string of length N consisting of L and R.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

Output

If there will be a collision, print Yes; otherwise, print No.


Sample Input 1

3
2 3
1 1
4 1
RRL

Sample Output 1

Yes

This input corresponds to the example in the Problem Statement.
If all people continue walking, Person 2 and Person 3 will collide. Thus, Yes should be printed.


Sample Input 2

2
1 1
2 1
RR

Sample Output 2

No

Since Person 1 and Person 2 walk in the same direction, they never collide.


Sample Input 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

Sample Output 3

Yes
D - Moves on Binary Tree

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

配点 : 400

問題文

頂点数 2^{10^{100}}-1 の完全二分木があり、頂点には 1,2,...,2^{10^{100}}-1 の番号がついています。
頂点 1 が根であり、各 1\leq i < 2^{10^{100}-1} について、頂点 i は 頂点 2i を左の子、頂点 2i+1 を右の子として持ちます。

高橋君は最初頂点 X におり、N 回移動を行います。移動は文字列 S により表され、i 回目の移動は次のように行います。

  • Si 番目の文字が U のとき、今いる頂点の親に移動する
  • Si 番目の文字が L のとき、今いる頂点の左の子に移動する
  • Si 番目の文字が R のとき、今いる頂点の右の子に移動する

N 回の移動後に高橋君がいる頂点の番号を求めてください。なお、答えが 10^{18} 以下になるような入力のみが与えられます。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq X \leq 10^{18}
  • N,X は整数
  • S は長さ N であり、U,L,R のみからなる
  • 高橋君が根にいるとき、親に移動しようとすることはない
  • 高橋君が葉にいるとき、子に移動しようとすることはない
  • 高橋君が N 回の移動後にいる頂点の番号は 10^{18} 以下である

入力

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

N X
S

出力

答えを出力せよ。


入力例 1

3 2
URL

出力例 1

6

完全二分木は次のような構造をしています。

図

各移動により、高橋君がいる頂点の番号は 2 \to 1 \to 3 \to 6 と変化します。


入力例 2

4 500000000000000000
RRUU

出力例 2

500000000000000000

移動の途中過程において、高橋君がいる頂点の番号が 10^{18} を超えることもあります。


入力例 3

30 123456789
LRULURLURLULULRURRLRULRRRUURRU

出力例 3

126419752371

Score : 400 points

Problem Statement

There is a perfect binary tree with 2^{10^{100}}-1 vertices, numbered 1,2,...,2^{10^{100}}-1.
Vertex 1 is the root. For each 1\leq i < 2^{10^{100}-1}, Vertex i has two children: Vertex 2i to the left and Vertex 2i+1 to the right.

Takahashi starts at Vertex X and performs N moves, represented by a string S. The i-th move is as follows.

  • If the i-th character of S is U, go to the parent of the vertex he is on now.
  • If the i-th character of S is L, go to the left child of the vertex he is on now.
  • If the i-th character of S is R, go to the right child of the vertex he is on now.

Find the index of the vertex Takahashi will be on after N moves. In the given cases, it is guaranteed that the answer is at most 10^{18}.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq X \leq 10^{18}
  • N and X are integers.
  • S has a length of N and consists of U, L, and R.
  • When Takahashi is at the root, he never attempts to go to the parent.
  • When Takahashi is at a leaf, he never attempts to go to a child.
  • The index of the vertex Takahashi is on after N moves is at most 10^{18}.

Input

Input is given from Standard Input in the following format:

N X
S

Output

Print the answer.


Sample Input 1

3 2
URL

Sample Output 1

6

The perfect binary tree has the following structure.

Figure

In the three moves, Takahashi goes 2 \to 1 \to 3 \to 6.


Sample Input 2

4 500000000000000000
RRUU

Sample Output 2

500000000000000000

During the process, Takahashi may be at a vertex whose index exceeds 10^{18}.


Sample Input 3

30 123456789
LRULURLURLULULRURRLRULRRRUURRU

Sample Output 3

126419752371
E - Edge Deletion

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

配点 : 500

問題文

N 頂点 M 辺の単純連結無向グラフが与えられます。
i は頂点 A_i と頂点 B_i を結ぶ長さ C_i の辺です。

以下の条件を満たすようにいくつかの辺を削除します。削除する辺の数の最大値を求めてください。

  • 辺を削除した後のグラフも連結である。
  • 全ての頂点対 (s,t) について、頂点 s と頂点 t の間の距離が削除前と削除後で変化しない。

注釈

単純連結無向グラフとは、単純かつ連結で辺に向きの無いグラフのことをいいます。
グラフが単純であるとは、グラフが自己ループや多重辺を含まないことをいいます。
グラフが連結であるとは、グラフ上の任意の 2 頂点 s, t について s から t へ辺をたどって行けることをいいます。
頂点 s と頂点 t の間の距離とは、頂点 s と頂点 t の間の最短路の長さのことをいいます。

制約

  • 2 \leq N \leq 300
  • N-1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i \lt B_i \leq N
  • 1 \leq C_i \leq 10^9
  • i \neq j ならば (A_i, B_i) \neq (A_j, B_j) である。
  • 与えられるグラフは連結である。
  • 入力はすべて整数である。

入力

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

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

出力

答えを出力せよ。


入力例 1

3 3
1 2 2
2 3 3
1 3 6

出力例 1

1

辺を削除する前の全ての頂点対の距離は次の通りです。

  • 頂点 1 と頂点 2 の距離は 2
  • 頂点 1 と頂点 3 の距離は 5
  • 頂点 2 と頂点 3 の距離は 3

3 を削除しても全ての頂点間の距離は変化しません。また、問題文の条件を満たすように 2 本以上の辺を削除することはできないので、答えは 1 本になります。


入力例 2

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

出力例 2

0

どの辺も削除することができません。


入力例 3

5 10
1 2 71
1 3 9
1 4 82
1 5 64
2 3 22
2 4 99
2 5 1
3 4 24
3 5 18
4 5 10

出力例 3

5

Score : 500 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges.
Edge i connects Vertex A_i and Vertex B_i, and has a length of C_i.

Let us delete some number of edges while satisfying the conditions below. Find the maximum number of edges that can be deleted.

  • The graph is still connected after the deletion.
  • For every pair of vertices (s, t), the distance between s and t remains the same before and after the deletion.

Notes

A simple connected undirected graph is a graph that is simple and connected and has undirected edges.
A graph is simple when it has no self-loops and no multi-edges.
A graph is connected when, for every pair of two vertices s and t, t is reachable from s by traversing edges.
The distance between Vertex s and Vertex t is the length of a shortest path between s and t.

Constraints

  • 2 \leq N \leq 300
  • N-1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i \lt B_i \leq N
  • 1 \leq C_i \leq 10^9
  • (A_i, B_i) \neq (A_j, B_j) if i \neq j.
  • The given graph is connected.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

Output

Print the answer.


Sample Input 1

3 3
1 2 2
2 3 3
1 3 6

Sample Output 1

1

The distance between each pair of vertices before the deletion is as follows.

  • The distance between Vertex 1 and Vertex 2 is 2.
  • The distance between Vertex 1 and Vertex 3 is 5.
  • The distance between Vertex 2 and Vertex 3 is 3.

Deleting Edge 3 does not affect the distance between any pair of vertices. It is impossible to delete two or more edges while satisfying the condition, so the answer is 1.


Sample Input 2

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

Sample Output 2

0

No edge can be deleted.


Sample Input 3

5 10
1 2 71
1 3 9
1 4 82
1 5 64
2 3 22
2 4 99
2 5 1
3 4 24
3 5 18
4 5 10

Sample Output 3

5
F - Lottery

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

配点 : 500

問題文

高橋君はくじを引こうとしています。

くじを 1 回引くごとに、N 種類の賞品のいずれかが手に入ります。賞品 i が手に入る確率は \frac{W_i}{\sum_{j=1}^{N}W_j} であり、各くじの結果は独立です。

くじを K 回引いたとき、ちょうど M 種類の賞品が手に入る確率はいくらでしょうか? \bmod 998244353 で求めてください。

注記

有理数を出力する際は、まずその有理数を分数 \frac{y}{x} として表してください。 ここで、x,y は整数であり、x998244353 で割り切れてはなりません(この問題の制約下で、そのような表現は必ず可能です)。 そして、xz\equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の唯一の整数 z を出力してください。

制約

  • 1 \leq K \leq 50
  • 1 \leq M \leq N \leq 50
  • 0 < W_i
  • 0 < W_1 + \ldots + W_N < 998244353
  • 入力は全て整数である

入力

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

N M K
W_1
\vdots
W_N

出力

答えを出力せよ。


入力例 1

2 1 2
2
1

出力例 1

221832079

各くじの結果として、賞品 1 が手に入る確率が \frac{2}{3}、賞品 2 が手に入る確率が \frac{1}{3} です。

2 回のくじの結果として、ともに賞品 1 を手に入れる確率が \frac{4}{9}、ともに賞品 2 を手に入れる確率が \frac{1}{9} であるため、求める答えは \frac{5}{9} です。

これを注記にしたがって \bmod 998244353 で出力すると 221832079 になります。


入力例 2

3 3 2
1
1
1

出力例 2

0

くじを 2 回引いて 3 種類の賞品を手に入れることはできません。したがって求める確率は 0 です。


入力例 3

3 3 10
499122176
499122175
1

出力例 3

335346748

入力例 4

10 8 15
1
1
1
1
1
1
1
1
1
1

出力例 4

755239064

Score : 500 points

Problem Statement

Takahashi is participating in a lottery.

Each time he takes a draw, he gets one of the N prizes available. Prize i is awarded with probability \frac{W_i}{\sum_{j=1}^{N}W_j}. The results of the draws are independent of each other.

What is the probability that he gets exactly M different prizes from K draws? Find it modulo 998244353.

Note

To print a rational number, start by representing it as a fraction \frac{y}{x}. Here, x and y should be integers, and x should not be divisible by 998244353 (under the Constraints of this problem, such a representation is always possible). Then, print the only integer z between 0 and 998244352 (inclusive) such that xz\equiv y \pmod{998244353}.

Constraints

  • 1 \leq K \leq 50
  • 1 \leq M \leq N \leq 50
  • 0 < W_i
  • 0 < W_1 + \ldots + W_N < 998244353
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
W_1
\vdots
W_N

Output

Print the answer.


Sample Input 1

2 1 2
2
1

Sample Output 1

221832079

Each draw awards Prize 1 with probability \frac{2}{3} and Prize 2 with probability \frac{1}{3}.

He gets Prize 1 at both of the two draws with probability \frac{4}{9}, and Prize 2 at both draws with probability \frac{1}{9}, so the sought probability is \frac{5}{9}.

The modulo 998244353 representation of this value, according to Note, is 221832079.


Sample Input 2

3 3 2
1
1
1

Sample Output 2

0

It is impossible to get three different prizes from two draws, so the sought probability is 0.


Sample Input 3

3 3 10
499122176
499122175
1

Sample Output 3

335346748

Sample Input 4

10 8 15
1
1
1
1
1
1
1
1
1
1

Sample Output 4

755239064
G - Sqrt

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

配点 : 600

問題文

長さ 1 の数列 A=(X) があります。この数列に対して次の操作を 10^{100} 回行います。

操作:A の末尾の要素を Y とする。1 以上 \sqrt{Y} 以下の整数を自由に選び、A の末尾に追加する。

10^{100} 回の操作後にできる数列は何種類ありますか?

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

なお、制約の条件下で答えは 2^{63} 未満になることが証明されます。

制約

  • 1 \leq T \leq 20
  • 1 \leq X \leq 9\times 10^{18}
  • 入力に含まれる値は全て整数である

入力

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

T
\rm case_1
\vdots
\rm case_T

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

X

出力

T 行出力せよ。 i 行目には、\rm case_i に対する答えを出力せよ。


入力例 1

4
16
1
123456789012
1000000000000000000

出力例 1

5
1
4555793983
23561347048791096

1 つ目のケースでは、操作後の数列として考えられるものは次の 5 種類です。

  • (16,4,2,1,1,1,\ldots)
  • (16,4,1,1,1,1,\ldots)
  • (16,3,1,1,1,1,\ldots)
  • (16,2,1,1,1,1,\ldots)
  • (16,1,1,1,1,1,\ldots)

Score : 600 points

Problem Statement

We have a sequence of length 1: A=(X). Let us perform the following operation on this sequence 10^{100} times.

Operation: Let Y be the element at the end of A. Choose an integer between 1 and \sqrt{Y} (inclusive), and append it to the end of A.

How many sequences are there that can result from 10^{100} operations?

You will be given T test cases to solve.

It can be proved that the answer is less than 2^{63} under the Constraints.

Constraints

  • 1 \leq T \leq 20
  • 1 \leq X \leq 9\times 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
\rm case_1
\vdots
\rm case_T

Each case is in the following format:

X

Output

Print T lines. The i-th line should contain the answer for \rm case_i.


Sample Input 1

4
16
1
123456789012
1000000000000000000

Sample Output 1

5
1
4555793983
23561347048791096

In the first case, the following five sequences can result from the operations.

  • (16,4,2,1,1,1,\ldots)
  • (16,4,1,1,1,1,\ldots)
  • (16,3,1,1,1,1,\ldots)
  • (16,2,1,1,1,1,\ldots)
  • (16,1,1,1,1,1,\ldots)
Ex - Builder Takahashi (Enhanced version)

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

配点 : 600

問題文

縦横 H \times W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
各マスの状態は C_{i,j} で表されます。各マスの状態は次の 4 つのいずれかです。

  • S : スタート地点。グリッド上にちょうど 1 つだけ存在する。
  • G : ゴール地点。グリッド上にちょうど 1 つだけ存在する。
  • . : 壁を建ててよいマス。
  • O : 壁を建ててはいけないマス。

青木君はスタート地点を出発してグリッド上を移動してゴール地点へ行こうとしています。青木君は (i,j) にいるときにマス (i+1,j),(i,j+1),(i-1,j),(i,j-1) のいずれかに移動することができます。ただし、グリッドの外に出るような移動や、壁があるマスへの移動を行うことはできません。

高橋君は、青木君が移動を開始する前に壁を建ててよいマスを 1 マス以上選んで壁を建てることで、青木君がどのように移動してもゴール地点へ行くことができないようにすることにしました。ただし、スタート地点およびゴール地点は選ぶことができません。

高橋君は青木君がゴールできないように壁を建てることができますか?さらに、壁を建てられる場合は

  • 青木君がゴールできないように壁を建てるときの最小枚数 n 、および
  • 最小枚数を達成する壁の立て方が何通りあるかを 998244353 で割ったあまり r

2 つも合わせて計算してください。

制約

  • 2 \leq H \leq 100
  • 2 \leq W \leq 100
  • C_{i,j}S, G, ., O のいずれかである。
  • S, GC_{i,j} の中でちょうど 1 回だけ登場する。

入力

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

H W
C_{1,1}C_{1,2}\dotsC_{1,W}
C_{2,1}C_{2,2}\dotsC_{2,W}
\vdots
C_{H,1}C_{H,2}\dotsC_{H,W}

出力

青木君がゴールできないように壁を建てられる場合は、文字列 Yes 、および問題文中で定義された整数 n, r を以下の形式で出力せよ。

Yes
n r

そうでない場合は No を出力せよ。


入力例 1

4 3
S..
O..
..O
..G

出力例 1

Yes
3 6

壁を建てるマスを # で表すと、最小枚数を達成する壁の建て方は次の 6 通りとなります。

S#.  S.#  S..  S..  S..  S..
O#.  O#.  O##  O.#  O.#  O.#
#.O  #.O  #.O  ##O  .#O  .#O
..G  ..G  ..G  ..G  #.G  .#G

入力例 2

3 2
.G
.O
.S

出力例 2

No

高橋君がどのように壁を建てても青木君はゴール地点へ行くことができます。


入力例 3

2 2
S.
.G

出力例 3

Yes
2 1

入力例 4

10 10
OOO...OOO.
.....OOO.O
OOO.OO.OOO
OOO..O..S.
....O.O.O.
.OO.O.OOOO
..OOOG.O.O
.O.O..OOOO
.O.O.OO...
...O..O..O

出力例 4

Yes
10 12

Score : 600 points

Problem Statement

There is a grid with H \times W squares. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.
C_{i,j} represents the state of each square. Each square is in one of the following four states.

  • S: The starting point. The grid has exactly one starting point.
  • G: The goal. The grid has exactly one goal.
  • .: A constructible square, where a wall can be built.
  • O: An unconstructible square, where a wall cannot be built.

Aoki intends to start at the starting point and get to the goal. When he is at (i,j), he can go to (i+1,j), (i,j+1), (i-1,j), or (i,j-1). It is not allowed to exit the grid or enter a square with a wall.

Takahashi decides to build a wall on one or more constructible squares of his choice before Aoki starts so that there is no way for Aoki to reach the goal. Here, the starting point and the goal cannot be chosen.

Is it possible for Takahashi to build walls to prevent Aoki from reaching the goal? If it is possible, also compute the two values below:

  • the minimum number n of walls needed to prevent Aoki from reaching the goal, and
  • the number of ways modulo 998244353, r, to achieve that minimum number of walls.

Constraints

  • 2 \leq H \leq 100
  • 2 \leq W \leq 100
  • C_{i,j} is S, G, ., or O.
  • Each of S and G appears exactly once in C_{i,j}.

Input

Input is given from Standard Input in the following format:

H W
C_{1,1}C_{1,2}\dotsC_{1,W}
C_{2,1}C_{2,2}\dotsC_{2,W}
\vdots
C_{H,1}C_{H,2}\dotsC_{H,W}

Output

If it is possible to build walls to prevent Aoki from reaching the goal, print the string Yes and the integers n and r defined in the Problem Statement, in the following format:

Yes
n r

Otherwise, print No.


Sample Input 1

4 3
S..
O..
..O
..G

Sample Output 1

Yes
3 6

Let # represent a square to build a wall on. The six ways to achieve the minimum number of walls are as follows:

S#.  S.#  S..  S..  S..  S..
O#.  O#.  O##  O.#  O.#  O.#
#.O  #.O  #.O  ##O  .#O  .#O
..G  ..G  ..G  ..G  #.G  .#G

Sample Input 2

3 2
.G
.O
.S

Sample Output 2

No

Regardless of how Takahashi builds walls, Aoki can always reach the goal.


Sample Input 3

2 2
S.
.G

Sample Output 3

Yes
2 1

Sample Input 4

10 10
OOO...OOO.
.....OOO.O
OOO.OO.OOO
OOO..O..S.
....O.O.O.
.OO.O.OOOO
..OOOG.O.O
.O.O..OOOO
.O.O.OO...
...O..O..O

Sample Output 4

Yes
10 12