C - Caesar Cipher

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

配点 : 200

問題文

高橋君は英小文字からなる文字列 S を持っています。

高橋君は文字列 S に対して、下記の操作をちょうど 1 回行います。

  • まず、非負整数 K を選ぶ。
  • その後、S の各文字を K 個後ろの英小文字に変更する。

ただし、

  • a1 個後ろの英小文字は b であり、
  • b1 個後ろの英小文字は c であり、
  • c1 個後ろの英小文字は d であり、
  • \cdots
  • y1 個後ろの英小文字は z であり、
  • z1 個後ろの英小文字は a です。

例えば、b4 個後ろの英小文字は f であり、y3 個後ろの英小文字は b です。

文字列 T が与えられます。 高橋君が上記の操作によって ST に一致させることができるかを判定してください。

制約

  • ST はそれぞれ英小文字からなる長さ 1 以上 10^5 以下の文字列
  • S の長さと T の長さは等しい

入力

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

S
T

出力

高橋君が ST に一致させることができる場合は Yes と出力し、 できない場合は No と出力せよ。


入力例 1

abc
ijk

出力例 1

Yes

高橋君が K=8 を選ぶと、

  • a8 個後ろの i
  • b8 個後ろの j
  • c8 個後ろの k

それぞれ変更され、ST が一致します。
高橋君が ST に一致させることができるため Yes と出力します。


入力例 2

z
a

出力例 2

Yes

高橋君が K=1 を選ぶと ST が一致します。
z1 個後ろの英小文字は a であることに注意してください。


入力例 3

ppq
qqp

出力例 3

No

高橋君は非負整数 K をどのように選んでも ST に一致させることができません。 よって、No と出力します。


入力例 4

atcoder
atcoder

出力例 4

Yes

高橋君が K=0 を選ぶと ST が一致します。

Score : 200 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

On this string, he will do the operation below just once.

  • First, choose a non-negative integer K.
  • Then, shift each character of S to the right by K (see below).

Here,

  • a shifted to the right by 1 is b;
  • b shifted to the right by 1 is c;
  • c shifted to the right by 1 is d;
  • \cdots
  • y shifted to the right by 1 is z;
  • z shifted to the right by 1 is a.

For example, b shifted to the right by 4 is f, and y shifted to the right by 3 is b.

You are given a string T. Determine whether Takahashi can make S equal T by the operation above.

Constraints

  • Each of S and T is a string of length between 1 and 10^5 (inclusive) consisting of lowercase English letters.
  • The lengths of S and T are equal.

Input

Input is given from Standard Input in the following format:

S
T

Output

If Takahashi can make S equal T, print Yes; if not, print No.


Sample Input 1

abc
ijk

Sample Output 1

Yes

When Takahashi chooses K=8,

  • a is shifted to the right by 8 and becomes i,
  • b is shifted to the right by 8 and becomes j,
  • c is shifted to the right by 8 and becomes k,

and now S and T are equal.
Therefore, he can make S equal T, so Yes should be printed.


Sample Input 2

z
a

Sample Output 2

Yes

Choosing K=1 makes S and T equal.
Note that the letter on the right of z is a.


Sample Input 3

ppq
qqp

Sample Output 3

No

There is no non-negative integer K that he can choose to make S equal T, so No should be printed.


Sample Input 4

atcoder
atcoder

Sample Output 4

Yes

Choosing K=0 makes S and T equal.

D - P(X or Y)

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

配点 : 250

問題文

1,2,3,4,5,66 種類の目が出るサイコロを 2 つ振ったときに、次の 2 つの条件の少なくとも一方を満たす確率を求めてください。

  • 2 つの出目の合計が X 以上である。
  • 2 つの出目の差の絶対値が Y 以上である。

ここで、どちらのサイコロについても 6 種類のどの目が出るかは同様に確からしく、それぞれのサイコロの出目は独立であるとします。

制約

  • 2\leq X\leq13
  • 0\leq Y\leq6
  • 入力はすべて整数

入力

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

X Y

出力

2 つのサイコロの出目が 2 つの条件の少なくとも一方を満たす確率を出力せよ。 出力された値と真の値との絶対誤差が 10 ^ {-9} 以下のとき、正答と判定される。


入力例 1

9 3

出力例 1

0.555555555555555555555555555555

2 つのサイコロの出目がそれぞれ xy であることを (x,y) で表すことにすると、それぞれの条件を満たすのは次の場合です。

  • 2 つのサイコロの出目の和が 9 以上になるのは、(3,6),(4,5),(4,6),(5,4),(5,5),(5,6),(6,3),(6,4),(6,5),(6,6) のとき
  • 2 つのサイコロの出目の差が 3 以上になるのは、(1,4),(1,5),(1,6),(2,5),(2,6),(3,6),(4,1),(5,1),(5,2),(6,1),(6,2),(6,3) のとき

これらの条件の少なくとも一方を満たすのは (1,4),(1,5),(1,6),(2,5),(2,6),(3,6),(4,1),(4,5),(4,6),(5,1),(5,2),(5,4),(5,5),(5,6),(6,1),(6,2),(6,3),(6,4),(6,5),(6,6)20 通りのいずれかのときです。

よって、求める答えは \dfrac{20}{36}=\dfrac59=0.5555555555\ldots です。

絶対誤差が 10 ^ {-9} 以下のとき正答と判定されるので、0.55555555650.55555555456789 などと出力しても正解となります。


入力例 2

13 6

出力例 2

0

2 つのサイコロの出目の和が 13 以上になることも、差が 6 以上になることもありません。

よって、求める答えは 0 です。


入力例 3

10 3

出力例 3

0.5

Score : 250 points

Problem Statement

Two dice, each with six faces 1,2,3,4,5,6, are rolled. Find the probability that at least one of the following two conditions holds:

  • The sum of the two outcomes is at least X.
  • The absolute difference of the two outcomes is at least Y.

Each face of each die is equally likely, and the two dice are independent.

Constraints

  • 2 \le X \le 13
  • 0 \le Y \le 6
  • All input values are integers.

Input

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

X Y

Output

Output the probability that the two outcomes satisfy at least one of the two conditions. Your answer is accepted if its absolute error from the true value is at most 10^{-9}.


Sample Input 1

9 3

Sample Output 1

0.555555555555555555555555555555

Let (x,y) denote the event that the dice show x and y.

  • The sum is at least 9 for (3,6),(4,5),(4,6),(5,4),(5,5),(5,6),(6,3),(6,4),(6,5),(6,6).
  • The difference is at least 3 for (1,4),(1,5),(1,6),(2,5),(2,6),(3,6),(4,1),(5,1),(5,2),(6,1),(6,2),(6,3).

At least one of these conditions holds for the following 20 pairs: (1,4),(1,5),(1,6),(2,5),(2,6),(3,6),(4,1),(4,5),(4,6),(5,1),(5,2),(5,4),(5,5),(5,6),(6,1),(6,2),(6,3),(6,4),(6,5),(6,6).

Thus, the answer is \dfrac{20}{36}=\dfrac59=0.5555555555\ldots.

Because an absolute error of at most 10^{-9} is allowed, outputs such as 0.5555555565 or 0.55555555456789 are accepted.


Sample Input 2

13 6

Sample Output 2

0

Neither the sum of two dice can be 13 or greater, nor can their difference be 6 or greater.

Thus, the answer is 0.


Sample Input 3

10 3

Sample Output 3

0.5
E - Make Them Narrow

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

配点 : 250

問題文

長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le K < N \le 2 \times 10^5
  • 1 \le A_i \le 10^9

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

5 2
3 1 5 4 9

出力例 1

2

A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。

  • 例えば 2 要素目の 15 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
    • このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。

入力例 2

6 5
1 1 1 1 1 1

出力例 2

0

入力例 3

8 3
31 43 26 6 18 36 22 13

出力例 3

18

Score : 250 points

Problem Statement

You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.

Constraints

  • All inputs are integers.
  • 1 \le K < N \le 2 \times 10^5
  • 1 \le A_i \le 10^9

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5 2
3 1 5 4 9

Sample Output 1

2

Consider removing exactly two elements from A=(3,1,5,4,9).

  • For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
    • In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.

Sample Input 2

6 5
1 1 1 1 1 1

Sample Output 2

0

Sample Input 3

8 3
31 43 26 6 18 36 22 13

Sample Output 3

18
F - String Delimiter

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

配点 : 300

問題文

英小文字、," からなる長さ N の文字列 S が与えられます。S に含まれる " の個数は偶数であることが保証されています。

S に含まれる " の個数を 2K 個とすると、各 i=1,2,\ldots,K について 2i-1 番目の " から 2i 番目の " までの文字のことを 括られた文字 と呼びます。

あなたの仕事は、 S に含まれる , のうち、括られた文字 でないもの. で置き換えて得られる文字列を答えることです。

制約

  • N1 以上 2\times 10^5 以下の整数
  • S は英小文字、," からなる長さ N の文字列
  • S に含まれる " の個数は偶数

入力

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

N
S

出力

答えを出力せよ。


入力例 1

8
"a,b"c,d

出力例 1

"a,b"c.d

S のうち "a,b" が括られた文字であり、c,d は括られた文字ではありません。

S に含まれる , のうち、括られた文字でないのは S の左から 7 番目の文字なので、7 番目の文字を . で置き換えたものが答えとなります。


入力例 2

5
,,,,,

出力例 2

.....

入力例 3

20
a,"t,"c,"o,"d,"e,"r,

出力例 3

a."t,"c."o,"d."e,"r.

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, ,, and ". It is guaranteed that S contains an even number of ".

Let 2K be the number of " in S. For each i=1,2,\ldots,K, the characters from the (2i-1)-th " through the (2i)-th " are said to be enclosed.

Your task is to replace each , in S that is not an enclosed character with . and print the resulting string.

Constraints

  • N is an integer between 1 and 2\times 10^5, inclusive.
  • S is a string of length N consisting of lowercase English letters, ,, and ".
  • S contains an even number of ".

Input

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

N
S

Output

Print the answer.


Sample Input 1

8
"a,b"c,d

Sample Output 1

"a,b"c.d

In S, "a,b" are enclosed characters, and c,d are not.

The , in S that is not an enclosed character is the seventh character from the left in S, so replace that character with . to get the answer.


Sample Input 2

5
,,,,,

Sample Output 2

.....

Sample Input 3

20
a,"t,"c,"o,"d,"e,"r,

Sample Output 3

a."t,"c."o,"d."e,"r.
G - Freefall

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

配点 : 400

問題文

スーパーマンである高橋くんは、地上で困っている人を助けるため、あるビルの屋上から飛び降りようとしています。 高橋くんがいる星には重力の大きさを表す g という値が定まっており、 高橋くんが落下を開始してから地面に到達するまでにかかる時間は \frac{A}{\sqrt{g}} です。

現在の時刻は 0 であり、g = 1 が成り立ちます。 高橋くんは、今から次の操作を好きな回数(0 回でもよい)行います。

  • 超能力により g の値を 1 増やす。時間が B 経過する。

その後、高橋くんはビルから飛び降ります。落下を開始した後は g の値を変えることはできません。 また、操作によって経過する時間と落下にかかる時間以外は考えないものとします。

高橋くんが地面に到達できる最も早い時刻を求めてください。

制約

  • 1 \leq A \leq 10^{18}
  • 1 \leq B \leq 10^{18}
  • 入力は全て整数

入力

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

A B

出力

高橋くんが地面に到達できる最も早い時刻を出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

10 1

出力例 1

7.7735026919
  • 操作を 0 回行うとき、地面に到達する時刻は 1\times 0+\frac{10}{\sqrt{1}} = 10 です。
  • 操作を 1 回行うとき、地面に到達する時刻は 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07 です。
  • 操作を 2 回行うとき、地面に到達する時刻は 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77 です。
  • 操作を 3 回行うとき、地面に到達する時刻は 1\times 3+\frac{10}{\sqrt{4}} = 8 です。

操作を 4 回以上行っても、地面への到達時刻は遅くなるのみです。 よって、操作を 2 回行ってから飛び降りるのが最適で、答えは 2+\frac{10}{\sqrt{3}} です。


入力例 2

5 10

出力例 2

5.0000000000

操作を 1 回も行わないのが最適です。


入力例 3

1000000000000000000 100

出力例 3

8772053214538.5976562500

Score : 400 points

Problem Statement

A superman, Takahashi, is about to jump off the roof of a building to help a person in trouble on the ground. Takahashi's planet has a constant value g that represents the strength of gravity, and the time it takes for him to reach the ground after starting to fall is \frac{A}{\sqrt{g}}.

It is now time 0, and g = 1. Takahashi will perform the following operation as many times as he wants (possibly zero).

  • Use a superpower to increase the value of g by 1. This takes a time of B.

Then, he will jump off the building. After starting to fall, he cannot change the value of g. Additionally, we only consider the time it takes to perform the operation and fall.

Find the earliest time Takahashi can reach the ground.

Constraints

  • 1 \leq A \leq 10^{18}
  • 1 \leq B \leq 10^{18}
  • All values in the input are integers.

Input

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

A B

Output

Print the earliest time Takahashi can reach the ground. Your output will be accepted when its absolute or relative error from the true value is at most 10^{-6}.


Sample Input 1

10 1

Sample Output 1

7.7735026919
  • If he performs the operation zero times, he will reach the ground at time 1\times 0+\frac{10}{\sqrt{1}} = 10.
  • If he performs the operation once, he will reach the ground at time 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07.
  • If he performs the operation twice, he will reach the ground at time 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77.
  • If he performs the operation three times, he will reach the ground at time 1\times 3+\frac{10}{\sqrt{4}} = 8.

Performing the operation four or more times will only delay the time to reach the ground. Therefore, it is optimal to perform the operation twice before jumping off, and the answer is 2+\frac{10}{\sqrt{3}}.


Sample Input 2

5 10

Sample Output 2

5.0000000000

It is optimal not to perform the operation at all.


Sample Input 3

1000000000000000000 100

Sample Output 3

8772053214538.5976562500