E - Error Correction

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

配点 : 300

問題文

高橋君は英小文字からなる文字列 T を青木君に向けて送りました。その結果、青木君は英小文字からなる文字列 T' を受信しました。

T'T から一部が変更されてしまっている可能性があり、具体的には、下記の 4 つのうちのちょうど 1 つが成り立つことがわかっています。

  • T' は、T と等しい。
  • T' は、T のいずれか 1 つの位置(先頭と末尾も含む)に英小文字を 1 つ挿入して得られる文字列である。
  • T' は、T からある 1 文字を削除して得られる文字列である。
  • T' は、T のある 1 文字を別の英小文字に変更して得られる文字列である。

青木君が受信した文字列 T' と、英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N が入力として与えられるので、 S_1, S_2, \ldots, S_N のうち、高橋君が送った文字列 T と等しい可能性があるものをすべて求めてください。

制約

  • N は整数
  • 1 \leq N \leq 5 \times 10^5
  • S_iT' は英小文字からなる長さ 1 以上 5 \times 10^5 以下の文字列
  • S_1, S_2, \ldots, S_N の長さの総和は 5 \times 10^5 以下

入力

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

N T'
S_1
S_2
\vdots
S_N

出力

S_1, S_2, \ldots, S_N のうち T と等しい可能性があるものすべての添字を昇順に並べた列を (i_1, i_2, \ldots, i_K) とする。 この列の長さ K および列自体を、下記の形式にしたがって出力せよ。

K
i_1 i_2 \ldots i_K

入力例 1

5 ababc
ababc
babc
abacbc
abdbc
abbac

出力例 1

4
1 2 3 4

S_1, S_2, \ldots, S_5 のうち、T と等しい可能性があるものは S_1, S_2, S_3, S_44 つであることが下記の通りわかります。

  • S_1T と等しい可能性があります。なぜなら、T' = ababcS_1 = ababc と等しいからです。
  • S_2T と等しい可能性があります。なぜなら、T' = ababcS_2 = babc の先頭に文字 a を挿入して得られる文字列だからです。
  • S_3T と等しい可能性があります。なぜなら、T' = ababcS_3 = abacbc から 4 文字目の c を削除して得られる文字列だからです。
  • S_4T と等しい可能性があります。なぜなら、T' = ababcS_4 = abdbc3 文字目の db に変更して得られる文字列だからです。
  • S_5T と等しい可能性はありません。なぜなら、S_5 = abbacT としたとき、T' = ababc は問題文中の 4 つの条件をいずれも満たさないからです。

入力例 2

1 aoki
takahashi

出力例 2

0


入力例 3

9 atcoder
atoder
atcode
athqcoder
atcoder
tacoder
jttcoder
atoder
atceoder
atcoer

出力例 3

6
1 2 4 7 8 9

Score : 300 points

Problem Statement

Takahashi sent a string T consisting of lowercase English letters to Aoki. As a result, Aoki received a string T' consisting of lowercase English letters.

T' may have been altered from T. Specifically, exactly one of the following four conditions is known to hold.

  • T' is equal to T.
  • T' is a string obtained by inserting one lowercase English letter at one position (possibly the beginning and end) in T.
  • T' is a string obtained by deleting one character from T.
  • T' is a string obtained by changing one character in T to another lowercase English letter.

You are given the string T' received by Aoki and N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters. Find all the strings among S_1, S_2, \ldots, S_N that could equal the string T sent by Takahashi.

Constraints

  • N is an integer.
  • 1 \leq N \leq 5 \times 10^5
  • S_i and T' are strings of length between 1 and 5 \times 10^5, inclusive, consisting of lowercase English letters.
  • The total length of S_1, S_2, \ldots, S_N is at most 5 \times 10^5.

Input

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

N T'
S_1
S_2
\vdots
S_N

Output

Let (i_1, i_2, \ldots, i_K) be the sequence of indices of all the strings among S_1, S_2, \ldots, S_N that could be equal to T, in ascending order. Print the length K of this sequence, and the sequence itself, in the following format:

K
i_1 i_2 \ldots i_K

Sample Input 1

5 ababc
ababc
babc
abacbc
abdbc
abbac

Sample Output 1

4
1 2 3 4

Among S_1, S_2, \ldots, S_5, the strings that could be equal to T are S_1, S_2, S_3, S_4, as explained below.

  • S_1 could be equal to T, because T' = ababc is equal to S_1 = ababc.
  • S_2 could be equal to T, because T' = ababc is obtained by inserting the letter a at the beginning of S_2 = babc.
  • S_3 could be equal to T, because T' = ababc is obtained by deleting the fourth character c from S_3 = abacbc.
  • S_4 could be equal to T, because T' = ababc is obtained by changing the third character d in S_4 = abdbc to b.
  • S_5 could not be equal to T, because if we take S_5 = abbac as T, then T' = ababc does not satisfy any of the four conditions in the problem statement.

Sample Input 2

1 aoki
takahashi

Sample Output 2

0


Sample Input 3

9 atcoder
atoder
atcode
athqcoder
atcoder
tacoder
jttcoder
atoder
atceoder
atcoer

Sample Output 3

6
1 2 4 7 8 9
F - Changing Jewels

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

配点 : 300

問題文

高橋君はレベル N の赤い宝石を 1 個持っています。(他に宝石は持っていません。)
高橋君は次の操作を好きなだけ行うことができます。

  • レベル n の赤い宝石 (n2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n の青い宝石 X 個」に変換する。
  • レベル n の青い宝石 (n2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n-1 の青い宝石 Y 個」に変換する。

高橋君はレベル 1 の青い宝石ができるだけたくさんほしいです。操作によって高橋君はレベル 1 の青い宝石を最大何個入手できますか?

制約

  • 1 \leq N \leq 10
  • 1 \leq X \leq 5
  • 1 \leq Y \leq 5
  • 入力される値はすべて整数

入力

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

N X Y

出力

答えを出力せよ。


入力例 1

2 3 4

出力例 1

12

次のような変換を行うことで、高橋君はレベル 1 の青い宝石を 12 個手に入れることができます。

  • まず、レベル 2 の赤い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個に変換します。
    • 操作後の高橋君は、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個を持っています。
  • 次に、レベル 2 の青い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 1 の青い宝石 4 個に変換します。この変換を 3 回繰り返します。
    • 操作後の高橋君は、レベル 1 の赤い宝石 4 個とレベル 1 の青い宝石 12 個を持っています。
  • これ以上変換を行うことはできません。

12 個より多くレベル 1 の青い宝石を手に入れることはできないので、答えは 12 になります。


入力例 2

1 5 5

出力例 2

0

高橋君がレベル 1 の青い宝石を 1 個も手に入れられない場合もあります。


入力例 3

10 5 5

出力例 3

3942349900

答えが 32 bit 整数に収まらない場合があることに注意してください。

Score : 300 points

Problem Statement

Takahashi has a red jewel of level N. (He has no other jewels.)
Takahashi can do the following operations any number of times.

  • Convert a red jewel of level n (n is at least 2) into "a red jewel of level (n-1) and X blue jewels of level n".
  • Convert a blue jewel of level n (n is at least 2) into "a red jewel of level (n-1) and Y blue jewels of level (n-1)".

Takahashi wants as many blue jewels of level 1 as possible. At most, how many blue jewels of level 1 can he obtain by the operations?

Constraints

  • 1 \leq N \leq 10
  • 1 \leq X \leq 5
  • 1 \leq Y \leq 5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

Print the answer.


Sample Input 1

2 3 4

Sample Output 1

12

Takahashi can obtain 12 blue jewels of level 1 by the following conversions.

  • First, he converts a red jewel of level 2 into a red jewel of level 1 and 3 blue jewels of level 2.
    • After this operation, Takahashi has 1 red jewel of level 1 and 3 blue jewels of level 2.
  • Next, he repeats the following conversion 3 times: converting a blue jewel of level 2 into a red jewel of level 1 and 4 blue jewels of level 1.
    • After these operations, Takahashi has 4 red jewels of level 1 and 12 blue jewels of level 1.
  • He cannot perform a conversion anymore.

He cannot obtain more than 12 blue jewels of level 1, so the answer is 12.


Sample Input 2

1 5 5

Sample Output 2

0

Takahashi may not be able to obtain a blue jewel of level 1.


Sample Input 3

10 5 5

Sample Output 3

3942349900

Note that the answer may not fit into a 32-bit integer type.

G - Cutting Woods

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

配点 : 400

問題文

長さ L メートルの直線状の木材があります。
x = 1, 2, \dots, L - 1 に対して、木材の左端から x メートルの地点には目印として線 x が引かれています。

Q 個のクエリが与えられます。 i 番目のクエリは数の組 (c_i, x_i) によって表されます。
以下の説明に従ってクエリを i の昇順に処理してください。

  • c_i = 1 のとき : 線 x_i がある地点で木材を 2 つに切る。
  • c_i = 2 のとき : 線 x_i を含む木材を選び、その長さを出力する。

ただし c_i = 1, 2 の両方に対して、線 x_i はクエリを処理する時点で切られていないことが保証されます。

制約

  • 1 \leq L \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • c_i = 1, 2 (1 \leq i \leq Q)
  • 1 \leq x_i \leq L - 1 (1 \leq i \leq Q)
  • 全ての i (1 \leq i \leq Q) に対して次が成り立つ: 1 \leq j \lt i かつ (c_j,x_j) = (1, x_i) を満たす j は存在しない。
  • 入力は全て整数である。

入力

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

L Q
c_1 x_1
c_2 x_2
\vdots
c_Q x_Q

出力

c_i = 2 を満たすクエリの回数と等しい行数だけ出力せよ。 j 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

5 3
2 2
1 3
2 2

出力例 1

5
3

1 番目のクエリ時点では木材は一度も切られていないので、線 2 を含む木材の長さは 5 メートルです。よって 5 を出力します。
2 番目のクエリによって、木材は 3 メートルの木材と 2 メートルの木材に分割されます。
3 番目のクエリ時点では 線 2 を含む木材の長さは 3 メートルなので、3 を出力します。


入力例 2

5 3
1 2
1 4
2 3

出力例 2

2

入力例 3

100 10
1 31
2 41
1 59
2 26
1 53
2 58
1 97
2 93
1 23
2 84

出力例 3

69
31
6
38
38

Score : 400 points

Problem Statement

We have a long piece of timber with a length of L meters.
For each x = 1, 2, \dots, L - 1, there is a mark called Mark x at x meters from the left end of the piece.

You are given Q queries, the i-th of which is represented as a pair of numbers (c_i, x_i).
Process the queries in ascending order of i as described below.

  • If c_i = 1: cut the piece at Mark x_i into two.
  • If c_i = 2: choose the piece with Mark x_i on it and print its length.

Here, for both kinds of queries c_i = 1, 2, it is guaranteed that there will have been no cut at Mark x_i when the query is to be processed.

Constraints

  • 1 \leq L \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • c_i = 1, 2 (1 \leq i \leq Q)
  • 1 \leq x_i \leq L - 1 (1 \leq i \leq Q)
  • For every i (1 \leq i \leq Q), the following holds: there is no j such that 1 \leq j \lt i and (c_j,x_j) = (1, x_i).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

L Q
c_1 x_1
c_2 x_2
\vdots
c_Q x_Q

Output

Print the number of lines equal to the number of queries c_i = 2. In the j-th line, print the response to the j-th such query.


Sample Input 1

5 3
2 2
1 3
2 2

Sample Output 1

5
3

At the time of the first query, no cut has been made, so the piece with Mark 2 has a length of 5 meters. Thus, you should print 5.
In the second query, the piece is cut into two pieces with lengths of 3 and 2 meters.
At the time of the third query, the piece with Mark 2 has a length of 3 meters, so you should print 3.


Sample Input 2

5 3
1 2
1 4
2 3

Sample Output 2

2

Sample Input 3

100 10
1 31
2 41
1 59
2 26
1 53
2 58
1 97
2 93
1 23
2 84

Sample Output 3

69
31
6
38
38
H - MEX

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

配点 : 475

問題文

0,1,2 からなる長さ N の数列 A=(A_1,A_2,\dots,A_N) と、 M, E, X からなる長さ N の文字列 S=S_1S_2\dots S_N が与えられます。

1 \leq i < j < k \leq N かつ S_iS_jS_k= MEX を満たす全ての整数の組 (i,j,k) に対する \text{mex}(A_i,A_j,A_k) の総和を求めてください。 ここで、\text{mex}(A_i,A_j,A_k)A_i,A_j,A_k のいずれとも一致しない最小の非負整数を意味します。

制約

  • 3\leq N \leq 2\times 10^5
  • N は整数
  • A_i \in \lbrace 0,1,2\rbrace
  • SM, E, X からなる長さ N の文字列

入力

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

N
A_1 A_2 \dots A_N
S

出力

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


入力例 1

4
1 1 0 2
MEEX

出力例 1

3

S_iS_jS_k = MEX となる i,j,k\ (1 \leq i < j < k \leq N) の組は (i,j,k)=(1,2,4),(1,3,4)2 つです。 \text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0,\text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3 より答えは 0+3=3 です。


入力例 2

3
0 0 0
XXX

出力例 2

0

入力例 3

15
1 1 2 0 0 2 0 2 0 0 0 0 0 2 2
EXMMXXXEMEXEXMM

出力例 3

13

Score : 475 points

Problem Statement

You are given a length-N sequence A=(A_1,A_2,\dots,A_N) consisting of 0, 1, and 2, and a length-N string S=S_1S_2\dots S_N consisting of M, E, and X.

Find the sum of \text{mex}(A_i,A_j,A_k) over all tuples of integers (i,j,k) such that 1 \leq i < j < k \leq N and S_iS_jS_k= MEX. Here, \text{mex}(A_i,A_j,A_k) denotes the minimum non-negative integer that equals neither A_i,A_j, nor A_k.

Constraints

  • 3\leq N \leq 2\times 10^5
  • N is an integer.
  • A_i \in \lbrace 0,1,2\rbrace
  • S is a string of length N consisting of M, E, and X.

Input

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

N
A_1 A_2 \dots A_N
S

Output

Print the answer as an integer.


Sample Input 1

4
1 1 0 2
MEEX

Sample Output 1

3

The tuples (i,j,k)\ (1 \leq i < j < k \leq N) such that S_iS_jS_k = MEX are the following two: (i,j,k)=(1,2,4),(1,3,4). Since \text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0 and \text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3, the answer is 0+3=3.


Sample Input 2

3
0 0 0
XXX

Sample Output 2

0

Sample Input 3

15
1 1 2 0 0 2 0 2 0 0 0 0 0 2 2
EXMMXXXEMEXEXMM

Sample Output 3

13
I - Divide the Cake

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

配点 : 500

問題文

AtCoder Land で高橋君と青木君はイチゴの乗ったケーキを食べることになりました。

AtCoder Landのケーキは円形で、N 個の半径方向の切れ込みによって N 個の扇形ピースに区切られています。

切れ込みを時計回りの順に切れ込み 1, 切れ込み 2, \ldots, 切れ込み N と番号をふったとき、時計回りに切れ込み i (1 \leq i \leq N-1) から切れ込み i+1 までの間の部分をピース i と呼びます。また、時計回りに切れ込み N から切れ込み 1 までの間の部分をピース N と呼びます。

ピース i (1 \leq i \leq N) の上には A_i 個のイチゴが乗っています。

高橋君と青木君はこれから以下のゲームをします。

  • まず、高橋君が N 個の切れ込みのなかから 1 つを選ぶ。選んだ切れ込みを切れ込み i とする。
  • 次に、青木君が高橋君の選んだ切れ込み以外の N-1 個の切れ込みのなかから 1 つを選ぶ。選んだ切れ込みを切れ込み j とする。
  • 高橋君は切れ込み i から時計回りに見ていって、切れ込み j までの範囲のピースをすべてもらう。
  • 青木君は、残りのピースをすべてもらう。
  • 高橋君がもらったピースの 1 ピースあたりのイチゴの個数の平均値が青木君がもらったピースの 1 ピースあたりのイチゴの個数の平均値以上であるとき、高橋君の勝ちです。そうでないとき、青木君の勝ちです。

青木君が勝つために最適な切れ込みを選ぶとき、高橋君は必ず勝つためにはどの切れ込みを選べばよいか求めてください。そのような切れ込みが、存在しないときは -1 を、複数存在する場合はそのうち番号が最小のものを答えてください。

制約

  • 2 \leq N \leq 5 \times 10^{5}
  • 0 \leq A_i \leq 5 \times 10^{5}
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えとなる切れ込みの番号を出力せよ。答えとなる切れ込みが存在しない場合は -1 を出力せよ。


入力例 1

3
3 8 5

出力例 1

2

高橋君が切れ込み 1 を選んだ時、青木君は切れ込み 2 を選ぶと高橋君はピース 1 のみを、青木君はピース 2,3 をもらいます。高橋君のもらったピースの上に乗ってるイチゴの個数の平均値は 3、青木君のもらったピースの上に乗ってるイチゴの個数の平均値は 6.5 です。よって青木君が勝ちます。

高橋君が切れ込み 2 を選んだ時、青木君はどの切れ込みを選んでも高橋君が勝ちます。よって、答えは 2 です。


入力例 2

15
4096 64 512 1 256 16384 8 1024 2048 2 128 32 4 16 8192

出力例 2

15