A - Shampoo

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

配点 : 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 - World Cup

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

配点 : 100 点

問題文

あるスポーツ大会は西暦年を 4 で割った余りが 2 である年の 6 月に開催されます。
現在が西暦 Y 年の 1 月である時、このスポーツ大会が次に開催されるのは西暦何年になるかを求めてください。

制約

  • 2000 \leq Y \leq 3000
  • Y は整数

入力

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

Y

出力

答えを出力せよ。


入力例 1

2022

出力例 1

2022

2022 は 4 で割った余りが 2 なので、現在が西暦 2022 年の 1 月である時、次の開催は同年の 6 月です。


入力例 2

2023

出力例 2

2026

入力例 3

3000

出力例 3

3002

Score : 100 points

Problem Statement

A sport event is held in June of every year whose remainder when divided by 4 is 2.
Suppose that it is now January of the year Y. In what year will this sport event be held next time?

Constraints

  • 2000 \leq Y \leq 3000
  • Y is an integer.

Input

Input is given from Standard Input in the following format:

Y

Output

Print the answer.


Sample Input 1

2022

Sample Output 1

2022

The remainder when 2022 is divided by 4 is 2, so if it is now January of 2022, the next games will be held in June of the same year.


Sample Input 2

2023

Sample Output 2

2026

Sample Input 3

3000

Sample Output 3

3002
C - Get Min

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

配点 : 200 点

問題文

中に何も入っていない袋があります。

Q 個のクエリが与えられるので、これらのクエリを順に処理したときのタイプ 2 の各クエリの答えを出力してください。

各クエリは、以下のいずれかの形式です。

  • タイプ 1 : 1 x の形式で入力として与えられる。整数 x が書かれたボールを 1 つ袋の中に入れる。

  • タイプ 2 : 2 の形式で入力として与えられる。袋に入っているボールのうち書かれている整数が最小のものを 1 つ取り出し、その整数を答えとする。ただし、袋の中にボールが入っていないときにはこのクエリは与えられない。

制約

  • 2 \leq Q \leq 100
  • タイプ 1 のクエリにおいて、1 \leq x \leq 100
  • タイプ 2 のクエリが与えられたとき、袋は空ではない
  • タイプ 2 のクエリが 1 つ以上与えられる
  • 入力される値はすべて整数

入力

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

Q 
\text{query}_1
\text{query}_2
\ldots
\text{query}_Q

ただし、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。

1 x
2

出力

タイプ 2 のクエリの個数を q として、q 行出力せよ。

i 行目には、i 番目のタイプ 2 のクエリの答えを出力せよ。


入力例 1

5
1 6
1 7
2
1 1
2

出力例 1

6
1

はじめ、袋の中にボールは入っていません。

1 番目のクエリでは、袋の中に 6 が書かれたボールを入れます。

2 番目のクエリでは、袋の中に 7 が書かれたボールを入れます。

3 番目のクエリでは、袋の中には 6 が書かれたボールと 7 が書かれたボールが入っているため、6 が書かれたボールを取り出します。このクエリの答えは 6 となります。

4 番目のクエリでは、袋の中に 1 が書かれたボールを入れます。

5 番目のクエリでは、袋の中には 1 が書かれたボールと 7 が書かれたボールが入っているため、1 が書かれたボールを取り出します。このクエリの答えは 1 となります。


入力例 2

8
1 5
1 1
1 1
1 9
2
2
1 2
2

出力例 2

1
1
2

同じ整数が書かれたボールを複数個袋の中に入れることもあります。

Score : 200 points

Problem Statement

There is an empty bag.

You are given Q queries. Process these queries in order and output the answer to each type-2 query.

Each query is of one of the following types.

  • Type 1: Given as input in the format 1 x. Put a ball with the integer x written on it into the bag.

  • Type 2: Given as input in the format 2. Pick out one ball with the minimum integer written on it from the balls in the bag, and report that integer as the answer. This query is not given when the bag contains no balls.

Constraints

  • 2 \leq Q \leq 100
  • In a type-1 query, 1 \leq x \leq 100.
  • When a type-2 query is given, the bag is not empty.
  • At least one type-2 query is given.
  • All input values are integers.

Input

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

Q 
\text{query}_1
\text{query}_2
\ldots
\text{query}_Q

Here, \text{query}_i is the i-th query and is given in one of the following formats:

1 x
2

Output

Let q be the number of type-2 queries, and output q lines.

The i-th line should contain the answer to the i-th type-2 query.


Sample Input 1

5
1 6
1 7
2
1 1
2

Sample Output 1

6
1

Initially, the bag contains no balls.

The 1st query puts a ball with 6 written on it into the bag.

The 2nd query puts a ball with 7 written on it into the bag.

In the 3rd query, the bag contains a ball with 6 written on it and a ball with 7 written on it, so you pick out the ball with 6 written on it. The answer to this query is 6.

The 4th query puts a ball with 1 written on it into the bag.

In the 5th query, the bag contains a ball with 1 written on it and a ball with 7 written on it, so you pick out the ball with 1 written on it. The answer to this query is 1.


Sample Input 2

8
1 5
1 1
1 1
1 9
2
2
1 2
2

Sample Output 2

1
1
2

The bag may contain multiple balls with the same integer.

D - 3-smooth Numbers

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

配点 : 200 点

問題文

正の整数 N が与えられます。 N=2^x3^y を満たす整数 x,y が存在するなら Yes 、そうでなければ No と出力してください。

制約

  • 1\leq N\leq10^{18}
  • N は整数

入力

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

N

出力

条件を満たす整数 x,y が存在するなら Yes 、そうでなければ No と 1 行に出力せよ。


入力例 1

324

出力例 1

Yes

x=2,y=4 とすると、2^x3^y=2^23^4=4\times81=324 となるため条件を満たします。 よって、Yes と出力してください。


入力例 2

5

出力例 2

No

どのような整数 x,y をとっても 2^x3^y=5 とすることはできません。 よって、No と出力してください。


入力例 3

32

出力例 3

Yes

x=5,y=0 とすると、2^x3^y=32\times1=32 となるため、Yes と出力してください。


入力例 4

37748736

出力例 4

Yes

Score : 200 points

Problem Statement

You are given a positive integer N. If there are integers x and y such that N=2^x3^y, print Yes; otherwise, print No.

Constraints

  • 1\leq N\leq10^{18}
  • N is an integer.

Input

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

N

Output

Print a single line containing Yes if there are integers x and y that satisfy the condition, and No otherwise.


Sample Input 1

324

Sample Output 1

Yes

For x=2,y=4, we have 2^x3^y=2^23^4=4\times81=324, so the condition is satisfied. Thus, you should print Yes.


Sample Input 2

5

Sample Output 2

No

There are no integers x and y such that 2^x3^y=5. Thus, you should print No.


Sample Input 3

32

Sample Output 3

Yes

For x=5,y=0, we have 2^x3^y=32\times1=32, so you should print Yes.


Sample Input 4

37748736

Sample Output 4

Yes
E - Doukasen

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

配点 : 300 点

問題文

N 本の導火線を一直線に接着したものがあります。左から i 本目の導火線は長さが A_i cm で、 1 秒あたり B_i cm の一定の速さで燃えます。

この導火線の左端と右端から同時に火をつけるとき、 2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 1000
  • 入力は全て整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か(単位を除いて)出力せよ。

想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。


入力例 1

3
1 1
2 1
3 1

出力例 1

3.000000000000000

着火前の導火線の左端から 3 cm の地点で 2 つの火がぶつかります。


入力例 2

3
1 3
2 2
3 1

出力例 2

3.833333333333333

入力例 3

5
3 9
1 2
4 6
1 5
5 3

出力例 3

8.916666666666668

Score : 300 points

Problem Statement

We have N fuses connected in series. The i-th fuse from the left has a length of A_i centimeters and burns at a constant speed of B_i centimeters per second.

Consider igniting this object from left and right ends simultaneously. Find the distance between the position where the two flames will meet and the left end of the object.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the distance between the position where the two flames will meet and the left end of the object, in centimeters (print just the number).

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-5}.


Sample Input 1

3
1 1
2 1
3 1

Sample Output 1

3.000000000000000

The two flames will meet at 3 centimeters from the left end of the object.


Sample Input 2

3
1 3
2 2
3 1

Sample Output 2

3.833333333333333

Sample Input 3

5
3 9
1 2
4 6
1 5
5 3

Sample Output 3

8.916666666666668
F - Cards Query Problem

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

配点 : 300 点

問題文

1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。

  • 1 i j \colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる
  • 2 i \colon 箱 i に入っているカードに書かれた数を昇順ですべて答える
  • 3 i \colon 数 i が書かれたカードが入っている箱の番号を昇順ですべて答える

ただし、以下の点に注意してください。

  • 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
  • 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • 2 番目の形式のクエリについて、
    • 1 \leq i \leq N
    • このクエリが与えられる時点で箱 i にカードが入っている
  • 3 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
  • 出力するべき数は合計で 2 \times 10^5 個以下
  • 入力はすべて整数

入力

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ただし、\mathrm{query}_q は q 個目のクエリを表しており、次のいずれかの形式で与えられる。

1 i j
2 i
3 i

出力

2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。


入力例 1

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

出力例 1

1 2
1 1 2
1 4
4

クエリを順に処理していきます。

  • カードに 1 を書き込み、箱 1 に入れます。
  • カードに 2 を書き込み、箱 4 に入れます。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 箱 4 に入っているカードに書かれた数は 1, 2 です。
    • 1, 2 の順に出力してください。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 箱 4 に入っているカードに書かれた数は 1, 1, 2 です。
    • 1 を 2 度出力することに注意してください。
  • 数 1 が書かれたカードが入っている箱は箱 1, 4 です。
    • 箱 4 には数 1 が書かれたカードが 2 枚入っていますが、4 は 1 回しか出力しないことに注意してください。
  • 数 2 が書かれたカードが入っている箱は箱 4 です。

入力例 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

出力例 2

1 2 200000
1

Score : 300 points

Problem Statement

We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.

  • 1 i j \colon Write the number i on a blank card and put it into box j.
  • 2 i \colon Report all numbers written on the cards in box i, in ascending order.
  • 3 i \colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.

Here, note the following.

  • In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
  • In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • For a query of the first kind:
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • For a query of the second kind:
    • 1 \leq i \leq N
    • Box i contains some cards when this query is given.
  • For a query of the third kind:
    • 1 \leq i \leq 2 \times 10^5
    • Some boxes contain a card with the number i when this query is given.
  • At most 2 \times 10^5 numbers are to be reported.
  • All values in the input are integers.

Input

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:

1 i j
2 i
3 i

Output

Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.


Sample Input 1

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

Sample Output 1

1 2
1 1 2
1 4
4

Let us process the queries in order.

  • Write 1 on a card and put it into box 1.
  • Write 2 on a card and put it into box 4.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1 and 2.
    • Print 1 and 2 in this order.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1, 1, and 2.
    • Note that you should print 1 twice.
  • Boxes 1 and 4 contain a card with the number 1.
    • Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
  • Boxes 4 contains a card with the number 2.

Sample Input 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

Sample Output 2

1 2 200000
1
G - Snaky Walk

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

配点 : 400 点

問題文

H 行 W 列のグリッドがあります。 上から i 行目、左から j 列目のマスを (i,j) と表記します。

各マスはスタートマス・ゴールマス・空きマス・障害物マスのいずれかであり、その情報は H 個の長さ W の文字列 S_1,S_2,\dots,S_H によって表されます。 具体的には、マス (i,j) は S_i の j 文字目が S であるときスタートマス、G であるときゴールマス、. であるとき空きマス、# であるとき障害物マスです。 ここで、スタートマスとゴールマスはちょうど 1 つずつ存在することが保証されます。

あなたは今スタートマスにいます。 あなたの目標は、今いるマスと辺で隣接するマスに移動することを繰り返してゴールマスへ行くことです。 ただし、障害物マスやグリッドの外に移動することはできず、また縦移動と横移動を 1 回ずつ交互に行わなければなりません。(最初の移動の向きは任意です。)

ゴールマスへ行くことが可能であるか判定し、可能ならば移動回数の最小値を求めてください。

より形式的には、以下の条件をすべて満たすマスの列 (i_1,j_1),(i_2,j_2),\dots,(i_k,j_k) が存在するか判定し、存在するならば k-1 の最小値を求めてください。

  • すべての 1\leq l\leq k について、1\leq i_l\leq H かつ 1\leq j_l\leq W であり、(i_l,j_l) は障害物マスでない
  • (i_1,j_1) はスタートマス
  • (i_k,j_k) はゴールマス
  • すべての 1\leq l\leq k-1 について、|i_l-i_{l+1}|+|j_l-j_{l+1}|=1
  • すべての 1\leq l\leq k-2 について、i_l\neq i_{l+1} ならば i_{l+1}=i_{l+2}
  • すべての 1\leq l\leq k-2 について、j_l\neq j_{l+1} ならば j_{l+1}=j_{l+2}

制約

  • 1\leq H,W \leq 1000
  • H,W は整数
  • S_i は S, G, ., # からなる長さ W の文字列
  • スタートマスとゴールマスはちょうど 1 つずつ存在する

入力

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

H W
S_1
S_2
\vdots
S_H

出力

ゴールマスへ行くことが可能ならば移動回数の最小値を、不可能ならば -1 を出力せよ。


入力例 1

3 5
.S#.G
.....
.#...

出力例 1

7

左図のように (1,2)\rightarrow(2,2)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,4)\rightarrow(2,4)\rightarrow(2,5)\rightarrow(1,5) と移動することで、7 回の移動でゴールマスへ行くことができます。 6 回以下の移動でゴールマスへ行くことはできないので、答えは 7 です。

右図のように横移動を連続で行う経路(あるいは縦移動を連続で行う経路)はとれないことに注意してください。


入力例 2

3 5
..#.G
.....
S#...

出力例 2

-1

ゴールマスへ行くことはできません。


入力例 3

8 63
...............................................................
..S...#............................#####..#####..#####..####G..
..#...#................................#..#...#......#..#......
..#####..####...####..####..#..#...#####..#...#..#####..#####..
..#...#..#..#...#..#..#..#..#..#...#......#...#..#..........#..
..#...#..#####..####..####..####...#####..#####..#####..#####..
................#.....#........#...............................
................#.....#........#...............................

出力例 3

148

Score : 400 points

Problem Statement

You are given a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.

Each cell is one of the following: a start cell, a goal cell, an empty cell, or an obstacle cell. This information is described by H strings S_1,S_2,\dots,S_H, each of length W. Specifically, the cell (i,j) is a start cell if the j-th character of S_i is S, a goal cell if it is G, an empty cell if it is ., and an obstacle cell if it is #. It is guaranteed that there is exactly one start cell and exactly one goal cell.

You are currently on the start cell. Your objective is to reach the goal cell by repeatedly moving to a cell adjacent by an edge to the one you are in. However, you cannot move onto an obstacle cell or move outside the grid, and you must alternate between moving vertically and moving horizontally each time. (The direction of the first move can be chosen arbitrarily.)

Determine if it is possible to reach the goal cell. If it is, find the minimum number of moves required.

More formally, check if there exists a sequence of cells (i_1,j_1),(i_2,j_2),\dots,(i_k,j_k) satisfying all of the following conditions. If such a sequence exists, find the minimum value of k-1.

  • For every 1 \leq l \leq k, it holds that 1 \leq i_l \leq H and 1 \leq j_l \leq W, and (i_l,j_l) is not an obstacle cell.
  • (i_1,j_1) is the start cell.
  • (i_k,j_k) is the goal cell.
  • For every 1 \leq l \leq k-1, |i_l - i_{l+1}| + |j_l - j_{l+1}| = 1.
  • For every 1 \leq l \leq k-2, if i_l \neq i_{l+1}, then i_{l+1} = i_{l+2}.
  • For every 1 \leq l \leq k-2, if j_l \neq j_{l+1}, then j_{l+1} = j_{l+2}.

Constraints

  • 1 \leq H,W \leq 1000
  • H and W are integers.
  • Each S_i is a string of length W consisting of S, G, ., #.
  • There is exactly one start cell and exactly one goal cell.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

If it is possible to reach the goal cell, print the minimum number of moves. Otherwise, print -1.


Sample Input 1

3 5
.S#.G
.....
.#...

Sample Output 1

7

As shown in the left figure, you can move as (1,2)\rightarrow(2,2)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,4)\rightarrow(2,4)\rightarrow(2,5)\rightarrow(1,5), reaching the goal cell in 7 moves. It is impossible in 6 moves or fewer, so the answer is 7.

Note that you cannot take a path that moves horizontally (or vertically) consecutively without alternating as shown in the right figure.


Sample Input 2

3 5
..#.G
.....
S#...

Sample Output 2

-1

It is not possible to reach the goal cell.


Sample Input 3

8 63
...............................................................
..S...#............................#####..#####..#####..####G..
..#...#................................#..#...#......#..#......
..#####..####...####..####..#..#...#####..#...#..#####..#####..
..#...#..#..#...#..#..#..#..#..#...#......#...#..#..........#..
..#...#..#####..####..####..####...#####..#####..#####..#####..
................#.....#........#...............................
................#.....#........#...............................

Sample Output 3

148
H - Toward 0

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

配点 : 450 点

問題文

整数 N が与えられます。あなたは次の 2 種類の操作を行うことができます。

  • X 円払う。N を \displaystyle\left\lfloor\frac{N}{A}\right\rfloor に置き換える。
  • Y 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。

ここで \lfloor s \rfloor は s 以下の最大の整数を表します。例えば \lfloor 3 \rfloor=3、\lfloor 2.5 \rfloor=2 です。

適切に操作を選択したとき、N を 0 にするまでに払う必要がある金額の期待値の最小値を求めてください。
なお、サイコロの出目は操作ごとに独立であり、操作の選択はそれまでの操作の結果を確認してから行うことができます。

制約

  • 1 \leq N \leq 10^{18}
  • 2 \leq A \leq 6
  • 1 \leq X,Y \leq 10^9
  • 入力は全て整数である

入力

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

N A X Y

出力

答えを出力せよ。
真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

3 2 10 20

出力例 1

20.000000000000000

行える操作は 次の 2 種類です。

  • 10 円払う。N を \displaystyle\left\lfloor\frac{N}{2}\right\rfloor に置き換える。
  • 20 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。

前者の操作を 2 回行うのが最適です。


入力例 2

3 2 20 20

出力例 2

32.000000000000000

行える操作は 次の 2 種類です。

  • 20 円払う。N を \displaystyle\left\lfloor\frac{N}{2}\right\rfloor に置き換える。
  • 20 円払う。1 以上 6 以下の整数が等確率で出るサイコロを振る。その出目を b としたとき、N を \displaystyle\left\lfloor\frac{N}{b}\right\rfloor に置き換える。

最適な操作は以下のようになります。

  • まず後者の操作でサイコロを振ります。
    • 4 以上が出た場合 N=0 となります。
    • 2 または 3 が出た場合 N=1 となります。続けて前者の操作を行うことで N=0 となります。
    • 1 が出た場合最初からやり直します。

入力例 3

314159265358979323 4 223606797 173205080

出力例 3

6418410657.7408381

Score: 450 points

Problem Statement

You are given an integer N. You can perform the following two types of operations:

  • Pay X yen to replace N with \displaystyle\left\lfloor\frac{N}{A}\right\rfloor.
  • Pay Y yen to roll a die (dice) that shows an integer between 1 and 6, inclusive, with equal probability. Let b be the outcome of the die, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.

Here, \lfloor s \rfloor denotes the greatest integer less than or equal to s. For example, \lfloor 3 \rfloor=3 and \lfloor 2.5 \rfloor=2.

Determine the minimum expected cost paid before N becomes 0 when optimally choosing operations.
The outcome of the die in each operation is independent of other rolls, and the choice of operation can be made after observing the results of the previous operations.

Constraints

  • 1 \leq N \leq 10^{18}
  • 2 \leq A \leq 6
  • 1 \leq X, Y \leq 10^9
  • All input values are integers.

Input

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

N A X Y

Output

Print the answer.
Your output will be considered correct if the absolute or relative error from the true answer is at most 10^{-6}.


Sample Input 1

3 2 10 20

Sample Output 1

20.000000000000000

The available operations are as follows:

  • Pay 10 yen. Replace N with \displaystyle\left\lfloor\frac{N}{2}\right\rfloor.
  • Pay 20 yen. Roll a die. Let b be the outcome, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.

The optimal strategy is to perform the first operation twice.


Sample Input 2

3 2 20 20

Sample Output 2

32.000000000000000

The available operations are as follows:

  • Pay 20 yen. Replace N with \displaystyle\left\lfloor\frac{N}{2}\right\rfloor.
  • Pay 20 yen. Roll a die. Let b be the outcome, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor.

The optimal strategy is as follows:

  • First, perform the second operation to roll the die.
    • If the outcome is 4 or greater, then N becomes 0.
    • If the outcome is 2 or 3, then N becomes 1. Now, perform the first operation to make N = 0.
    • If the outcome is 1, restart from the beginning.

Sample Input 3

314159265358979323 4 223606797 173205080

Sample Output 3

6418410657.7408381
I - Best Concatenation

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

配点 : 500 点

問題文

1 から 9 の数字および X のみからなる N 個の文字列 S_1, S_2, \ldots, S_N が与えられます。

(1, 2, \ldots, N) を並べ替えた列 P = (P_1, P_2, \ldots, P_N) を一つ選び、 文字列 T = S_{P_1} + S_{P_2} + \cdots + S_{P_N} を作ります。ここで、+ は文字列の連結を表します。

そして、文字列 T = T_1T_2\ldots T_{|T|} の「スコア」を計算します( |T| は文字列 T の長さを表します)。
T のスコアは、スコアが 0 の状態から始め、下記の 9 個の手順を行うことで計算されます。

  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 1 を満たす整数の組 (i, j) の個数だけ、スコアを 1 点加算する 。
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 2 を満たす整数の組 (i, j) の個数だけ、スコアを 2 点加算する。
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 3 を満たす整数の組 (i, j) の個数だけ、スコアを 3 点加算する。
  • \cdots
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 9 を満たす整数の組 (i, j) の個数だけ、スコアを 9 点加算する。

P を任意に選ぶときの、T のスコアとしてあり得る最大値を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N は整数
  • S_i は 1 から 9 の数字および X のみからなる長さ 1 以上の文字列
  • S_1, S_2, \ldots, S_N の長さの総和は 2 \times 10^5 以下

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

3
1X3
59
XXX

出力例 1

71

P = (3, 1, 2) とすると、T = S_3 + S_1 + S_2 = XXX1X359 です。 このとき、T のスコアは次の通り計算されます。

  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 1 を満たす整数の組 (i, j) が 3 個あり、
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 3 を満たす整数の組 (i, j) が 4 個あり、
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 5 を満たす整数の組 (i, j) が 4 個あり、
  • 1 \leq i \lt j \leq |T| および T_i = X かつ T_j = 9 を満たす整数の組 (i, j) が 4 個あります。

よって、T のスコアは 1 \times 3 + 3 \times 4 + 5 \times 4 + 9 \times 4 = 71 であり、これが考えられる最大値です。


入力例 2

10
X63X395XX
X2XX3X22X
13
3716XXX6
45X
X6XX
9238
281X92
1XX4X4XX6
54X9X711X1

出力例 2

3010

Score : 500 points

Problem Statement

You are given N strings S_1, S_2, \ldots, S_N consisting of digits from 1 through 9 and the character X.

We will choose a permutation P = (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) to construct a string T = S_{P_1} + S_{P_2} + \cdots + S_{P_N}, where + denotes a concatenation of strings.

Then, we will calculate the "score" of the string T = T_1T_2\ldots T_{|T|} (where |T| denotes the length of T).
The score is calculated by the following 9 steps, starting from the initial score 0:

  • Add 1 point to the score as many times as the number of integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 1.
  • Add 2 points to the score as many times as the number of integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 2.
  • Add 3 points to the score as many times as the number of integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 3.
  • \cdots
  • Add 9 points to the score as many times as the number of integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 9.

Find the maximum possible score of T when P can be chosen arbitrarily.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N is an integer.
  • S_i is a string of length at least 1 consisting of digits from 1 through 9 and the character X.
  • The sum of lengths of S_1, S_2, \ldots, S_N is at most 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

3
1X3
59
XXX

Sample Output 1

71

When P = (3, 1, 2), we have T = S_3 + S_1 + S_2 = XXX1X359. Then, the score of T is calculated as follows:

  • there are 3 integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 1;
  • there are 4 integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 3;
  • there are 4 integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 5;
  • there are 4 integer pairs (i, j) such that 1 \leq i \lt j \leq |T|, T_i = X, and T_j = 9.

Therefore, the score of T is 1 \times 3 + 3 \times 4 + 5 \times 4 + 9 \times 4 = 71, which is the maximum possible value.


Sample Input 2

10
X63X395XX
X2XX3X22X
13
3716XXX6
45X
X6XX
9238
281X92
1XX4X4XX6
54X9X711X1

Sample Output 2

3010