A - Anyway Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

整数 a, b, c, d が与えられるので、以下の指示に従って 2 行出力してください。

1 行目は (a + b) \times (c - d) の計算結果を整数として出力してください。
2 行目は入力にかかわらず Takahashi と出力してください。

制約

  • -100 \leq a, b, c, d \leq 100
  • a,b,c,d は整数

入力

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

a b c d 

出力

問題文の指示に従って 2 行出力せよ。


入力例 1

1 2 5 3

出力例 1

6
Takahashi

(1 + 2) \times(5 - 3) = 3 \times 2 = 6 です。よって 1 行目は 6 を出力します。
2 行目は Takahashi を出力します。1 文字目を小文字にしたりスペルを誤ったりすると誤答となるので注意してください。


入力例 2

10 -20 30 -40

出力例 2

-700
Takahashi

入出力に負の数が含まれる場合もあります。


入力例 3

100 100 100 -100

出力例 3

40000
Takahashi

Score : 100 points

Problem Statement

You are given integers a, b, c, and d. Print two lines as follows.

The first line should contain the result of calculating (a + b) \times (c - d) as an integer.
The second line should contain Takahashi, regardless of the input.

Constraints

  • -100 \leq a, b, c, d \leq 100
  • a, b, c, and d are integers.

Input

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

a b c d 

Output

Print two lines according to the Problem Statement.


Sample Input 1

1 2 5 3

Sample Output 1

6
Takahashi

We have (1 + 2) \times(5 - 3) = 3 \times 2 = 6, so the first line should contain 6.
The second line should contain Takahashi. Lowercasing the first character or incorrect spelling will not be accepted, so be careful.


Sample Input 2

10 -20 30 -40

Sample Output 2

-700
Takahashi

The input or output may contain negative numbers.


Sample Input 3

100 100 100 -100

Sample Output 3

40000
Takahashi
B - Rectangle Detection

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋くんは、以下の方法で 10 個の文字列 S_1,S_2,\dots,S_{10} を生成しました。

  • まず、 S_i (1 \le i \le 10)= .......... ( .10 個並んだ文字列) とする。
  • 次に、以下の条件を全て満たす 4 つの整数 A,B,C,D を選ぶ。
    • 1 \le A \le B \le 10
    • 1 \le C \le D \le 10
  • その後、以下の条件を全て満たす全ての整数組 (i,j) について、 S_ij 文字目を # に書き換える。
    • A \le i \le B
    • C \le j \le D

以上の方法で生成された S_1,S_2,\dots,S_{10} が与えられるので、高橋くんが選んだ整数 A,B,C,D を求めてください。
なお、制約より A,B,C,D は一意に定まる (答えはただひとつ存在する) ことが証明できます。

制約

  • S_1,S_2,\dots,S_{10} は問題文中の方法で生成されうるそれぞれ長さ 10 の文字列

入力

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

S_1
S_2
\vdots
S_{10}

出力

答えを以下の形式で出力せよ。

A B
C D

入力例 1

..........
..........
..........
..........
...######.
...######.
...######.
...######.
..........
..........

出力例 1

5 8
4 9

高橋くんが選んだ整数は A=5,B=8,C=4,D=9 です。
このように選ぶことにより、 S_5,S_6,S_7,S_84 文字目から 9 文字目が # であり他の文字が . である 10 個の長さ 10 の文字列 S_1,S_2,\dots,S_{10} が生成されます。
これは入力で与えられた 10 個の文字列と一致します。


入力例 2

..........
..#.......
..........
..........
..........
..........
..........
..........
..........
..........

出力例 2

2 2
3 3

入力例 3

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########

出力例 3

1 10
1 10

Score : 200 points

Problem Statement

Takahashi generated 10 strings S_1,S_2,\dots,S_{10} as follows.

  • First, let S_i (1 \le i \le 10)= .......... (10 .s in a row).
  • Next, choose four integers A, B, C, and D satisfying all of the following.
    • 1 \le A \le B \le 10.
    • 1 \le C \le D \le 10.
  • Then, for every pair of integers (i,j) satisfying all of the following, replace the j-th character of S_i with #.
    • A \le i \le B.
    • C \le j \le D.

You are given S_1,S_2,\dots,S_{10} generated as above. Find the integers A, B, C, and D Takahashi chose.
It can be proved that such integers A, B, C, and D uniquely exist (there is just one answer) under the Constraints.

Constraints

  • S_1,S_2,\dots,S_{10} are strings, each of length 10, that can be generated according to the Problem Statement.

Input

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

S_1
S_2
\vdots
S_{10}

Output

Print the answer in the following format:

A B
C D

Sample Input 1

..........
..........
..........
..........
...######.
...######.
...######.
...######.
..........
..........

Sample Output 1

5 8
4 9

Here, Takahashi chose A=5, B=8, C=4, D=9.
This choice generates 10 strings S_1,S_2,\dots,S_{10}, each of length 10, where the 4-th through 9-th characters of S_5,S_6,S_7,S_8 are #, and the other characters are ..
These are equal to the strings given in the input.


Sample Input 2

..........
..#.......
..........
..........
..........
..........
..........
..........
..........
..........

Sample Output 2

2 2
3 3

Sample Input 3

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########

Sample Output 3

1 10
1 10
C - Submask

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

非負整数 N が与えられるので、以下の条件を満たす非負整数 x を昇順に全て出力してください。

  • x2 進数として表記した時に 1 となる位の集合が、 N2 進数として表記した時に 1 となる位の集合の部分集合となる。
    • すなわち、全ての非負整数 k について、「 x2^k の位が 1 ならば、 N2^k の位は 1 」が成り立つ。

制約

  • N は整数
  • 0 \le N < 2^{60}
  • N2 進数として表記した時、 1 となる位は 15 個以下である

入力

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

N

出力

答えを 1 行に 1 つずつ、10 進法の整数として昇順に出力せよ。


入力例 1

11

出力例 1

0
1
2
3
8
9
10
11

N = 11_{(10)}2 進数で表記すると、 1011_{(2)} となります。
条件を満たす非負整数 x は以下の通りです。

  • 0000_{(2)}=0_{(10)}
  • 0001_{(2)}=1_{(10)}
  • 0010_{(2)}=2_{(10)}
  • 0011_{(2)}=3_{(10)}
  • 1000_{(2)}=8_{(10)}
  • 1001_{(2)}=9_{(10)}
  • 1010_{(2)}=10_{(10)}
  • 1011_{(2)}=11_{(10)}

入力例 2

0

出力例 2

0

入力例 3

576461302059761664

出力例 3

0
524288
549755813888
549756338176
576460752303423488
576460752303947776
576461302059237376
576461302059761664

入力は 32bit 符号付き整数に収まらない可能性があります。

Score : 300 points

Problem Statement

You are given a non-negative integer N. Print all non-negative integers x that satisfy the following condition in ascending order.

  • The set of the digit positions containing 1 in the binary representation of x is a subset of the set of the digit positions containing 1 in the binary representation of N.
    • That is, the following holds for every non-negative integer k: if the digit in the "2^ks" place of x is 1, the digit in the 2^ks place of N is 1.

Constraints

  • N is an integer.
  • 0 \le N < 2^{60}
  • In the binary representation of N, at most 15 digit positions contain 1.

Input

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

N

Output

Print the answer as decimal integers in ascending order, each in its own line.


Sample Input 1

11

Sample Output 1

0
1
2
3
8
9
10
11

The binary representation of N = 11_{(10)} is 1011_{(2)}.
The non-negative integers x that satisfy the condition are:

  • 0000_{(2)}=0_{(10)}
  • 0001_{(2)}=1_{(10)}
  • 0010_{(2)}=2_{(10)}
  • 0011_{(2)}=3_{(10)}
  • 1000_{(2)}=8_{(10)}
  • 1001_{(2)}=9_{(10)}
  • 1010_{(2)}=10_{(10)}
  • 1011_{(2)}=11_{(10)}

Sample Input 2

0

Sample Output 2

0

Sample Input 3

576461302059761664

Sample Output 3

0
524288
549755813888
549756338176
576460752303423488
576460752303947776
576461302059237376
576461302059761664

The input may not fit into a 32-bit signed integer.

D - Do use hexagon grid

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

以下のような、無限に広い六角形のグリッドがあります。最初、全てのマスは白です。

ある六角形のマスは 2 つの整数 i,j を用いて (i,j) と表されます。
マス (i,j) は以下の 6 つのマスと隣接します。

  • (i-1,j-1)
  • (i-1,j)
  • (i,j-1)
  • (i,j+1)
  • (i+1,j)
  • (i+1,j+1)

高橋くんは、 N 個のマス (X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) を黒く塗りました。
黒いマスがいくつの連結成分をなすか求めてください。
ただし、ある 2 つの黒いマスが同じ連結成分に属するとは、この 2 つの黒いマスの間をいくつかの隣接する黒いマスを辿って移動できることを指します。

制約

  • 入力は全て整数
  • 1 \le N \le 1000
  • |X_i|,|Y_i| \le 1000
  • (X_i,Y_i) は相異なる

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

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


入力例 1

6
-1 -1
0 1
0 2
1 0
1 2
2 0

出力例 1

3

高橋くんがマスを黒く塗った後、グリッドは下図の状態になります。

黒いマスがなす連結成分は以下の 3 つです。

  • (-1,-1)
  • (1,0),(2,0)
  • (0,1),(0,2),(1,2)

入力例 2

4
5 0
4 1
-3 -4
-2 -5

出力例 2

4

入力例 3

5
2 1
2 -1
1 0
3 1
1 -1

出力例 3

1

Score : 400 points

Problem Statement

We have an infinite hexagonal grid shown below. Initially, all squares are white.

A hexagonal cell is represented as (i,j) with two integers i and j.
Cell (i,j) is adjacent to the following six cells:

  • (i-1,j-1)
  • (i-1,j)
  • (i,j-1)
  • (i,j+1)
  • (i+1,j)
  • (i+1,j+1)

Takahashi has painted N cells (X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) black.
Find the number of connected components formed by the black cells.
Two black cells belong to the same connected component when one can travel between those two black cells by repeatedly moving to an adjacent black cell.

Constraints

  • All values in the input are integers.
  • 1 \le N \le 1000
  • |X_i|,|Y_i| \le 1000
  • The pairs (X_i,Y_i) are distinct.

Input

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the answer as an integer.


Sample Input 1

6
-1 -1
0 1
0 2
1 0
1 2
2 0

Sample Output 1

3

After Takahashi paints cells black, the grid looks as shown below.

The black squares form the following three connected components:

  • (-1,-1)
  • (1,0),(2,0)
  • (0,1),(0,2),(1,2)

Sample Input 2

4
5 0
4 1
-3 -4
-2 -5

Sample Output 2

4

Sample Input 3

5
2 1
2 -1
1 0
3 1
1 -1

Sample Output 3

1
E - Last Rook

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。

縦横 N マスのチェス盤と N 個のルークの駒があります。以下では上から i 行目、左から j 列目のマスを (i, j) と表します。
チェス盤のマスにルークを置くことを考えます。ただし、あなたは次の条件をすべて満たすようにルークをチェス盤に置く必要があります。

  • 1 つの行に 2 個以上のルークが存在しない。
  • 1 つの列に 2 個以上のルークが存在しない。

今、チェス盤に N-1 個のルークが上の条件をすべて満たした状態で置かれています。あなたはルークが置かれていないマスを 1 つ選び、そのマスにルークを置くことにしました。(上の条件をすべて満たすようにルークを置くことができるマスは少なくとも 1 つ以上存在することが証明できます。)

ただし、あなたはチェス盤のどのマスにルークが置かれているかを直接知ることはできません。
そのかわりに、ジャッジシステムに対して以下の質問を 20 回まで行うことができます。

  • 整数 A, B, C, D1 \leq A \leq B \leq N, 1 \leq C \leq D \leq N を満たすように選ぶ。そして、A \leq i \leq B, C \leq j \leq D を満たすマス (i, j) からなる長方形領域に置かれているルークの個数を聞く。

ルークを置くことができるマスを見つけてください。

制約

  • 2 \leq N \leq 10^3
  • N は整数

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。

最初に、チェス盤のサイズ N を標準入力から受け取ってください。

N

次に、ルークを置くことができるマスが見つかるまで質問を繰り返してください。
質問は、以下の形式で標準出力に出力してください。

? A B C D

これに対する応答は、次の形式で標準入力から与えられます。

T

ここで、T は質問に対する答えです。ただし、不正な質問を行った、あるいは質問の回数が 20 回を超えた場合は T-1 となります。

ジャッジが -1 を返した場合、提出はすでに不正解とみなされています。この場合、ただちにプログラムを終了してください。

ルークを置くことができるマスを見つけたら、そのマスを (X, Y) として、解答を以下の形式で出力してください。その後、ただちにプログラムを終了してください。

! X Y

答えが複数ある場合、どれを出力しても正解とみなされます。

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った場合のジャッジ結果は不定です。
  • 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。

入出力例

以下は、N=3(1, 2), (2, 1) にルークが置かれている場合の入出力例です。

入力 出力 説明
3 まず整数 N が与えられます。
? 1 2 1 3 (A,B,C,D)=(1,2,1,3) として質問を行います。
2 質問の答えは 2 なので、ジャッジはその値を返します。
? 2 3 1 1 (A,B,C,D)=(2,3,1,1) として質問を行います。
1 質問の答えは 1 なので、ジャッジはその値を返します。
? 1 3 3 3 (A,B,C,D)=(1,3,3,3) として質問を行います。
0 質問の答えは 0 なので、ジャッジはその値を返します。
! 3 3 答えは (3, 3) だとわかったので、それを出力します。

Score : 500 points

Problem Statement

This is an interactive task (where your program interacts with the judge's program via input and output).

We have an N-by-N chessboard and N rooks. Below, the square at the i-th row from the top and j-th column from the left is denoted by (i, j).
Consider placing the rooks on squares of the chessboard. Here, you have to place the rooks so that all of the following conditions are satisfied.

  • No row contains two or more rooks.
  • No column contains two or more rooks.

Now, N-1 rooks are placed on the chessboard so that all of the above conditions are satisfied. You will choose a square that is not occupied by a rook and place a rook on that square. (It can be proved that there is at least one square on which a rook can be placed under the conditions.)

However, you cannot directly see which squares of the chessboard are occupied by a rook.
Instead, you may ask at most 20 questions to the judge in the following manner.

  • You choose integers A, B, C, and D such that 1 \leq A \leq B \leq N, 1 \leq C \leq D \leq N, and ask the number of rooks in the rectangular region formed by the squares (i, j) such that A \leq i \leq B, C \leq j \leq D.

Find a square to place a rook.

Constraints

  • 2 \leq N \leq 10^3
  • N is an integer.

Input and Output

This is an interactive task (where your program interacts with the judge's program via input and output).

First, receive the size of the chessboard, N, from Standard Input.

N

Next, repeat asking a question until you find a square to place a rook.
A question should be printed to Standard Output in the following format:

? A B C D

The response will be given from Standard Input in the following format:

T

Here, T is the response to the question, or -1 if the question is invalid or more than 20 questions have been asked.

When the judge returns -1, the submission is already regarded as incorrect. In this case, terminate the program immediately.

When you find a square to place a rook, let (X, Y) be that square and print an answer in the following format. Then, terminate the program immediately.

! X Y

If there are multiple appropriate answers, any of them will be accepted.

Notes

  • Each time you print something, end it with a newline and then flush Standard Output. Otherwise, you may get a TLE verdict.
  • If an invalid output is printed during the interaction, the verdict will be indeterminate.
  • Terminate the program immediately after printing an answer. Otherwise, the verdict will be indeterminate.

Sample Interaction

Below is an interaction where N=3 and the rooks are placed on (1, 2) and (2, 1).

Input Output Description
3 Judge first gives the integer N.
? 1 2 1 3 Participant asks a question with (A,B,C,D)=(1,2,1,3).
2 Judge returns the answer to the question, which is 2.
? 2 3 1 1 Participant asks a question with (A,B,C,D)=(2,3,1,1).
1 Judge returns the answer to the question, which is 1.
? 1 3 3 3 Participant asks a question with (A,B,C,D)=(1,3,3,3).
0 Judge returns the answer to the question, which is 0.
! 3 3 Participant finds the answer to be (3, 3) and prints it.
F - Numbered Checker

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

NM 列のグリッドがあり、上から i 行目、左から j 列目のマス (i,j) には整数 (i-1) \times M + j が書かれています。
このグリッドに、以下の操作を施します。

  • 全てのマス (i,j) について、 i+j が奇数ならそのマスに書かれている数字を 0 に書き換える。

操作後のグリッドについて、Q 個の質問に答えてください。
i 個目の質問は以下の通りです:

  • 以下の条件を全て満たすマス (p,q) 全てについて、そこに書かれている整数を全て足し合わせた値を 998244353 で割った余りを求めよ。
    • A_i \le p \le B_i
    • C_i \le q \le D_i

制約

  • 入力は全て整数
  • 1 \le N,M \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • 1 \le A_i \le B_i \le N
  • 1 \le C_i \le D_i \le M

入力

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

N M
Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
\vdots
A_Q B_Q C_Q D_Q

出力

Q 行出力せよ。
そのうち i 行目には、 i 個目の質問に対する答えを整数として出力せよ。


入力例 1

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

出力例 1

28
27
36
14
0
104

この入力では、グリッドは以下の通りです。

この入力には 6 つの質問が含まれます。

  • 1 個目の質問の答えは 0+3+0+6+0+8+0+11+0=28 です。
  • 2 個目の質問の答えは 1+0+9+0+17=27 です。
  • 3 個目の質問の答えは 17+0+19+0=36 です。
  • 4 個目の質問の答えは 14 です。
  • 5 個目の質問の答えは 0 です。
  • 6 個目の質問の答えは 104 です。

入力例 2

1000000000 1000000000
3
1000000000 1000000000 1000000000 1000000000
165997482 306594988 719483261 992306147
1 1000000000 1 1000000000

出力例 2

716070898
240994972
536839100

1 個目の質問について、マス (10^9,10^9) に書かれている整数は 10^{18} ですが、それを 998244353 で割った余りを求めることに注意してください。


入力例 3

999999999 999999999
3
999999999 999999999 999999999 999999999
216499784 840031647 84657913 415448790
1 999999999 1 999999999

出力例 3

712559605
648737448
540261130

Score : 500 points

Problem Statement

We have a grid with N rows and M columns. The square (i,j) at the i-th row from the top and j-th column from the left has an integer (i-1) \times M + j written on it.
Let us perform the following operation on this grid.

  • For every square (i,j) such that i+j is odd, replace the integer on that square with 0.

Answer Q questions on the grid after the operation.
The i-th question is as follows:

  • Find the sum of the integers written on all squares (p,q) that satisfy all of the following conditions, modulo 998244353.
    • A_i \le p \le B_i.
    • C_i \le q \le D_i.

Constraints

  • All values in the input are integers.
  • 1 \le N,M \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • 1 \le A_i \le B_i \le N
  • 1 \le C_i \le D_i \le M

Input

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

N M
Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
\vdots
A_Q B_Q C_Q D_Q

Output

Print Q lines.
The i-th line should contain the answer to the i-th question as an integer.


Sample Input 1

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

Sample Output 1

28
27
36
14
0
104

The grid in this input is shown below.

This input contains six questions.

  • The answer to the first question is 0+3+0+6+0+8+0+11+0=28.
  • The answer to the second question is 1+0+9+0+17=27.
  • The answer to the third question is 17+0+19+0=36.
  • The answer to the fourth question is 14.
  • The answer to the fifth question is 0.
  • The answer to the sixth question is 104.

Sample Input 2

1000000000 1000000000
3
1000000000 1000000000 1000000000 1000000000
165997482 306594988 719483261 992306147
1 1000000000 1 1000000000

Sample Output 2

716070898
240994972
536839100

For the first question, note that although the integer written on the square (10^9,10^9) is 10^{18}, you are to find it modulo 998244353.


Sample Input 3

999999999 999999999
3
999999999 999999999 999999999 999999999
216499784 840031647 84657913 415448790
1 999999999 1 999999999

Sample Output 3

712559605
648737448
540261130
G - Reversible Cards 2

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N までの番号がついた N 枚のカードがあります。
カード i の表には整数 A_i, 裏には整数 B_i が書いてあります。 また、\sum_{i=1}^N (A_i + B_i) = M です。
k=0,1,2,...,M について次の問題を解いてください。

N 枚のカードがすべて表側が見える状態で並べられています。あなたは 0 枚以上 N 枚以下のカードを選び、それらを裏返すことができます。
見えている数の和が k になるには最小で何枚のカードを裏返す必要がありますか?枚数を出力してください。
ただし、どのようにカードを裏返しても見えている数の和が k にならない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq A_i, B_i \leq M
  • \sum_{i=1}^N (A_i + B_i) = M
  • 入力される値はすべて整数

入力

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

N M
A_1 B_1
A_2 B_2
\vdots 
A_N B_N

出力

M+1 行出力せよ。i 行目には k=i-1 のときの答えを出力せよ。


入力例 1

3 6
0 2
1 0
0 3

出力例 1

1
0
2
1
1
3
2

例えば k=0 のときは、カード 2 のみを裏返せば見えている数の和を 0+0+0=0 にすることができて、これが最適です。
また、k=5 のときは、すべてのカードを裏返せば見えている数の和を 2+0+3=5 にすることができて、これが最適です。


入力例 2

2 3
1 1
0 1

出力例 2

-1
0
1
-1

入力例 3

5 12
0 1
0 3
1 0
0 5
0 2

出力例 3

1
0
1
1
1
2
1
2
2
2
3
3
4

Score : 600 points

Problem Statement

We have N cards numbered 1 to N.
Card i has an integer A_i written on the front and an integer B_i written on the back. Here, \sum_{i=1}^N (A_i + B_i) = M.
For each k=0,1,2,...,M, solve the following problem.

The N cards are arranged so that their front sides are visible. You may choose between 0 and N cards (inclusive) and flip them.
To make the sum of the visible numbers equal to k, at least how many cards must be flipped? Print this number of cards.
If there is no way to flip cards to make the sum of the visible numbers equal to k, print -1 instead.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq A_i, B_i \leq M
  • \sum_{i=1}^N (A_i + B_i) = M
  • All values in the input are integers.

Input

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

N M
A_1 B_1
A_2 B_2
\vdots 
A_N B_N

Output

Print M+1 lines. The i-th line should contain the answer for k=i-1.


Sample Input 1

3 6
0 2
1 0
0 3

Sample Output 1

1
0
2
1
1
3
2

For k=0, for instance, flipping just card 2 makes the sum of the visible numbers 0+0+0=0. This choice is optimal.
For k=5, flipping all cards makes the sum of the visible numbers 2+0+3=5. This choice is optimal.


Sample Input 2

2 3
1 1
0 1

Sample Output 2

-1
0
1
-1

Sample Input 3

5 12
0 1
0 3
1 0
0 5
0 2

Sample Output 3

1
0
1
1
1
2
1
2
2
2
3
3
4
Ex - Antichain

Time Limit: 8 sec / Memory Limit: 1024 MB

配点 : 600

問題文

頂点に 1 から N の番号がついた N 頂点の根付き木 T があります。頂点 1 が根で、頂点 i (2 \leq i \leq N) の親は頂点 P_i です。

T の頂点集合 V = \lbrace 1, 2,\dots, N\rbrace の空でない部分集合 S のうち、次の条件を満たすものを 良い頂点集合 と呼びます。

  • S に含まれる任意の異なる頂点の組 (u, v) について、uv の祖先でない。

K = 1, 2, \dots, N について、 (良い頂点集合のうち、頂点数が K であるものの個数) \text{mod }998244353 を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \lt i
  • 入力される値はすべて整数

入力

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

N
P_2 P_3 \dots P_N

出力

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


入力例 1

4
1 2 1

出力例 1

4
2
0
0

1 \leq K \leq N について、サイズが K である良い頂点集合を列挙すると次のようになります。

  • K=1 : \lbrace 1 \rbrace, \lbrace 2 \rbrace, \lbrace 3 \rbrace, \lbrace 4 \rbrace
  • K=2 : \lbrace 2, 4 \rbrace, \lbrace 3, 4 \rbrace
  • K=3,4 : 良い頂点集合は存在しない

入力例 2

6
1 1 2 2 5

出力例 2

6
6
2
0
0
0

入力例 3

6
1 1 1 1 1

出力例 3

6
10
10
5
1
0

入力例 4

10
1 2 1 2 1 1 2 6 9

出力例 4

10
30
47
38
16
3
0
0
0
0

Score : 600 points

Problem Statement

We have a rooted tree T with N vertices numbered 1 to N. Vertex 1 is the root, and the parent of vertex i (2 \leq i \leq N) is vertex P_i.

A non-empty subset S of the vertex set V = \lbrace 1, 2,\dots, N\rbrace of T is said to be a good vertex set when it satisfies the following condition.

  • For every pair of different vertices (u, v) in S, the following holds: u is not an ancestor of v.

For each K = 1, 2, \dots, N, find the number, modulo 998244353, of good vertex sets with exactly K vertices.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \lt i
  • All values in the input are integers.

Input

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

N
P_2 P_3 \dots P_N

Output

Print N lines. The i-th line should contain the answer for K = i.


Sample Input 1

4
1 2 1

Sample Output 1

4
2
0
0

For each 1 \leq K \leq N, the good vertex sets of size K are listed below.

  • K=1: \lbrace 1 \rbrace, \lbrace 2 \rbrace, \lbrace 3 \rbrace, \lbrace 4 \rbrace.
  • K=2: \lbrace 2, 4 \rbrace, \lbrace 3, 4 \rbrace.
  • K=3,4: There is none.

Sample Input 2

6
1 1 2 2 5

Sample Output 2

6
6
2
0
0
0

Sample Input 3

6
1 1 1 1 1

Sample Output 3

6
10
10
5
1
0

Sample Input 4

10
1 2 1 2 1 1 2 6 9

Sample Output 4

10
30
47
38
16
3
0
0
0
0