A - camel Case

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

英大文字および英小文字からなる文字列 S が与えられます。
ここで、 S のうちちょうど 1 文字だけが英大文字であり、それ以外は全て英小文字です。
大文字が S の先頭から何文字目に登場するか出力してください。
ただし、S の先頭の文字を 1 文字目とします。

制約

  • S は英大文字および英小文字からなる長さ 2 以上 100 以下の文字列
  • S に大文字はちょうど 1 つ含まれる。

入力

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

S

出力

大文字が S の先頭から何文字目に登場するかを、整数で出力せよ。


入力例 1

aBc

出力例 1

2

aBc の先頭から 1 文字目は a , 2 文字目は B , 3 文字目は c であり、 このうち大文字は 2 文字目です。
よって、2 を出力します。


入力例 2

xxxxxxXxxx

出力例 2

7

S=xxxxxxXxxx の 7 文字目に、大文字である X が登場しています。よって、7 を出力します。


入力例 3

Zz

出力例 3

1

Score : 100 points

Problem Statement

You are given a string S consisting of uppercase and lowercase English letters.
Here, exactly one character of S is uppercase, and the others are all lowercase.
Find the integer x such that the x-th character of S is uppercase.
Here, the initial character of S is considered the 1-st one.

Constraints

  • S is a string of length between 2 and 100, inclusive, consisting of uppercase and lowercase English letters.
  • S has exactly one uppercase letter.

Input

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

S

Output

Print the integer x such that the x-th character of S is uppercase.


Sample Input 1

aBc

Sample Output 1

2

The 1-st character of aBc is a, the 2-nd is B, and the 3-rd is c; the 2-nd character is uppercase.
Thus, 2 should be printed.


Sample Input 2

xxxxxxXxxx

Sample Output 2

7

An uppercase letter X occurs as the 7-th character of S=xxxxxxXxxx, so 7 should be printed.


Sample Input 3

Zz

Sample Output 3

1
B - Cut

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N 枚からなるカードの山があり、上から i 枚目のカードには整数 A_i が書かれています。

山の下から K 枚のカードを取り出し、順序を保ったまま山の一番上に乗せました。

この操作後の山の上から順に、カードに書かれた整数を出力してください。

制約

  • 1 \leq K < N \leq 100
  • 1 \leq A_i \leq 100
  • 入力は全て整数

入力

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

N K
A_1 A_2 \ldots A_N

出力

操作後の山の上から i 枚目のカードに書かれた整数を B_i とする。B_1,B_2,\ldots,B_N をこの順に空白区切りで出力せよ。


入力例 1

5 3
1 2 3 4 5

出力例 1

3 4 5 1 2

最初、カードに書かれた整数は山の上から順に 1,2,3,4,5 です。

山の下から 3 枚のカードを取り出し、そのまま山の一番上に乗せたあと、カードに書かれた整数は山の上から順に 3,4,5,1,2 となります。


入力例 2

6 2
1 2 1 2 1 2

出力例 2

1 2 1 2 1 2

カードに書かれている整数は相異なるとは限りません。

Score : 100 points

Problem Statement

There is a stack of N cards, and the i-th card from the top has an integer A_i written on it.

You take K cards from the bottom of the stack and place them on top of the stack, maintaining their order.

Print the integers written on the cards from top to bottom after the operation.

Constraints

  • 1 \leq K < N \leq 100
  • 1 \leq A_i \leq 100
  • All input values are integers.

Input

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

N K
A_1 A_2 \ldots A_N

Output

Let B_i be the integer written on the i-th card from the top of the stack after the operation. Print B_1,B_2,\ldots,B_N in this order, separated by spaces.


Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

3 4 5 1 2

Initially, the integers written on the cards are 1,2,3,4,5 from top to bottom.

After taking three cards from the bottom of the stack and placing them on top, the integers written on the cards become 3,4,5,1,2 from top to bottom.


Sample Input 2

6 2
1 2 1 2 1 2

Sample Output 2

1 2 1 2 1 2

The integers written on the cards are not necessarily distinct.

C - Triangle (Easier)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1, \dots, N の番号が付けられており、i \, (1 \leq i \leq M) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。

以下の条件を全て満たす整数 a, b, c の組の総数を求めてください。

  • 1 \leq a \lt b \lt c \leq N
  • 頂点 a と頂点 b を結ぶ辺が存在する。
  • 頂点 b と頂点 c を結ぶ辺が存在する。
  • 頂点 c と頂点 a を結ぶ辺が存在する。

制約

  • 3 \leq N \leq 100
  • 1 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
  • (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
  • 入力は全て整数

入力

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

N M
U_1 V_1
\vdots
U_M V_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

2

(a, b, c) = (1, 4, 5), (2, 3, 5) が条件を満たします。


入力例 2

3 1
1 2

出力例 2

0

入力例 3

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

出力例 3

4

Score : 200 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1, \dots, N, and the i-th (1 \leq i \leq M) edge connects Vertex U_i and Vertex V_i.

Find the number of tuples of integers a, b, c that satisfy all of the following conditions:

  • 1 \leq a \lt b \lt c \leq N
  • There is an edge connecting Vertex a and Vertex b.
  • There is an edge connecting Vertex b and Vertex c.
  • There is an edge connecting Vertex c and Vertex a.

Constraints

  • 3 \leq N \leq 100
  • 1 \leq M \leq \frac{N(N - 1)}{2}
  • 1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)
  • (U_i, V_i) \neq (U_j, V_j) \, (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
U_1 V_1
\vdots
U_M V_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

2

(a, b, c) = (1, 4, 5), (2, 3, 5) satisfy the conditions.


Sample Input 2

3 1
1 2

Sample Output 2

0

Sample Input 3

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

Sample Output 3

4
D - chess960

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君は chess960 と呼ばれるゲームで遊んでいます。 高橋君はランダムに決めた初期配置が chess960 の条件を満たすか確認するプログラムを書くことにしました。

長さ 8 の文字列 S が与えられます。S には K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれます。 S が以下の条件を全て満たしているか判定してください。

  • S において左から x,y\ (x < y) 文字目が B であるとする。このとき x と y の偶奇が異なる。

  • K は 2 つの R の間にある。より形式的には、S において左から x,y\ (x < y) 文字目が R であり、 z 文字目が K であるとする。このとき、 x< z < y が成り立つ。

制約

  • S は 長さ 8 の文字列であり、K, Q がちょうど 1 文字ずつ、R, B, N がちょうど 2 文字ずつ含まれる。

入力

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

S

出力

S が条件を満たす場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

RNBQKBNR

出力例 1

Yes

B は左から 3 番目、6 番目にあり、3 と 6 は偶奇が異なります。 また、K は 2 つの R の間にあります。よって条件を満たします。


入力例 2

KRRBBNNQ

出力例 2

No

K は 2 つの R の間にありません。


入力例 3

BRKRBQNN

出力例 3

No

Score : 200 points

Problem Statement

Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960.

You are given a string S of length eight. S has exactly one K and Q, and exactly two R's, B's , and N's. Determine if S satisfies all of the following conditions.

  • Suppose that the x-th and y-th (x < y) characters from the left of S are B; then, x and y have different parities.

  • K is between two R's. More formally, suppose that the x-th and y-th (x < y) characters from the left of S are R and the z-th is K; then x< z < y.

Constraints

  • S is a string of length 8 that contains exactly one K and Q, and exactly two R's, B's , and N's.

Input

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

S

Output

Print Yes if S satisfies the conditions; print No otherwise.


Sample Input 1

RNBQKBNR

Sample Output 1

Yes

The 3-rd and 6-th characters are B, and 3 and 6 have different parities. Also, K is between the two R's. Thus, the conditions are fulfilled.


Sample Input 2

KRRBBNNQ

Sample Output 2

No

K is not between the two R's.


Sample Input 3

BRKRBQNN

Sample Output 3

No
E - Transportation Expenses

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

あるイベントには N 人が参加し、i 番目の人の交通費は A_i 円でした。

イベントの主催者である高橋くんは、交通費補助額の上限額 x を設定して、人 i には交通費補助額として \min(x,A_i) 円を支給することとしました。ここで x は非負整数である必要があります。

高橋くんの予算が M 円であり、N 人に渡す交通費補助額の総和を M 円以下にしたいとき、交通費補助額の上限額 x は最大でいくらにできますか?

ただし、交通費補助額の上限額を無限に大きくできる場合は代わりにそのことを報告してください。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq M \leq 2\times 10^{14}
  • 1\leq A_i \leq 10^9
  • 入力される数値は全て整数

入力

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

N M
A_1 A_2 \ldots A_{N}

出力

予算の条件を満たすときの交通費補助額の上限額 x の最大値を整数として出力せよ。

ただし、交通費補助額の上限額を無限に大きくできる場合は代わりに infinite と出力せよ。


入力例 1

4 8
1 3 2 4

出力例 1

2

交通費補助額の上限額を 2 円にすると、N 人に渡す交通費補助額の総和は \min(2,1) + \min(2,3) + \min(2,2) + \min(2,4) = 7 円となり、予算の 8 円以下となります。

交通費補助額の上限額を 3 円にすると、N 人に渡す交通費補助額の総和は \min(3,1) + \min(3,3) + \min(3,2) + \min(3,4) = 9 円となり、予算の 8 円を超えてしまいます。

よって、交通費補助額の上限額の最大値は 2 円となります。


入力例 2

3 20
5 3 2

出力例 2

infinite

交通費補助額の上限額を無限に大きくできます。


入力例 3

10 23
2 5 6 5 2 1 7 9 7 2

出力例 3

2

Score : 300 points

Problem Statement

There are N people participating in an event, and the transportation cost for the i-th person is A_i yen.

Takahashi, the organizer of the event, decided to set a maximum limit x for the transportation subsidy. The subsidy for person i will be \min(x, A_i) yen. Here, x must be a non-negative integer.

Given that Takahashi's budget is M yen, and he wants the total transportation subsidy for all N people to be at most M yen, what is the maximum possible value of the subsidy limit x?

If the subsidy limit can be made infinitely large, report that instead.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^{14}
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

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

N M
A_1 A_2 \ldots A_{N}

Output

Print the maximum value of the subsidy limit x that satisfies the budget condition, as an integer.

If the subsidy limit can be made infinitely large, print infinite instead.


Sample Input 1

4 8
1 3 2 4

Sample Output 1

2

If the subsidy limit is set to 2 yen, the total transportation subsidy for all N people is \min(2,1) + \min(2,3) + \min(2,2) + \min(2,4) = 7 yen, which is within the budget of 8 yen.

If the subsidy limit is set to 3 yen, the total transportation subsidy for all N people is \min(3,1) + \min(3,3) + \min(3,2) + \min(3,4) = 9 yen, which exceeds the budget of 8 yen.

Therefore, the maximum possible value of the subsidy limit is 2 yen.


Sample Input 2

3 20
5 3 2

Sample Output 2

infinite

The subsidy limit can be made infinitely large.


Sample Input 3

10 23
2 5 6 5 2 1 7 9 7 2

Sample Output 3

2
F - Perfect Standings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋くんは、プログラミングコンテストを主催することにしました。

コンテストは A 問題、B 問題、C 問題、D 問題、E 問題の 5 問からなり、それぞれの配点は a 点、b 点、c 点、d 点、e 点です。

コンテストには 31 人が参加し、全員が 1 問以上解きました。

より具体的には、文字列 ABCDE の空でない(連続するとは限らない)部分列すべてについて、その部分列を名前とする参加者が存在し、その参加者は名前に含まれる文字に対応する問題をすべて解き、それ以外の問題は解きませんでした。

例えば、A さんは A 問題のみを、BCE さんは B 問題、C 問題、E 問題を解きました。

参加者の名前を、取った点数が大きいほうから順に出力してください。 ただし、参加者が取った点数は、その参加者が解いた問題の配点の合計です。

ただし、同じ点数を獲得した参加者については、名前が辞書順で小さいほうを先に出力してください。

辞書順で小さいとは?

辞書順とは、一言で説明すると「単語が辞書に載っている順番」を意味します。

より厳密には、英大文字からなる相異なる文字列 S,T について、S が T より辞書順で小さいとは、以下の条件のどちらかが成り立つことを意味します。

  • S の長さ |S| が T の長さより短く、T の先頭 |S| 文字が S と一致する
  • ある整数 1\leq i\leq\min\lbrace|S|,|T|\rbrace が存在して、次の 2 つの条件を両方を満たす
    • 1\leq j\lt i を満たすすべての整数 j に対して S の j 文字目と T の j 文字目が等しい
    • S の i 文字目が T の i 文字目よりアルファベット順で小さい

例えば、S= AB ,T= ABC とすると、ひとつめの条件が成り立つため S は T より小さいです。 また、S= ABD ,T= ACD とすると、ふたつめの条件が i=2 で成り立つため S は T より小さいです。

制約

  • 100\leq a\leq b\leq c\leq d\leq e\leq 2718
  • 入力はすべて整数

入力

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

a b c d e

出力

31 行出力せよ。 i 行目 (1\leq i\leq 31) には、i 番目に高い得点を獲得した参加者の名前を出力せよ。 同じ得点を獲得した参加者については、それらのうち辞書順で小さい名前をもつ参加者を先に出力せよ。


入力例 1

400 500 600 700 800

出力例 1

ABCDE
BCDE
ACDE
ABDE
ABCE
ABCD
CDE
BDE
ADE
BCE
ACE
BCD
ABE
ACD
ABD
ABC
DE
CE
BE
CD
AE
BD
AD
BC
AC
AB
E
D
C
B
A

それぞれの参加者の得点は以下のようになります。

例えば、ADE さんと BCE さんは同じ得点を獲得していますが、ADE さんのほうが辞書順で小さい名前をもつため、ADE さんを先に出力してください。


入力例 2

800 800 900 900 1000

出力例 2

ABCDE
ACDE
BCDE
ABCE
ABDE
ABCD
CDE
ACE
ADE
BCE
BDE
ABE
ACD
BCD
ABC
ABD
CE
DE
AE
BE
CD
AC
AD
BC
BD
AB
E
C
D
A
B

入力例 3

128 256 512 1024 2048

出力例 3

ABCDE
BCDE
ACDE
CDE
ABDE
BDE
ADE
DE
ABCE
BCE
ACE
CE
ABE
BE
AE
E
ABCD
BCD
ACD
CD
ABD
BD
AD
D
ABC
BC
AC
C
AB
B
A

Score : 300 points

Problem Statement

Takahashi decided to hold a programming contest.

The contest consists of five problems: A, B, C, D, E, with scores a, b, c, d, e, respectively.

There are 31 participants, and all of them solved at least one problem.

More specifically, for every non-empty subsequence (not necessarily contiguous) of the string ABCDE, there is a participant named after that subsequence who solved the problems corresponding to the letters in their name and did not solve the other problems.

For example, participant A solved only problem A, and participant BCE solved problems B, C, and E.

Print the names of the participants in order of their obtained scores, from the largest to the smallest. The score obtained by a participant is the sum of the scores of the problems they solved.

If two participants obtained the same score, print the one whose name is lexicographically smaller first.

What does "lexicographically smaller" mean?

In short, "lexicographically smaller" refers to the order in which words would appear in a dictionary.

More precisely, for distinct strings S,T consisting of uppercase English letters, S is lexicographically smaller than T if either of the following conditions holds:

  • The length |S| of S is less than the length of T, and the first |S| characters of T match S.
  • There exists an integer 1\leq i\leq\min\{ |S|,|T|\} that satisfy both of the following two conditions:
    • For every integer j with 1\leq j\lt i, the j-th character of S equals the j-th character of T.
    • The i-th character of S is alphabetically smaller than the i-th character of T.

For example, if S= AB and T= ABC, the first condition holds, so S is lexicographically smaller than T. If S= ABD and T= ACD, the second condition holds for i=2, so S is lexicographically smaller than T.

Constraints

  • 100\leq a\leq b\leq c\leq d\leq e\leq 2718
  • All input values are integers.

Input

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

a b c d e

Output

Print 31 lines. The i-th line (1\leq i\leq 31) should contain the name of the participant who obtained the i-th highest score. If multiple participants have the same score, print them in lexicographical order.


Sample Input 1

400 500 600 700 800

Sample Output 1

ABCDE
BCDE
ACDE
ABDE
ABCE
ABCD
CDE
BDE
ADE
BCE
ACE
BCD
ABE
ACD
ABD
ABC
DE
CE
BE
CD
AE
BD
AD
BC
AC
AB
E
D
C
B
A

The score of each participant is as follows:

For example, ADE and BCE obtained the same score, and ADE is lexicographically smaller, so print ADE before BCE.


Sample Input 2

800 800 900 900 1000

Sample Output 2

ABCDE
ACDE
BCDE
ABCE
ABDE
ABCD
CDE
ACE
ADE
BCE
BDE
ABE
ACD
BCD
ABC
ABD
CE
DE
AE
BE
CD
AC
AD
BC
BD
AB
E
C
D
A
B

Sample Input 3

128 256 512 1024 2048

Sample Output 3

ABCDE
BCDE
ACDE
CDE
ABDE
BDE
ADE
DE
ABCE
BCE
ACE
CE
ABE
BE
AE
E
ABCD
BCD
ACD
CD
ABD
BD
AD
D
ABC
BC
AC
C
AB
B
A
G - Add One Edge

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

N_1+N_2 頂点 M 辺の無向グラフがあります。i=1,2,\ldots,M に対し、i 番目の辺は頂点 a_i と頂点 b_i を結びます。
また、以下を満たすことが保障されます。

  • 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • 頂点 1 と頂点 N_1+N_2 は非連結

次の操作をちょうど 1 回行います。

  • 1 \leq u \leq N_1 を満たす整数 u と N_1+1 \leq v \leq N_1+N_2 を満たす整数 v を選び、頂点 u と頂点 v を結ぶ辺を追加する

操作後のグラフにおいて、頂点 1 と頂点 N_1+N_2 は必ず連結であることが示せます。そこで、頂点 1 と頂点 N_1+N_2 を結ぶ経路の長さ(辺の本数)の最小値を d とします。

操作で追加する辺を適切に選んだ時にありえる d の最大値を求めてください。

連結とは? 無向グラフの頂点 u,v が連結であるとは、頂点 u と頂点 v を結ぶ経路が存在することをいいます。

制約

  • 1 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0 \leq M \leq 3 \times 10^5
  • 1 \leq a_i \leq b_i \leq N_1+N_2
  • i \neq j ならば (a_i,b_i) \neq (a_j,b_j)
  • 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • 頂点 1 と頂点 N_1+N_2 は非連結
  • 入力はすべて整数

入力

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

N_1 N_2 M
a_1 b_1
\vdots
a_M b_M

出力

答えを出力せよ。


入力例 1

3 4 6
1 2
2 3
4 5
4 6
1 3
6 7

出力例 1

5

u=2,v=5 として操作することで d=5 と出来ます。これが最大値です。


入力例 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

出力例 2

4

Score : 400 points

Problem Statement

We have an undirected graph with (N_1+N_2) vertices and M edges. For i=1,2,\ldots,M, the i-th edge connects vertex a_i and vertex b_i.
The following properties are guaranteed:

  • Vertex u and vertex v are connected, for all integers u and v with 1 \leq u,v \leq N_1.
  • Vertex u and vertex v are connected, for all integers u and v with N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 1 and vertex (N_1+N_2) are disconnected.

Consider performing the following operation exactly once:

  • choose an integer u with 1 \leq u \leq N_1 and an integer v with N_1+1 \leq v \leq N_1+N_2, and add an edge connecting vertex u and vertex v.

We can show that vertex 1 and vertex (N_1+N_2) are always connected in the resulting graph; so let d be the minimum length (number of edges) of a path between vertex 1 and vertex (N_1+N_2).

Find the maximum possible d resulting from adding an appropriate edge to add.

Definition of "connected" Two vertices u and v of an undirected graph are said to be connected if and only if there is a path between vertex u and vertex v.

Constraints

  • 1 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0 \leq M \leq 3 \times 10^5
  • 1 \leq a_i \leq b_i \leq N_1+N_2
  • (a_i,b_i) \neq (a_j,b_j) if i \neq j.
  • Vertex u and vertex v are connected for all integers u and v such that 1 \leq u,v \leq N_1.
  • Vertex u and vertex v are connected for all integers u and v such that N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 1 and vertex (N_1+N_2) are disconnected.
  • All input values are integers.

Input

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

N_1 N_2 M
a_1 b_1
\vdots
a_M b_M

Output

Print the answer.


Sample Input 1

3 4 6
1 2
2 3
4 5
4 6
1 3
6 7

Sample Output 1

5

If we set u=2 and v=5, the operation yields d=5, which is the maximum possible.


Sample Input 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

Sample Output 2

4
H - Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

K, E, Y のみからなる文字列 S が与えられます。

S の隣接する 2 文字を入れ替える操作を K 回まで行えるとき、作ることができる文字列は何種類ありますか?

制約

  • 2 \leq |S| \leq 30
  • 0 \leq K \leq 10^9
  • S は K, E, Y のみからなる

入力

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

S
K

出力

答えを出力せよ。


入力例 1

KEY
1

出力例 1

3

KEY に対して 1 回以下の操作を行うことで得られる文字列は KEY, EKY, KYE の 3 種類です。


入力例 2

KKEE
2

出力例 2

4

KKEE に対して 2 回以下の操作を行うことで得られる文字列は KKEE, KEKE, EKKE, KEEK の 4 種類です。


入力例 3

KKEEYY
1000000000

出力例 3

90

Score : 500 points

Problem Statement

Given is a string S consisting of K, E, Y.

How many strings are there that can be obtained with at most K swaps of two adjacent characters in S?

Constraints

  • 2 \leq |S| \leq 30
  • 0 \leq K \leq 10^9
  • S consists of K, E, Y.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the answer.


Sample Input 1

KEY
1

Sample Output 1

3

With at most one swap, there are three strings that can be obtained: KEY, EKY, KYE.


Sample Input 2

KKEE
2

Sample Output 2

4

With at most two swaps, there are four strings that can be obtained: KKEE, KEKE, EKKE, KEEK.


Sample Input 3

KKEEYY
1000000000

Sample Output 3

90
I - Black Jack

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550 点

問題文

あなたとディーラーでゲームをします。 ゲームは 1 以上 D 以下の整数が等確率で出る D 面サイコロ、0 で初期化された変数 x,y を用いて以下のように行われます。

  • あなたはサイコロを振り、出た目を x に加算する操作を好きな回数行える。ここで、あなたは操作を行うたびに次の操作を行うかを選択できる。

  • その後、ディーラーは y < L を満たす限り、サイコロを振り、出た目を y に加算する操作を繰り返す。

  • x > N の場合あなたの負けである。そうでない場合、y > N または x>y のいずれかを満たす場合あなたの勝ちで、どちらも満たさない場合あなたの負けである。

あなたが勝率を最大化するように適切に行動する際、勝率を求めてください。

制約

  • 入力は全て整数
  • 1\leq L\leq N\leq 2\times 10^5
  • 1\leq D \leq N

入力

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

N L D

出力

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


入力例 1

3 2 2

出力例 1

0.468750000000000

x が 2 以下の場合操作を続けるという戦略が最適であることが証明できます。


入力例 2

200000 200000 200000

出力例 2

0.999986408692793

Score: 550 points

Problem Statement

You will play a game against a dealer. The game uses a D-sided die (dice) that shows an integer from 1 to D with equal probability, and two variables x and y initialized to 0. The game proceeds as follows:

  • You may perform the following operation any number of times: roll the die and add the result to x. You can choose whether to continue rolling or not after each roll.

  • Then, the dealer will repeat the following operation as long as y < L: roll the die and add the result to y.

  • If x > N, you lose. Otherwise, you win if y > N or x > y and lose if neither is satisfied.

Determine the probability of your winning when you act in a way that maximizes the probability of winning.

Constraints

  • All inputs are integers.
  • 1 \leq L \leq N \leq 2 \times 10^5
  • 1 \leq D \leq N

Input

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

N L D

Output

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


Sample Input 1

3 2 2

Sample Output 1

0.468750000000000

It can be proved that the optimal strategy is to continue rolling as long as x is not greater than 2.


Sample Input 2

200000 200000 200000

Sample Output 2

0.999986408692793