E - Error Correction

Time Limit: 2 sec / Memory Limit: 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 - Ameba

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

あなたはアメーバの観察記録をつけました。

最初 1 匹のアメーバがおり、番号は 1 です。

観察記録は時系列順に N 個あり、i 番目の観察記録は「番号 A_i のアメーバが分裂して消滅し、新たに 2 匹のアメーバが生まれ、それらにそれぞれ 2i,2i+1 と番号をつけた」というものです。
このとき、アメーバ A_i を アメーバ 2i,2i+1 の親と呼びます。

k=1,\ldots,2N+1 について、アメーバ k から何代親を遡るとアメーバ 1 になるか求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 観察記録は矛盾していない。すなわち
    • 1\leq A_i \leq 2i-1
    • A_i は相異なる整数

入力

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

N
A_1 A_2 \ldots A_N

出力

2N+1 行出力せよ。k 行目にはアメーバ k から何代親を遡るとアメーバ 1 になるかを出力せよ。


入力例 1

2
1 2

出力例 1

0
1
1
2
2

アメーバ 1 からアメーバ 2,3 が生まれ、アメーバ 2 からアメーバ 4,5 が生まれました。

  • アメーバ 10 代遡るとアメーバ 1 になります。
  • アメーバ 21 代遡るとアメーバ 1 になります。
  • アメーバ 31 代遡るとアメーバ 1 になります。
  • アメーバ 41 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。
  • アメーバ 51 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。

入力例 2

4
1 3 5 2

出力例 2

0
1
1
2
2
3
3
2
2

Score : 300 points

Problem Statement

You observed amoebae and kept some records.

Initially, there was one amoeba, numbered 1.

You made N records. According to the i-th record, the amoeba numbered A_i disappeared by dividing itself into two new amoebae, which were then numbered 2i and 2i+1.
Here, amoeba A_i is said to be the parent of amoebae 2i and 2i+1.

For each k=1,\ldots,2N+1, how many generations away is amoeba k from amoeba 1?

Constraints

  • 1 \leq N \leq 2\times 10^5
  • The records are consistent. That is:
    • 1\leq A_i \leq 2i-1.
    • A_i are distinct integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print 2N+1 lines. The k-th line should contain the generation distance between amoeba 1 and amoeba k.


Sample Input 1

2
1 2

Sample Output 1

0
1
1
2
2

From amoeba 1, amoebae 2 and 3 were born. From amoeba 2, amoebae 4 and 5 were born.

  • Amoeba 1 is zero generations away from amoeba 1.
  • Amoeba 2 is one generation away from amoeba 1.
  • Amoeba 3 is one generation away from amoeba 1.
  • Amoeba 4 is one generation away from amoeba 2, and two generations away from amoeba 1.
  • Amoeba 5 is one generation away from amoeba 2, and two generations away from amoeba 1.

Sample Input 2

4
1 3 5 2

Sample Output 2

0
1
1
2
2
3
3
2
2
G - K-th Nearest

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

数直線上に N+Q 個の点 A_1,\dots,A_N,B_1,\dots,B_Q があり、点 A_i の座標は a_i、点 B_j の座標は b_j です。

j=1,2,\dots,Q それぞれについて、以下の問題に答えてください。

  • A_1,A_2,\dots,A_N のうち点 B_j との距離が k_j 番目に近い点を X としたとき、点 X と点 B_j との距離を求めよ。 より厳密には、点 A_i と点 B_j との距離を d_i として、(d_1,d_2,\dots,d_N) を昇順に並び替えてできる列を (d_1',d_2',\dots,d_N') としたとき、d_{k_j}' を求めよ。

制約

  • 1\leq N,Q \leq 10^5
  • -10^8\leq a_i,b_j \leq 10^8
  • 1\leq k_j\leq N
  • 入力は全て整数

入力

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

N Q
a_1 a_2 \dots a_N
b_1 k_1
b_2 k_2
\vdots
b_Q k_Q

出力

Q 行出力せよ。 l\ (1\leq l \leq Q) 行目には、j=l としたときの問題に対する答えを整数として出力せよ。


入力例 1

4 3
-3 -1 5 6
-2 3
2 1
10 4

出力例 1

7
3
13

1 番目のクエリについて説明します。

A_1,A_2,A_3,A_4 と点 B_1 との距離は順に 1,1,7,8 なので、点 B_1 との距離が 3 番目に近いのは点 A_3 です。 よって、点 A_3 と点 B_1 との距離である 7 を出力します。


入力例 2

2 2
0 0
0 1
0 2

出力例 2

0
0

同じ座標に複数の点がある可能性もあります。


入力例 3

10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7

出力例 3

11
66
59
54
88

Score : 425 points

Problem Statement

There are N+Q points A_1,\dots,A_N,B_1,\dots,B_Q on a number line, where point A_i has a coordinate a_i and point B_j has a coordinate b_j.

For each j=1,2,\dots,Q, answer the following question:

  • Let X be the point among A_1,A_2,\dots,A_N that is the k_j-th closest to point B_j. Find the distance between points X and B_j. More formally, let d_i be the distance between points A_i and B_j. Sort (d_1,d_2,\dots,d_N) in ascending order to get the sequence (d_1',d_2',\dots,d_N'). Find d_{k_j}'.

Constraints

  • 1 \leq N, Q \leq 10^5
  • -10^8 \leq a_i, b_j \leq 10^8
  • 1 \leq k_j \leq N
  • All input values are integers.

Input

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

N Q
a_1 a_2 \dots a_N
b_1 k_1
b_2 k_2
\vdots
b_Q k_Q

Output

Print Q lines. The l-th line (1 \leq l \leq Q) should contain the answer to the question for j=l as an integer.


Sample Input 1

4 3
-3 -1 5 6
-2 3
2 1
10 4

Sample Output 1

7
3
13

Let us explain the first query.

The distances from points A_1, A_2, A_3, A_4 to point B_1 are 1, 1, 7, 8, respectively, so the 3rd closest to point B_1 is point A_3. Therefore, print the distance between point A_3 and point B_1, which is 7.


Sample Input 2

2 2
0 0
0 1
0 2

Sample Output 2

0
0

There may be multiple points with the same coordinates.


Sample Input 3

10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7

Sample Output 3

11
66
59
54
88
H - Random Swaps of Balls

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

N - 1 個の白いボールと 1 個の黒いボールがあります。これらの N 個のボールが横一列に並んでおり、はじめ黒いボールが最も左にあります。

高橋くんは、これから以下の操作をちょうど K 回行います。

  • 1 以上 N 以下の整数を一様ランダムに選ぶ試行を 2 回行う。選んだ整数をそれぞれ a, b とする。さらに、 a \neq b であれば左から a 番目のボールと b 番目のボールを交換する。

K 回の操作のあと黒いボールがある位置を左から x 番目とします。x の期待値を \text{mod} \ 998244353 で求めてください。

期待値 \text{mod} \ 998244353 とは 求める期待値は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q \not \equiv 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 1 \leq N \leq 998244352
  • 1 \leq K \leq 10^5

入力

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

N K

出力

答えを 1 行に出力せよ。


入力例 1

2 1

出力例 1

499122178

1 回の操作が終わった後、黒いボールが左から 1 番目にある確率、 2 番目にある確率はそれぞれ \displaystyle \frac{1}{2} です。よって期待値は \displaystyle \frac{3}{2} です。


入力例 2

3 2

出力例 2

554580198

入力例 3

4 4

出力例 3

592707587

Score : 450 points

Problem Statement

There are N - 1 white balls and one black ball. These N balls are arranged in a row, with the black ball initially at the leftmost position.

Takahashi will perform the following operation exactly K times.

  • Choose an integer uniformly at random between 1 and N, inclusive, twice. Let a and b the chosen integers. If a \neq b, swap the a-th and b-th balls from the left.

After K operations, let the black ball be at the x-th position from the left. Find the expected value of x, modulo 998244353.

What is expected value modulo 998244353? It can be proved that the sought expected value will always be rational. Additionally, under the constraints of this problem, it can be proved that if this value is expressed as an irreducible fraction \frac{P}{Q}, then Q \not \equiv 0 \pmod{998244353}. Therefore, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Report this R.

Constraints

  • 1 \leq N \leq 998244352
  • 1 \leq K \leq 10^5

Input

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

N K

Output

Print the answer in one line.


Sample Input 1

2 1

Sample Output 1

499122178

After one operation, the probabilities that the black ball is at the 1st position and the 2nd position from the left are both \displaystyle \frac{1}{2}. Thus, the expected value is \displaystyle \frac{3}{2}.


Sample Input 2

3 2

Sample Output 2

554580198

Sample Input 3

4 4

Sample Output 3

592707587
I - Problem where +s Separate Digits

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1 から 9 までの数字のみで構成された文字列 S が与えられます。
この文字列 S から、以下の操作によって式 T を作ります。

  • はじめ、 T=S であるとする。
  • 各要素が 1 以上 |S|-1 以下の整数である、値に重複のない集合 A を選ぶ。なお、 A が空集合であってもよい。
  • A の全ての要素 x について、 x の降順に以下の操作を行う。
    • Tx 文字目と x+1 文字目の間に、 + を挿入する。

例えば、 S= 1234A= \lbrace 2,3 \rbrace であるとき、 T= 12+3+4 となります。

この操作によって得られる T としてあり得る全ての式に対して、式を計算したときの値の総和を 998244353 で割った余りを求めてください。

制約

  • 1 \le |S| \le 2 \times 10^5
  • S1, 2, 3, 4, 5, 6, 7, 8, 9 のみからなる。

入力

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

S

出力

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


入力例 1

1234

出力例 1

1736

T としてあり得るものは 1234, 123+4, 12+34, 12+3+4, 1+234, 1+23+4, 1+2+34, 1+2+3+48 つです。
これらを計算した値の総和は 1736 です。


入力例 2

1

出力例 2

1

S の長さが 1 であることもあります。この場合、 A として指定可能なのは空集合のみです。


入力例 3

31415926535897932384626433832795

出力例 3

85607943

答えを 998244353 で割った余りを求めてください。

Score : 500 points

Problem Statement

Given is a string S consisting of digits from 1 through 9.
From this string S, let us make a formula T by the following operations.

  • Initially, let T=S.
  • Choose a (possibly empty) set A of different integers where each element is between 1 and |S|-1 (inclusive).
  • For each element x in descending order, do the following.
    • Insert a + between the x-th and (x+1)-th characters of T.

For example, when S= 1234 and A= \lbrace 2,3 \rbrace, we will have T= 12+3+4.

Consider evaluating all possible formulae T obtained by these operations. Find the sum, modulo 998244353, of the evaluations.

Constraints

  • 1 \le |S| \le 2 \times 10^5
  • S consists of 1, 2, 3, 4, 5, 6, 7, 8, and 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

1234

Sample Output 1

1736

There are eight formulae that can be obtained as T: 1234, 123+4, 12+34, 12+3+4, 1+234, 1+23+4, 1+2+34, and 1+2+3+4.
The sum of the evaluations of these formulae is 1736.


Sample Input 2

1

Sample Output 2

1

S may have a length of 1, in which case the only possible choice for A is the empty set.


Sample Input 3

31415926535897932384626433832795

Sample Output 3

85607943

Be sure to find the sum modulo 998244353.