A - Discount

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

定価 A 円の商品が B 円で売られているとき、この商品の売値は定価の何パーセント引きですか?

制約

  • A, B は整数
  • 1 ≤ B < A ≤ 10^5

入力

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

A B

出力

答えを小数で出力せよ。
想定解答との絶対誤差または相対誤差が 10^{-2} 以下であれば正解と判定される。


入力例 1

100 80

出力例 1

20.0

定価 100 円の商品が 80 円で売られているとき、この売値は定価の 20 %引きです。


入力例 2

7 6

出力例 2

14.285714285714285714

入力例 3

99999 99998

出力例 3

0.00100001000010000100

Score : 100 points

Problem Statement

A shop sells a product whose regular price is A yen (Japanese currency) for B yen. By what percentage of the regular price is this product discounted?

Constraints

  • A and B are integers.
  • 1 ≤ B < A ≤ 10^5

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer as a decimal.
Your answer will be judged as correct when its absolute or relative error from our answer is at most 10^{-2}.


Sample Input 1

100 80

Sample Output 1

20.0

If a product whose regular price is 100 yen is sold for 80 yen, it is discounted by 20 percent of the regular price.


Sample Input 2

7 6

Sample Output 2

14.285714285714285714

Sample Input 3

99999 99998

Sample Output 3

0.00100001000010000100
B - Play Snuke

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋くんは人気ゲーム機「スヌケマシン」を買おうとしています。
スヌケマシンを販売している店は店 1, 2, \dots, NN 軒あり、店 i は高橋くんの現在地から徒歩 A_i 分、スヌケマシンの販売価格は P_i 円、現在のスヌケマシンの在庫は X_i 台です。
高橋くんは今から徒歩でスヌケマシンを販売している店に向かい、店に着いたときにスヌケマシンの在庫があればスヌケマシンを買います。
しかし、スヌケマシンは人気商品なので、今から 0.5, 1.5, 2.5, \dots 分後に全ての店でスヌケマシンの在庫が (存在するなら) 1 台減ります。
高橋くんがスヌケマシンを買うことができるか判定し、できる場合は買うのに必要な最小の金額を求めてください。

制約

  • 入力は全て整数
  • 1 ≤ N ≤ 10^5
  • 1 ≤ A_i, P_i, X_i ≤ 10^9

入力

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

N
A_1 P_1 X_1
\vdots
A_N P_N X_N

出力

高橋くんがスヌケマシンを買うことができる場合は、買うのに必要な最小の金額を出力せよ。
できない場合は、-1 を出力せよ。


入力例 1

3
3 9 5
4 8 5
5 7 5

出力例 1

8

1 に向かうと、高橋くんが着いたときにはスヌケマシンが 2 台残っていて、9 円でスヌケマシンを買うことができます。
2 に向かうと、高橋くんが着いたときにはスヌケマシンが 1 台残っていて、8 円でスヌケマシンを買うことができます。
3 に向かうと、高橋くんが着いたときにはスヌケマシンが売り切れていて、買うことができません。


入力例 2

3
5 9 5
6 8 5
7 7 5

出力例 2

-1

入力例 3

10
158260522 877914575 602436426
24979445 861648772 623690081
433933447 476190629 262703497
211047202 971407775 628894325
731963982 822804784 450968417
430302156 982631932 161735902
880895728 923078537 707723857
189330739 910286918 802329211
404539679 303238506 317063340
492686568 773361868 125660016

出力例 3

861648772

Score : 200 points

Problem Statement

Takahashi wants to buy the popular video game console called Play Snuke.
There are N shops that sell Play Snuke: Shop 1, 2, \dots, N. Shop i is A_i minutes' walk from where Takahashi is now, sells Play Snuke for P_i yen (Japanese currency), and currently has X_i Play Snukes in stock.
Now, Takahashi will go to one of those shops on foot and buy Play Snuke if it is still in stock when he gets there.
However, Play Snuke is so popular that the number of consoles in stock (if any) in every shop will decrease by 1 at the following moments: 0.5, 1.5, 2.5, \dots minutes from now.
Determine whether Takahashi can buy Play Snuke. If he can, find the minimum amount of money needed to buy one.

Constraints

  • All values in input are integers.
  • 1 ≤ N ≤ 10^5
  • 1 ≤ A_i, P_i, X_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 P_1 X_1
\vdots
A_N P_N X_N

Output

If Takahashi can buy Play Snuke, print the minimum amount of money needed to buy one, as an integer.
If he cannot buy one, print -1.


Sample Input 1

3
3 9 5
4 8 5
5 7 5

Sample Output 1

8

If he goes to Shop 1, it will still have 2 Play Snukes when he gets there, and he can buy one for 9 yen.
If he goes to Shop 2, it will still have 1 Play Snuke when he gets there, and he can buy one for 8 yen.
If he goes to Shop 3, Play Snuke will be out of stock when he gets there; he cannot buy one.


Sample Input 2

3
5 9 5
6 8 5
7 7 5

Sample Output 2

-1

Sample Input 3

10
158260522 877914575 602436426
24979445 861648772 623690081
433933447 476190629 262703497
211047202 971407775 628894325
731963982 822804784 450968417
430302156 982631932 161735902
880895728 923078537 707723857
189330739 910286918 802329211
404539679 303238506 317063340
492686568 773361868 125660016

Sample Output 3

861648772
C - Unexpressed

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

整数 N が与えられます。 1 以上 N 以下の整数のうち、 2 以上の整数 a, b を用いて a^b と表せないものはいくつあるでしょうか?

制約

  • N は整数
  • 1 ≤ N ≤ 10^{10}

入力

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

N

出力

答えを出力せよ。


入力例 1

8

出力例 1

6

4, 82^2 = 4,\ 2^3 = 8 と、a^b の形で表すことができます。
1, 2, 3, 5, 6, 72 以上の整数 a, b を用いて a^b と表せないので、答えは 6 です。


入力例 2

100000

出力例 2

99634

Score : 300 points

Problem Statement

Given is an integer N. How many integers between 1 and N (inclusive) are unrepresentable as a^b, where a and b are integers not less than 2?

Constraints

  • N is an integer.
  • 1 ≤ N ≤ 10^{10}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

8

Sample Output 1

6

4 and 8 are representable as a^b: we have 2^2 = 4 and 2^3 = 8.
On the other hand, 1, 2, 3, 5, 6, and 7 are unrepresentable as a^b using integers a and b not less than 2, so the answer is 6.


Sample Input 2

100000

Sample Output 2

99634
D - Poker

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1, 2, \dots, 9 が表に書かれたカードが K 枚ずつ、計 9K 枚のカードがあります。
これらのカードをランダムにシャッフルして、高橋くんと青木くんにそれぞれ、4 枚を表向きに、1 枚を裏向きにして配りました。
高橋くんに配られたカードが文字列 S として、青木くんに配られたカードが文字列 T として与えられます。
S, T5 文字の文字列で、先頭 4 文字は 1, 2, \dots, 9 からなり、表向きのカードに書かれた数を表します。 末尾 1 文字は # であり、裏向きのカードであることを表します。
5 枚の手札の点数を、c_i をその手札に含まれる i の枚数として、\displaystyle \sum_{i=1}^9 i \times 10^{c_i} で定義します。
高橋くんが青木くんより点数の高い手札を持っていたら高橋くんの勝ちです。
高橋くんの勝つ確率を求めてください。

制約

  • 2 ≤ K ≤ 10^5
  • |S| = |T| = 5
  • S, T1 文字目から 4 文字目は 1, 2, \dots, 9 のいずれか
  • 1, 2, \dots, 9 はそれぞれ、ST に合計 K 回までしか出現しない
  • S, T5 文字目は #

入力

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

K
S
T

出力

高橋くんの勝つ確率を小数で出力せよ。
想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解と判定される。


入力例 1

2
1144#
2233#

出力例 1

0.4444444444444444

例えば、高橋くんの手札が 11449 、青木くんの手札が 22338 の場合、高橋くんの点数は 100+2+3+400+5+6+7+8+90=621 、青木くんの点数は 1+200+300+4+5+6+7+80+9=612 で、高橋くんの勝ちになります。
裏向きのカードの大小によって勝敗が決まるので、高橋くんの勝つ確率は \frac49 です。


入力例 2

2
9988#
1122#

出力例 2

1.0

入力例 3

6
1122#
2228#

出力例 3

0.001932367149758454

高橋くんの手札が 11222 、青木くんの手札が 22281 の場合にのみ高橋くんの勝ちになります。
高橋くんの勝つ確率は \frac2{1035} です。


入力例 4

100000
3226#
3597#

出力例 4

0.6296297942426154

Score : 400 points

Problem Statement

We have 9K cards. For each i = 1, 2, \dots, 9, there are K cards with i written on it.
We randomly shuffled these cards and handed out five cards - four face up and one face down - to each of Takahashi and Aoki.
You are given a string S representing the cards handed out to Takahashi and a string T representing the cards handed out to Aoki.
S and T are strings of five characters each. Each of the first four characters of each string is 1, 2, \dots, or 9, representing the number written on the face-up card. The last character of each string is #, representing that the card is face down.
Let us define the score of a five-card hand as \displaystyle \sum_{i=1}^9 i \times 10^{c_i}, where c_i is the number of cards with i written on them.
Takahashi wins when the score of Takahashi's hand is higher than that of Aoki's hand.
Find the probability that Takahashi wins.

Constraints

  • 2 ≤ K ≤ 10^5
  • |S| = |T| = 5
  • The first through fourth characters of each of S and T are 1, 2, \dots, or 9.
  • Each of the digit 1, 2, \dots, and 9 appears at most K times in total in S and T.
  • The fifth character of each of S and T is #.

Input

Input is given from Standard Input in the following format:

K
S
T

Output

Print the probability that Takahashi wins, as a decimal. Your answer will be judged as correct when its absolute or relative error from our answer is at most 10^{-5}.


Sample Input 1

2
1144#
2233#

Sample Output 1

0.4444444444444444

For example, if Takahashi's hand is 11449 and Aoki's hand is 22338, Takahashi's score is 100+2+3+400+5+6+7+8+90=621 and Aoki's score is 1+200+300+4+5+6+7+80+9=612, resulting in Takahashi's win.
Takahashi wins when the number on his face-down card is greater than that of Aoki's face-down card, so Takahashi will win with probability \frac49.


Sample Input 2

2
9988#
1122#

Sample Output 2

1.0

Sample Input 3

6
1122#
2228#

Sample Output 3

0.001932367149758454

Takahashi wins only when Takahashi's hand is 11222 and Aoki's hand is 22281, with probability \frac2{1035}.


Sample Input 4

100000
3226#
3597#

Sample Output 4

0.6296297942426154
E - Oversleeping

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

A と街 B の間を往復する電車があります。 電車は時刻 0 に街 A を出発した後、

  • X 秒かけて街 B に移動
  • BY 秒停車
  • X 秒かけて街 A に移動
  • AY 秒停車

を繰り返します。
より厳密には、これらは半開区間として扱います。すなわち、n = 0, 1, 2, \dots について、

  • (2X + 2Y)n ≤ t < (2X + 2Y)n + X を満たす時刻 t には電車は街 B に移動している
  • (2X + 2Y)n + X ≤ t < (2X + 2Y)n + X + Y を満たす時刻 t には電車は街 B で停車している
  • (2X + 2Y)n + X + Y ≤ t < (2X + 2Y)n + 2X + Y を満たす時刻 t には電車は街 A に移動している
  • (2X + 2Y)n + 2X + Y ≤ t < (2X + 2Y)(n + 1) を満たす時刻 t には電車は街 A で停車している

が満たされます。

高橋くんは電車に乗って時刻 0 に街 A を出発し、街 B で電車を降りようと思っています。 高橋くんは時刻 0 に街 A を出発した後、

  • P 秒間眠っている
  • Q 秒間起きている

を繰り返します。
これらも半開区間として扱います。すなわち、n = 0, 1, 2, \dots について、

  • (P + Q)n ≤ t < (P + Q)n + P を満たす時刻 t には高橋くんは眠っている
  • (P + Q)n + P ≤ t < (P + Q)(n + 1) を満たす時刻 t には高橋くんは起きている

が満たされます。

B に電車が停車しており、かつ、高橋くんが起きていれば高橋くんは街 B で電車を降りることができます。
高橋くんが街 B で電車を降りることができるか判定し、できる場合は、最短でいつになるか求めてください。
なお、この値はこの問題の制約下で整数になることが証明できます。

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

制約

  • 入力は全て整数
  • 1 ≤ T ≤ 10
  • 1 ≤ X ≤ 10^9
  • 1 ≤ Y ≤ 500
  • 1 ≤ P ≤ 10^9
  • 1 ≤ Q ≤ 500

入力

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

T
\rm case_1
\rm case_2
\hspace{9pt}\vdots
\rm case_T

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

X Y P Q

出力

T 行出力せよ。
i 行目には、\rm case_i についてこの問題を解き、街 B で電車を降りられる時刻が存在する場合、そのような時刻のうち最小のものを整数で出力せよ。 街 B で電車を降りられる時刻が存在しない場合、infinity と出力せよ。


入力例 1

3
5 2 7 6
1 1 3 1
999999999 1 1000000000 1

出力例 1

20
infinity
1000000000999999999

[a, b) で区間 a ≤ t < b を表すことにします。

1 個目のケースでは、電車が街 B で停車している時刻は [5, 7), [19, 21), [33, 35), \dots 、 高橋くんが起きている時刻は [7, 13), [20, 26), [33, 39), \dots なので、時刻 20 に初めて街 B で電車を降りることが出来ます。

Score : 500 points

Problem Statement

A train goes back and forth between Town A and Town B. It departs Town A at time 0 and then repeats the following:

  • goes to Town B, taking X seconds;
  • stops at Town B for Y seconds;
  • goes to Town A, taking X seconds;
  • stops at Town A for Y seconds.

More formally, these intervals of time are half-open, that is, for each n = 0, 1, 2, \dots:

  • at time t such that (2X + 2Y)n ≤ t < (2X + 2Y)n + X, the train is going to Town B;
  • at time t such that (2X + 2Y)n + X ≤ t < (2X + 2Y)n + X + Y, the train is stopping at Town B;
  • at time t such that (2X + 2Y)n + X + Y ≤ t < (2X + 2Y)n + 2X + Y, the train is going to Town A;
  • at time t such that (2X + 2Y)n + 2X + Y ≤ t < (2X + 2Y)(n + 1), the train is stopping at Town A.

Takahashi is thinking of taking this train to depart Town A at time 0 and getting off at Town B. After the departure, he will repeat the following:

  • be asleep for P seconds;
  • be awake for Q seconds.

Again, these intervals of time are half-open, that is, for each n = 0, 1, 2, \dots:

  • at time t such that (P + Q)n ≤ t < (P + Q)n + P, Takahashi is asleep;
  • at time t such that (P + Q)n + P ≤ t < (P + Q)(n + 1), Takahashi is awake.

He can get off the train at Town B if it is stopping at Town B and he is awake.
Determine whether he can get off at Town B. If he can, find the earliest possible time to do so.
Under the constraints of this problem, it can be proved that the earliest time is an integer.

You are given T cases. Solve each of them.

Constraints

  • All values in input are integers.
  • 1 ≤ T ≤ 10
  • 1 ≤ X ≤ 10^9
  • 1 ≤ Y ≤ 500
  • 1 ≤ P ≤ 10^9
  • 1 ≤ Q ≤ 500

Input

Input is given from Standard Input in the following format:

T
\rm case_1
\rm case_2
\hspace{9pt}\vdots
\rm case_T

Each case is in the following format:

X Y P Q

Output

Print T lines.
The i-th line should contain the answer to \rm case_i.
If there exists a time such that Takahashi can get off the train at Town B, the line should contain the earliest such time; otherwise, the line should contain infinity.


Sample Input 1

3
5 2 7 6
1 1 3 1
999999999 1 1000000000 1

Sample Output 1

20
infinity
1000000000999999999

Let [a, b) denote the interval a ≤ t < b.

In the first case, the train stops at Town B during [5, 7), [19, 21), [33, 35), \dots, and Takahashi is awake during [7, 13), [20, 26), [33, 39), \dots, so he can get off at time 20 at the earliest.

F - Zebraness

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N マス、横 N マスのマス目があります。
上から i 行目、左から j 列目のマスをマス (i, j) と表すことにします。 マス (i, j) の色の情報が文字 c_{i,j} により与えられます。
B はマスが黒で塗られていることを、 W はマスが白で塗られていることを、 ? はマスにまだ色が塗られていないことを表します。

高橋くんは、まだ色が塗られていないマスをそれぞれ黒または白で塗り、白黒のマス目を作ります。
マス目の しまうま度 を、辺で接する黒マスと白マスの組の個数と定義します。
高橋くんが達成できるしまうま度の最大値を求めてください。

制約

  • 1 ≤ N ≤ 100
  • c_{i, j}B, W, ? のいずれか

入力

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

N
c_{1,1} \dots c_{1,N}
\hspace{20pt}\vdots
c_{N,1} \dots c_{N,N}

出力

答えを出力せよ。


入力例 1

2
BB
BW

出力例 1

2

辺で接する黒マスと白マスの組は、マス (1, 2) とマス (2, 2) 、マス (2, 1) とマス (2, 2)2 組あるので、しまうま度は 2 です。


入力例 2

3
BBB
BBB
W?W

出力例 2

4

マス (3, 2) を白で塗ったときのしまうま度は 3 、黒で塗ったときのしまうま度は 4 です。


入力例 3

5
?????
?????
?????
?????
?????

出力例 3

40

Score : 600 points

Problem Statement

We have a grid with N horizontal rows and N vertical columns.
Let (i, j) denote the square at the i-th row from the top and j-th column from the left. A character c_{i,j} describes the color of (i, j).
B means the square is painted black; W means the square is painted white; ? means the square is not yet painted.

Takahashi will complete the black-and-white grid by painting each unpainted square black or white.
Let the zebraness of the grid be the number of pairs of a black square and a white square sharing a side.
Find the maximum possible zebraness of the grid that Takahashi can achieve.

Constraints

  • 1 ≤ N ≤ 100
  • c_{i, j} is B, W, or ?.

Input

Input is given from Standard Input in the following format:

N
c_{1,1} \dots c_{1,N}
\hspace{20pt}\vdots
c_{N,1} \dots c_{N,N}

Output

Print the answer.


Sample Input 1

2
BB
BW

Sample Output 1

2

We have two pairs of a black square and a white square sharing a side: (1, 2), (2, 2) and (2, 1), (2, 2), so the zebraness of this grid is 2.


Sample Input 2

3
BBB
BBB
W?W

Sample Output 2

4

Painting (3, 2) white makes the zebraness 3, and painting it black makes the zebraness 4.


Sample Input 3

5
?????
?????
?????
?????
?????

Sample Output 3

40