E - Divide and Divide

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

黒板に整数 N1 個書かれています。
高橋君は黒板に書かれている 2 以上の整数が全て無くなるまで以下の一連の操作を繰り返します。

  • 黒板に書かれている 2 以上の整数を 1 つ選び x とする。
  • 黒板から x1 個消す。そして、2 個の整数 \left \lfloor \dfrac{x}{2} \right\rfloor, \left\lceil \dfrac{x}{2} \right\rceil を新たに黒板に書く。
  • この一連の操作を行うために高橋君は x 円払う必要がある。

ここで \lfloor a \rfloora 以下の整数のうち最大のものを、\lceil a \rceila 以上の整数のうち最小のものを意味します。

操作を行えなくなった時点で高橋君が払った金額の総和は何円ですか?
なお、どのような順番で操作を行っても高橋君が払う金額の総和は一定であることが証明できます。

制約

  • 2 \leq N \leq 10^{17}

入力

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

N

出力

高橋君が払った金額の総和が何円であるかを出力せよ。


入力例 1

3

出力例 1

5

高橋君が行う操作の一例を挙げると次のようになります。

  • はじめ、黒板には 31 個書かれている。
  • 高橋君は 3 を選ぶ。高橋君は 3 円を払い、黒板から 31 個消して \left \lfloor \dfrac{3}{2} \right\rfloor = 1, \left\lceil \dfrac{3}{2} \right\rceil = 2 を新たに黒板に書く。
  • 黒板には 21 個と 11 個書かれている。
  • 高橋君は 2 を選ぶ。高橋君は 2 円を払い、黒板から 21 個消して \left \lfloor \dfrac{2}{2} \right\rfloor = 1, \left\lceil \dfrac{2}{2} \right\rceil = 1 を新たに黒板に書く。
  • 黒板には 13 個書かれている。
  • 黒板から 2 以上の整数が全て無くなったので操作を終了する。

操作全体で高橋君は 3 + 2 = 5 円払ったので、5 を出力します。


入力例 2

340

出力例 2

2888

入力例 3

100000000000000000

出力例 3

5655884811924144128

Score: 300 points

Problem Statement

There is a single integer N written on a blackboard.
Takahashi will repeat the following series of operations until all integers not less than 2 are removed from the blackboard:

  • Choose one integer x not less than 2 written on the blackboard.
  • Erase one occurrence of x from the blackboard. Then, write two new integers \left \lfloor \dfrac{x}{2} \right\rfloor and \left\lceil \dfrac{x}{2} \right\rceil on the blackboard.
  • Takahashi must pay x yen to perform this series of operations.

Here, \lfloor a \rfloor denotes the largest integer not greater than a, and \lceil a \rceil denotes the smallest integer not less than a.

What is the total amount of money Takahashi will have paid when no more operations can be performed?
It can be proved that the total amount he will pay is constant regardless of the order in which the operations are performed.

Constraints

  • 2 \leq N \leq 10^{17}

Input

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

N

Output

Print the total amount of money Takahashi will have paid, in yen.


Sample Input 1

3

Sample Output 1

5

Here is an example of how Takahashi performs the operations:

  • Initially, there is one 3 written on the blackboard.
  • He chooses 3. He pays 3 yen, erases one 3 from the blackboard, and writes \left \lfloor \dfrac{3}{2} \right\rfloor = 1 and \left\lceil \dfrac{3}{2} \right\rceil = 2 on the blackboard.
  • There is one 2 and one 1 written on the blackboard.
  • He chooses 2. He pays 2 yen, erases one 2 from the blackboard, and writes \left \lfloor \dfrac{2}{2} \right\rfloor = 1 and \left\lceil \dfrac{2}{2} \right\rceil = 1 on the blackboard.
  • There are three 1s written on the blackboard.
  • Since all integers not less than 2 have been removed from the blackboard, the process is finished.

Takahashi has paid a total of 3 + 2 = 5 yen for the entire process, so print 5.


Sample Input 2

340

Sample Output 2

2888

Sample Input 3

100000000000000000

Sample Output 3

5655884811924144128
F - Rotation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。

以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。

  • 1 x: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。
  • 2 x: Sx 番目の文字を出力する。

制約

  • 2 \le N \le 5 \times 10^5
  • 1 \le Q \le 5 \times 10^5
  • 1 \le x \le N
  • |S|=N
  • S は英小文字からなる。
  • 2 x の形式のクエリが 1 個以上与えられる。
  • N,Q,x はすべて整数。

入力

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

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

それぞれのクエリは以下の形式で与えられる。ここで、t1 または 2 である。

t x

出力

2 x の形式の各クエリについて、答えを一行に出力せよ。


入力例 1

3 3
abc
2 2
1 1
2 2

出力例 1

b
a

1 個目のクエリのとき、Sabc なので 2 文字目の b を出力します。 2 個目のクエリのとき、Sabc から cab に変わります。 3 個目のクエリのとき、Scab なので 2 文字目の a を出力します。


入力例 2

10 8
dsuccxulnl
2 4
2 7
1 2
2 7
1 1
1 2
1 3
2 5

出力例 2

c
u
c
u

Score : 300 points

Problem Statement

You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.

Process Q queries. Each query is of one of the following two types.

  • 1 x: Perform the following x times in a row: delete the last character of S and append it to the beginning.
  • 2 x: Print the x-th character of S.

Constraints

  • 2 \le N \le 5 \times 10^5
  • 1 \le Q \le 5 \times 10^5
  • 1 \le x \le N
  • |S|=N
  • S consists of lowercase English letters.
  • At least one query in the format 2 x.
  • N, Q, x are all integers.

Input

Input is given from Standard Input in the following format:

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

Each query is in the following format, where t is 1 or 2:

t x

Output

For each query in the format 2 x, print the answer in a single line.


Sample Input 1

3 3
abc
2 2
1 1
2 2

Sample Output 1

b
a

In the 1-st query, S is abc, so the 2-nd character b should be printed. In the 2-nd query, S is changed from abc to cab. In the 3-rd query, S is cab, so the 2-nd character a should be printed.


Sample Input 2

10 8
dsuccxulnl
2 4
2 7
1 2
2 7
1 1
1 2
1 3
2 5

Sample Output 2

c
u
c
u
G - Mismatched Parentheses

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

英小文字および (, ) からなる長さ N の文字列 S が与えられます。
以下の操作を可能な限り繰り返したあとの S を出力してください。

  • S の連続部分文字列であって、最初の文字が ( かつ 最後の文字が ) かつ 最初と最後以外に () も含まないものを自由に 1 つ選び削除する

なお、操作を可能な限り繰り返したあとの S は操作の手順によらず一意に定まることが証明できます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • N は整数である
  • S は英小文字および (, ) からなる長さ N の文字列である

入力

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

N
S

出力

答えを出力せよ。


入力例 1

8
a(b(d))c

出力例 1

ac

例えば、以下の手順により操作後の Sac となります。

  • S4 文字目から 6 文字目までからなる部分文字列 (d) を削除する。Sa(b)c となる。
  • S2 文字目から 4 文字目までからなる部分文字列 (b) を削除する。Sac となる。
  • これ以上操作を行うことはできない。

入力例 2

5
a(b)(

出力例 2

a(

入力例 3

2
()

出力例 3


操作後の S は空文字列になる可能性があります。


入力例 4

6
)))(((

出力例 4

)))(((

Score : 400 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters and the characters ( and ).
Print the string S after performing the following operation as many times as possible.

  • Choose and delete a contiguous substring of S that starts with (, ends with ), and does not contain ( or ) other than the first and last characters.

It can be proved that the string S after performing the operation as many times as possible is uniquely determined without depending on how it is performed.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • N is an integer.
  • S is a string of length N consisting of lowercase English letters and the characters ( and ).

Input

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

N
S

Output

Print the answer.


Sample Input 1

8
a(b(d))c

Sample Output 1

ac

Here is one possible procedure, after which S will be ac.

  • Delete the substring (d) formed by the fourth to sixth characters of S, making it a(b)c.
  • Delete the substring (b) formed by the second to fourth characters of S, making it ac.
  • The operation can no longer be performed.

Sample Input 2

5
a(b)(

Sample Output 2

a(

Sample Input 3

2
()

Sample Output 3


The string S after the procedure may be empty.


Sample Input 4

6
)))(((

Sample Output 4

)))(((
H - Toward 0

Time Limit: 2 sec / Memory Limit: 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 \rfloors 以下の最大の整数を表します。例えば \lfloor 3 \rfloor=3\lfloor 2.5 \rfloor=2 です。

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

制約

  • 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 - Substring of Sorted String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

英小文字からなる長さ N の文字列 SQ 個のクエリが与えられます。クエリを順に処理してください。

クエリは以下の 2 種類です。

  • 1 x cSx 文字目を文字 c に置き換える
  • 2 l rS を文字の昇順に並び替えて得られる文字列を T とする。Sl 文字目から r 文字目までからなる文字列が T の部分文字列であるとき Yes、部分文字列でないとき No を出力する
部分文字列とは? S部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。 例えば、ababc の部分文字列ですが、acabc の部分文字列ではありません。

制約

  • 1\leq N \leq 10^5
  • S は英小文字からなる長さ N の文字列
  • 1 \leq Q \leq 10^5
  • 1 種類目のクエリにおいて、1 \leq x \leq N
  • 1 種類目のクエリにおいて、c は英小文字
  • 2 種類目のクエリにおいて、1 \leq l \leq r \leq N

入力

入力は以下の形式で標準入力から与えられる。ただし、\text{query}_ii 番目のクエリを表す。

N
S
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

出力

問題文中の指示に従ってクエリを処理せよ。


入力例 1

6
abcdcf
4
2 1 3
2 2 6
1 5 e
2 2 6

出力例 1

Yes
No
Yes
  • 1 番目のクエリにおいて、S を文字の昇順に並び替えて得られる文字列 Tabccdf です。 S1 文字目から 3 文字目までからなる文字列は abc であり T の部分文字列です。よって Yes を出力します。
  • 2 番目のクエリにおいて、S を文字の昇順に並び替えて得られる文字列 Tabccdf です。 S2 文字目から 6 文字目までからなる文字列は bcdcf であり T の部分文字列ではありません。よって No を出力します。
  • 3 番目のクエリにより、S5 文字目が e に置き換えられ、Sabcdef となります。
  • 4 番目のクエリにおいて、S を文字の昇順に並び替えて得られる文字列 Tabcdef です。 S2 文字目から 6 文字目までからなる文字列は bcdef であり T の部分文字列です。よって Yes を出力します。

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and Q queries. Process the queries in order.

Each query is of one of the following two kinds:

  • 1 x c : replace the x-th character of S by the character c.
  • 2 l r : let T be the string obtained by sorting the characters of S in ascending order. Print Yes if the string consisting of the l-th through r-th characters of S is a substring of T; print No otherwise.
What is a substring? A substring of S is a string obtained by removing 0 or more initial characters and 0 or more final characters of S. For example, ab is a substring of abc, while ac is not a substring of abc.

Constraints

  • 1\leq N \leq 10^5
  • S is a string of length N consisting of lowercase English letters.
  • 1 \leq Q \leq 10^5
  • For each query of the first kind, 1 \leq x \leq N.
  • For each query of the first kind, c is a lowercase English letter.
  • For each query of the second kind, 1 \leq l \leq r \leq N.

Input

The input is given from Standard Input in the following format, where \text{query}_i denotes the i-th query:

N
S
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Output

Process the queries as instructed in the Problem Statement.


Sample Input 1

6
abcdcf
4
2 1 3
2 2 6
1 5 e
2 2 6

Sample Output 1

Yes
No
Yes
  • In the 1-st query, abccdf is the string T obtained by sorting the characters of S in ascending order. The string abc, consisting of the 1-st through 3-rd characters of S, is a substring of T, so Yes should be printed.
  • In the 2-nd query, abccdf is the string T obtained by sorting the characters of S in ascending order. The string bcdcf, consisting of the 2-nd through 6-th characters of S, is not a substring of T, so No should be printed.
  • The 3-rd query sets the 5-th character of S to e, making S abcdef.
  • In the 4-th query, abcdef is the string T obtained by sorting the characters of S in ascending order. The string bcdef, consisting of the 2-nd through 6-th characters of S, is a substring of T, so Yes should be printed.