A - Grandma's Footsteps

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

高橋君は学校でゲームを楽しんでいます。チャイムが鳴ると同時にゲームが開始します。

高橋君はチャイムが鳴った直後から、以下の動作を繰り返し行います。

  • 毎秒 S メートルの速さで A 秒間走る。その後の B 秒間は静止する。

チャイムが鳴ってから X 秒が経過するまでに、高橋君は合計何メートル走りますか?

制約

  • 1 \leq S \leq 15
  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • 1 \leq X \leq 1000
  • 入力される値はすべて整数

入力

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

S A B X

出力

答えを 1 行に出力せよ。単位 (メートル) は省いて出力すること。


入力例 1

7 3 2 11

出力例 1

49

チャイムが鳴ってからの 11 秒間、高橋君は以下のように動きます。

  • 0 秒後から 3 秒後までのあいだ、高橋君は毎秒 7 メートルの速さで走る。このあいだの移動距離は 21 メートル。
  • 3 秒後から 5 秒後までのあいだ、高橋君は静止する。
  • 5 秒後から 8 秒後までのあいだ、高橋君は毎秒 7 メートルの速さで走る。このあいだの移動距離は 21 メートル。
  • 8 秒後から 10 秒後までのあいだ、高橋君は静止する。
  • 10 秒後から 11 秒後までのあいだ、高橋君は毎秒 7 メートルの速さで走る。このあいだの移動距離は 7 メートル。

総移動距離は 49 メートルなので、49 を出力します。


入力例 2

6 3 2 9

出力例 2

36

チャイムが鳴ってからの 9 秒間、高橋君は以下のように動きます。

  • 0 秒後から 3 秒後までのあいだ、高橋君は毎秒 6 メートルの速さで走る。このあいだの移動距離は 18 メートル。
  • 3 秒後から 5 秒後までのあいだ、高橋君は静止する。
  • 5 秒後から 8 秒後までのあいだ、高橋君は毎秒 6 メートルの速さで走る。このあいだの移動距離は 18 メートル。
  • 8 秒後から 9 秒後までのあいだ、高橋君は静止する。

総移動距離は 36 メートルなので、36 を出力します。


入力例 3

1 1 666 428

出力例 3

1

チャイムが鳴ってからの 428 秒間、高橋君は以下のように動きます。

  • 0 秒後から 1 秒後までのあいだ、高橋君は毎秒 1 メートルの速さで走る。このあいだの移動距離は 1 メートル。
  • 1 秒後から 428 秒後までのあいだ、高橋君は静止する。

総移動距離は 1 メートルなので、1 を出力します。

Score : 150 points

Problem Statement

Takahashi is enjoying a game at school. The game starts at the moment the bell rings.

Immediately after the bell rings, he repeats the following actions:

  • Run at a speed of S meters per second for A seconds. Then, remain stationary for B seconds.

How many meters in total does he run by the time X seconds have elapsed since the bell rang?

Constraints

  • 1 \leq S \leq 15
  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • 1 \leq X \leq 1000
  • All input values are integers.

Input

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

S A B X

Output

Output the answer in one line. Omit the unit (meters) in the output.


Sample Input 1

7 3 2 11

Sample Output 1

49

During the 11 seconds after the bell rings, Takahashi moves as follows:

  • From 0 seconds to 3 seconds, he runs at a speed of 7 meters per second. The distance traveled during this time is 21 meters.
  • From 3 seconds to 5 seconds, he remains stationary.
  • From 5 seconds to 8 seconds, he runs at a speed of 7 meters per second. The distance traveled during this time is 21 meters.
  • From 8 seconds to 10 seconds, he remains stationary.
  • From 10 seconds to 11 seconds, he runs at a speed of 7 meters per second. The distance traveled during this time is 7 meters.

The total distance traveled is 49 meters, so output 49.


Sample Input 2

6 3 2 9

Sample Output 2

36

During the 9 seconds after the bell rings, Takahashi moves as follows:

  • From 0 seconds to 3 seconds, he runs at a speed of 6 meters per second. The distance traveled during this time is 18 meters.
  • From 3 seconds to 5 seconds, he remains stationary.
  • From 5 seconds to 8 seconds, he runs at a speed of 6 meters per second. The distance traveled during this time is 18 meters.
  • From 8 seconds to 9 seconds, he remains stationary.

The total distance traveled is 36 meters, so output 36.


Sample Input 3

1 1 666 428

Sample Output 3

1

During the 428 seconds after the bell rings, Takahashi moves as follows:

  • From 0 seconds to 1 second, he runs at a speed of 1 meter per second. The distance traveled during this time is 1 meter.
  • From 1 second to 428 seconds, he remains stationary.

The total distance traveled is 1 meter, so output 1.

B - Most Frequent Substrings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

英小文字からなる長さ N の文字列 S が与えられます。

長さ K の文字列 t出現回数を、以下を満たす整数 i の個数として定義します。

  • 1 \leq i \leq N-K+1
  • Si 文字目から i+K-1 文字目までからなる部分文字列が t に一致する。

長さ K の文字列に対する出現回数の最大値 x を求めてください。 また、出現回数が x となるような長さ K の文字列をすべて辞書順昇順に出力してください。

制約

  • N, K は整数
  • S は英小文字からなる長さ N の文字列
  • 1 \leq K \leq N \leq 100

入力

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

N K
S

出力

2 行出力せよ。

1 行目には、長さ K の文字列に対する出現回数の最大値 x を出力せよ。

2 行目には、出現回数が x となるような長さ K の文字列を辞書順昇順に、空白区切りで出力せよ。


入力例 1

9 3
ovowowovo

出力例 1

2
ovo owo

出現回数 2 の文字列として、以下が挙げられます。

  • 文字列 ovo について、i=1,7 が条件を満たすことから、ovo の出現回数は 2 です。
  • 文字列 owo について、i=3,5 が条件を満たすことから、owo の出現回数は 2 です。

出現回数が 2 よりも大きいような長さ 3 の文字列は存在しないので、求める最大値は 2 です。

一方で、出現回数が 2 よりも小さい文字列として、以下が挙げられます。

  • 文字列 vow について、i=2 が条件を満たすことから、vow の出現回数は 1 です。
  • 文字列 ooo について、条件を満たす i が存在しないことから、ooo の出現回数は 0 です。

入力例 2

9 1
ovowowovo

出力例 2

5
o

入力例 3

35 3
thequickbrownfoxjumpsoverthelazydog

出力例 3

2
the

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Define the number of occurrences of a string t of length K as the number of integers i that satisfy the following condition:

  • 1 \leq i \leq N-K+1
  • The substring of S from the i-th character to the (i+K-1)-th character matches t.

Find the maximum value x of the number of occurrences of a string of length K. Also, output all strings of length K with x occurrences in ascending lexicographical order.

Constraints

  • N, K are integers.
  • S is a string of length N consisting of lowercase English letters.
  • 1 \leq K \leq N \leq 100

Input

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

N K
S

Output

Output two lines.

The first line should contain the maximum value x of the number of occurrences of a string of length K.

The second line should contain all strings of length K with x occurrences in ascending lexicographical order, separated by spaces.


Sample Input 1

9 3
ovowowovo

Sample Output 1

2
ovo owo

The following strings have two occurrences:

  • For the string ovo, i=1,7 satisfy the condition, so the number of occurrences of ovo is 2.
  • For the string owo, i=3,5 satisfy the condition, so the number of occurrences of owo is 2.

There is no string of length 3 with more than two occurrences, so the maximum value is 2.

On the other hand, the following are examples of strings with fewer than two occurrences:

  • For the string vow, i=2 satisfies the condition, so the number of occurrences of vow is 1.
  • For the string ooo, there is no i that satisfies the condition, so the number of occurrences of ooo is 0.

Sample Input 2

9 1
ovowowovo

Sample Output 2

5
o

Sample Input 3

35 3
thequickbrownfoxjumpsoverthelazydog

Sample Output 3

2
the
C - Brackets Stack Query

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

文字列 T が次の条件を満たすとき、T を良い括弧列と呼びます。

  • 次の操作を 0 回以上繰り返すことで T を空文字列にすることができる。

    • T に(連続な)部分文字列として含まれる () を選び、これを取り除く。

例えば ()(()()), および空文字列は良い括弧列ですが、)()())) は良い括弧列ではありません。

文字列 S があります。S ははじめ空文字列です。
以下で説明されるクエリを与えられる順に Q 個処理してください。そして、各クエリの直後に S が良い括弧列であるかを判定してください。
クエリは次の 2 種類です。

  • 1 c: 文字 c が与えられる。c( または ) である。cS の末尾に追加する。
  • 2: S の末尾の文字を削除する。この時、S は空文字列でないことが保証される。

制約

  • 1 \leq Q \leq 8 \times 10^5
  • 1 種類目のクエリの c( または )
  • 2 種類目のクエリを与えられた時点で S は空でないことが保証される
  • Q は整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_ii 番目のクエリを意味する。

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

各クエリは以下の 2 種類のいずれかの形式で与えられる。

1 c
2

出力

Q 行出力せよ。i 行目には、i 番目のクエリを処理した直後の文字列 S が良い括弧列である場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

8
1 (
2
1 (
1 )
2
1 (
1 )
1 )

出力例 1

No
Yes
No
Yes
No
No
No
Yes

1 番目のクエリを処理した直後の S( で、これは良い括弧列ではありません。
2 番目のクエリを処理した直後の S は空文字列で、これは良い括弧列です。
3 番目のクエリを処理した直後の S( で、これは良い括弧列ではありません。
4 番目のクエリを処理した直後の S() で、これは良い括弧列です。
5 番目のクエリを処理した直後の S( で、これは良い括弧列ではありません。
6 番目のクエリを処理した直後の S(( で、これは良い括弧列ではありません。
7 番目のクエリを処理した直後の S(() で、これは良い括弧列ではありません。
8 番目のクエリを処理した直後の S(()) で、これは良い括弧列です。

Score : 300 points

Problem Statement

A string T is called a good bracket sequence if and only if it satisfies the following condition:

  • T can be made into an empty string by repeating the following operation zero or more times:

    • Choose () contained in T as a (contiguous) substring and remove it.

For example, (), (()()), and the empty string are good bracket sequences, but )()( and ))) are not good bracket sequences.

There is a string S. Initially, S is an empty string.
Process Q queries in the order they are given. After each query, determine whether S is a good bracket sequence.
There are two types of queries:

  • 1 c: A character c is given. c is either ( or ). Append c to the end of S.
  • 2: Remove the last character of S. It is guaranteed that S is not an empty string at this time.

Constraints

  • 1 \leq Q \leq 8 \times 10^5
  • c in queries of the first type is ( or ).
  • It is guaranteed that S is not empty when a query of the second type is given.
  • Q is an integer.

Input

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

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

Each query is given in one of the following two formats:

1 c
2

Output

Output Q lines. The i-th line should contain Yes if the string S immediately after processing the i-th query is a good bracket sequence, and No otherwise.


Sample Input 1

8
1 (
2
1 (
1 )
2
1 (
1 )
1 )

Sample Output 1

No
Yes
No
Yes
No
No
No
Yes

S immediately after processing the 1st query is (, which is not a good bracket sequence.
S immediately after processing the 2nd query is an empty string, which is a good bracket sequence.
S immediately after processing the 3rd query is (, which is not a good bracket sequence.
S immediately after processing the 4th query is (), which is a good bracket sequence.
S immediately after processing the 5th query is (, which is not a good bracket sequence.
S immediately after processing the 6th query is ((, which is not a good bracket sequence.
S immediately after processing the 7th query is ((), which is not a good bracket sequence.
S immediately after processing the 8th query is (()), which is a good bracket sequence.

D - 183184

Time Limit: 2.5 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

正整数 x,y に対して f(x,y) を以下で定義します。

  • 十進表記の x,y をそれぞれ文字列として解釈しこの順に連結して得られる文字列を z とする。z を十進表記の整数として解釈したときの値を f(x,y) とする。

たとえば f(3,14)=314,\ f(100,3)=1003 です。

正の整数 C, D が与えられます。以下を満たす整数 x の個数を求めてください。

  • 1 \leq x \leq D
  • f(C, C+x) は平方数である

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \leq T \leq 3 \times 10^5
  • 1 \leq C \leq 2 \times 10^8
  • 1 \leq D \leq 5 \times 10^9
  • 入力される値はすべて整数

入力

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

T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T

\textrm{case}_ii 番目のテストケースを表す。各テストケースは以下の形式で与えられる。

C D

出力

T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。


入力例 1

4
4 80
183 5000
18 10
824 5000000000

出力例 1

3
2
0
1421

1 番目のテストケースにおいて、条件を満たす xx = 5, 37, 803 通りです。

  • x=5 のとき f(C, C+5) = f(4,9) = 49 = 7^2
  • x=37 のとき f(C, C+37) = f(4,41) = 441 = 21^2
  • x=80 のとき f(C, C+80) = f(4,84) = 484 = 22^2

2 番目のテストケースにおいて、条件を満たす xx = 1, 31332 通りです。

  • x=1 のとき f(C, C+1) = f(183,184) = 183184 = 428^2
  • x=3133 のとき f(C, C+3133) = f(183,3316) = 1833316 = 1354^2

3 番目のテストケースにおいて、条件を満たす x0 通りです。

4 番目のテストケースにおいて、条件を満たす x1421 通りです。

Score : 400 points

Problem Statement

For positive integers x and y, define f(x,y) as follows:

  • Let z be the string obtained by interpreting x,y in decimal notation as strings and concatenating them in this order. Let f(x,y) be the value when z is interpreted as an integer in decimal notation.

For example, f(3,14)=314,\ f(100,3)=1003.

You are given positive integers C and D. Find the number of integers x that satisfy the following conditions:

  • 1 \leq x \leq D
  • f(C, C+x) is a perfect square.

You are given T test cases, find the answer for each of them.

Constraints

  • 1 \leq T \leq 3 \times 10^5
  • 1 \leq C \leq 2 \times 10^8
  • 1 \leq D \leq 5 \times 10^9
  • All input values are integers.

Input

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

T
\textrm{case}_1
\textrm{case}_2
\vdots
\textrm{case}_T

\textrm{case}_i represents the i-th test case. Each test case is given in the following format:

C D

Output

Output T lines. The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case.


Sample Input 1

4
4 80
183 5000
18 10
824 5000000000

Sample Output 1

3
2
0
1421

For the first test case, there are three values of x that satisfy the conditions: x = 5, 37, 80.

  • When x=5, f(C, C+5) = f(4,9) = 49 = 7^2
  • When x=37, f(C, C+37) = f(4,41) = 441 = 21^2
  • When x=80, f(C, C+80) = f(4,84) = 484 = 22^2

For the second test case, there are two values of x that satisfy the conditions: x = 1, 3133.

  • When x=1, f(C, C+1) = f(183,184) = 183184 = 428^2
  • When x=3133, f(C, C+3133) = f(183,3316) = 1833316 = 1354^2

For the third test case, there are zero values of x that satisfy the conditions.

For the fourth test case, there are 1421 values of x that satisfy the conditions.

E - Farthest Vertex

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

頂点に 1 から N の番号がついた N 頂点の木があります。i 番目の辺は頂点 A_i と頂点 B_i を結ぶ辺です。
頂点 u と頂点 v の距離を、頂点 u と頂点 v を両端点とするパスに含まれる辺の本数として定義します。(このパスは一意に定まります)

v = 1, 2, \dots, N について次の問題を解いてください。

  • 頂点 1, 2, \dots, N のうち頂点 v からの距離が最大となる頂点の番号を出力してください。ただし、条件を満たす頂点が複数存在する場合は 最も番号が大きい頂点 を出力してください。

制約

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \lt B_i \leq N
  • 入力で与えられるグラフは木
  • 入力される値は全て整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}

出力

N 行出力せよ。i 行目には v=i の時の答えを出力せよ。


入力例 1

3
1 2
2 3

出力例 1

3
3
1

頂点 1 からの距離が最大となる点は頂点 3 です。
頂点 2 からの距離が最大となる点は頂点 1 および頂点 3 です。このうち番号が大きい頂点である頂点 3 が答えとなります。
頂点 3 からの距離が最大となる点は頂点 1 です。


入力例 2

5
1 2
2 3
2 4
1 5

出力例 2

4
5
5
5
4

Score : 475 points

Problem Statement

There is a tree with N vertices numbered 1 to N. The i-th edge connects vertices A_i and B_i.
Define the distance between vertices u and v as the number of edges in the path with endpoints at vertices u and v. (This path is uniquely determined.)

Solve the following problem for v = 1, 2, \dots, N.

  • Among vertices 1, 2, \dots, N, output the number of the vertex that has the maximum distance from vertex v. If there are multiple vertices that satisfy the condition, output the vertex with the largest number.

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \lt B_i \leq N
  • The graph given in the input is a tree.
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}

Output

Output N lines. The i-th line should contain the answer for v=i.


Sample Input 1

3
1 2
2 3

Sample Output 1

3
3
1

The vertex with the maximum distance from vertex 1 is vertex 3.
The vertices with the maximum distance from vertex 2 are vertices 1 and 3. Among them, vertex 3, which has the larger number, is the answer.
The vertex with the maximum distance from vertex 3 is vertex 1.


Sample Input 2

5
1 2
2 3
2 4
1 5

Sample Output 2

4
5
5
5
4
F - Pyramid Alignment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

数直線上に N 個の区間があり、1 から N までの番号が付けられています。

区間 i の左端の座標は 0、右端の座標は W_i です。ここで、W_1 < W_2 < \dots < W_N です。

Q 個のクエリが与えられるので、与えられた順に処理してください。クエリは次の 3 種類のいずれかです。

  • タイプ 1 (1 v): 現在の区間 v左端の座標を l とする。番号が v 以下であるすべての区間をそれぞれ、左端の座標が l となるように平行移動する。
  • タイプ 2 (2 v): 現在の区間 v右端の座標を r とする。番号が v 以下であるすべての区間をそれぞれ、右端の座標が r となるように平行移動する。
  • タイプ 3 (3 x): 座標 x+\frac{1}{2} を含む区間の現在の個数を出力する。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
  • W_1 < W_2 < \dots < W_N
  • タイプ 1,2 のクエリで与えられる v について、1 \leq v \leq N
  • タイプ 3 のクエリで与えられる x について、0 \leq x \leq 10^9
  • タイプ 3 のクエリは少なくとも 1 つ与えられる
  • 入力される値はすべて整数

入力

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

N
W_1 \dots W_N
Q
\textrm{query}_1
\textrm{query}_2
\vdots
\textrm{query}_Q

\textrm{query}_jj 番目のクエリを表す。各クエリは以下のいずれかの形式で与えられる。

1 v
2 v
3 x

出力

タイプ 3 のクエリの回数を q として、q 行出力せよ。j 行目 (1 \leq j \leq q) には j 番目のタイプ 3 のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

4
1

はじめ、各区間は番号順に [0, 2], [0, 4], [0, 6], [0, 10] です。

  • 1 番目のクエリにおいて、操作前の区間 3右端の座標は 6 なので、操作後の各区間は番号順に [4, 6], [2, 6], [0, 6], [0, 10] になります。
  • 2 番目のクエリにおいて、操作前の区間 2左端の座標は 2 なので、操作後の各区間は番号順に [2, 4], [2, 6], [0, 6], [0, 10] になります。
  • 3 番目のクエリにおいて、座標 2 + \frac{1}{2} を含む区間は区間 1,2,3,44 個なので、4 を出力します。
  • 4 番目のクエリにおいて、操作前の区間 4右端の座標は 10 なので、操作後の各区間は番号順に [8, 10], [6, 10], [4, 10], [0, 10] になります。
  • 5 番目のクエリにおいて、座標 1 + \frac{1}{2} を含む区間は区間 4 のみの 1 個なので、1 を出力します。

Score : 525 points

Problem Statement

There are N intervals on a number line, numbered from 1 to N.

The left endpoint of interval i is at coordinate 0, and the right endpoint is at coordinate W_i. Here, W_1 < W_2 < \dots < W_N.

You are given Q queries; process them in the order they are given. Each query is one of the following three types:

  • Type 1 (1 v): Let l be the coordinate of the current left endpoint of interval v. Translate each of the intervals numbered v or less so that its left endpoint is at coordinate l.
  • Type 2 (2 v): Let r be the coordinate of the current right endpoint of interval v. Translate each of the intervals numbered v or less so that its right endpoint is at coordinate r.
  • Type 3 (3 x): Output the current number of intervals that contain coordinate x+\frac{1}{2}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
  • W_1 < W_2 < \dots < W_N
  • For v given in queries of types 1 and 2, 1 \leq v \leq N.
  • For x given in queries of type 3, 0 \leq x \leq 10^9.
  • At least one query of type 3 is given.
  • All input values are integers.

Input

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

N
W_1 \dots W_N
Q
\textrm{query}_1
\textrm{query}_2
\vdots
\textrm{query}_Q

\textrm{query}_j represents the j-th query. Each query is given in one of the following formats:

1 v
2 v
3 x

Output

Let q be the number of queries of type 3, output q lines. The j-th line (1 \leq j \leq q) should contain the answer to the j-th query of type 3.


Sample Input 1

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

Sample Output 1

4
1

Initially, the intervals in order of their numbers are [0, 2], [0, 4], [0, 6], [0, 10].

  • For the 1st query, the coordinate of the right endpoint of interval 3 before the operation is 6, so the intervals after the operation are [4, 6], [2, 6], [0, 6], [0, 10] in order of their numbers.
  • For the 2nd query, the coordinate of the left endpoint of interval 2 before the operation is 2, so the intervals after the operation are [2, 4], [2, 6], [0, 6], [0, 10] in order of their numbers.
  • For the 3rd query, the intervals that contain coordinate 2 + \frac{1}{2} are intervals 1,2,3,4, which is four intervals, so output 4.
  • For the 4th query, the coordinate of the right endpoint of interval 4 before the operation is 10, so the intervals after the operation are [8, 10], [6, 10], [4, 10], [0, 10] in order of their numbers.
  • For the 5th query, the intervals that contain coordinate 1 + \frac{1}{2} is only interval 4, which is one interval, so output 1.
G - Necklace

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 625

問題文

1 から N の番号のついた N 種類の宝石があります。あなたは全ての種類の宝石を無限に持っています。同じ種類の宝石同士は区別できません。
また、2 以上の整数 U があります。各宝石の美しさは 2 以上 U 以下の整数値で表され、宝石 i の美しさは b_i です。

2 \leq x \leq U を満たす整数 x について、以下の問題の答えを f(x) と置きます。

あなたはいくつかの宝石を円形状に並べてネックレスを作ることにしました。
使用した宝石の美しさの総積が x になるようなネックレスの個数を 998244353 で割った余りを求めてください。
ただし、2 つのネックレスは回転させて一致する時は同じネックレスとみなして 1 回だけ数えます。また、2 つのネックレスは上下をひっくり返した上で回転させて一致する場合でも、単に回転させて一致することがなければ別々に数えます。例えば、

  • ネックレス A: 宝石 1, 宝石 2, 宝石 3 をこの順に時計回りに並べてできるネックレス
  • ネックレス B: 宝石 2, 宝石 3, 宝石 1 をこの順に時計回りに並べてできるネックレス
  • ネックレス C: 宝石 1, 宝石 3, 宝石 2 をこの順に時計回りに並べてできるネックレス

とすると、ネックレス A とネックレス B は同じネックレスとみなして 1 回だけ数えますが、ネックレス A とネックレス C は別々に数えます。

f(2), f(3), \dots, f(U) を計算してください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 2 \leq U \leq 5 \times 10^5
  • 2 \leq b_i \leq U
  • 入力される値は全て整数

入力

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

N U
b_1 b_2 \dots b_N

出力

f(2), f(3), \dots, f(U) を空白区切りで 1 行に出力せよ。


入力例 1

4 6
2 2 3 4

出力例 1

2 1 4 0 2

例えば x=4 の時、条件を満たすネックレスは次の 4 個です。

  • 宝石 1, 宝石 1 をこの順に時計回りに並べてできるネックレス
  • 宝石 1, 宝石 2 をこの順に時計回りに並べてできるネックレス
  • 宝石 2, 宝石 2 をこの順に時計回りに並べてできるネックレス
  • 宝石 4 を並べてできるネックレス

入力例 2

4 16
2 2 2 2

出力例 2

4 0 10 0 0 0 24 0 0 0 0 0 0 0 70

入力例 3

14 12
10 2 2 3 8 7 11 6 12 4 9 5 3 2

出力例 3

3 2 7 1 7 1 15 4 4 1 24

Score : 625 points

Problem Statement

There are N types of jewels numbered from 1 to N. You have an infinite number of jewels of all types. Jewels of the same type are indistinguishable.
Also, there is an integer U that is 2 or greater. The beauty of each jewel is represented by an integer value between 2 and U, inclusive, and the beauty of jewel i is b_i.

For each integer x satisfying 2 \leq x \leq U, let f(x) be the answer to the following problem:

You will arrange some jewels in a circular pattern to make a necklace.
Find the number, modulo 998244353, of necklaces such that the product of the beauties of the jewels used equals x.
Here, two necklaces are considered the same and counted only once if they match after rotation. However, two necklaces are counted separately if they do not match just by rotation, even if they match after flipping upside down and rotating. For example, consider the following:

  • Necklace A: A necklace formed by arranging jewels 1, 2, 3 in this order clockwise.
  • Necklace B: A necklace formed by arranging jewels 2, 3, 1 in this order clockwise.
  • Necklace C: A necklace formed by arranging jewels 1, 3, 2 in this order clockwise.

Here, necklaces A and B are considered the same and counted only once, but necklaces A and C are counted separately.

Compute f(2), f(3), \dots, f(U).

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 2 \leq U \leq 5 \times 10^5
  • 2 \leq b_i \leq U
  • All input values are integers.

Input

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

N U
b_1 b_2 \dots b_N

Output

Output f(2), f(3), \dots, f(U) separated by spaces in one line.


Sample Input 1

4 6
2 2 3 4

Sample Output 1

2 1 4 0 2

For example, when x=4, the following four necklaces satisfy the condition:

  • A necklace formed by arranging jewels 1, 1 in this order clockwise.
  • A necklace formed by arranging jewels 1, 2 in this order clockwise.
  • A necklace formed by arranging jewels 2, 2 in this order clockwise.
  • A necklace formed by arranging jewel 4.

Sample Input 2

4 16
2 2 2 2

Sample Output 2

4 0 10 0 0 0 24 0 0 0 0 0 0 0 70

Sample Input 3

14 12
10 2 2 3 8 7 11 6 12 4 9 5 3 2

Sample Output 3

3 2 7 1 7 1 15 4 4 1 24