A - Four Digits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

0 以上 9999 以下の整数 N が与えられます。

N の先頭に必要なだけ 0 をつけ、4 桁の文字列にしたものを出力してください。

制約

  • 0 \leq N \leq 9999
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

321

出力例 1

0321

3213 桁なので、先頭に 10 をつけると 4 桁になります。


入力例 2

7777

出力例 2

7777

入力例 3

1

出力例 3

0001

Score : 100 points

Problem Statement

You are given an integer N between 0 and 9999 (inclusive).

Print it as a four-digit string after appending to it the necessary number of leading zeros.

Constraints

  • 0 \leq N \leq 9999
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

321

Sample Output 1

0321

321 has three digits, so we need to add one leading zero to it to make it have four digits.


Sample Input 2

7777

Sample Output 2

7777

Sample Input 3

1

Sample Output 3

0001
B - Failing Grade

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

N 人の学生が試験を受けました。学生には学生 1, 学生 2, \dots, 学生 N と番号がついていて、学生 ia_i 点を取りました。

P 点未満の点数を取った学生は "不可" となり単位を取得できません。 "不可" となった学生の人数を答えてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq P \leq 100
  • 0 \leq a_i \leq 100 (1 \leq i \leq N)
  • 入力はすべて整数である。

入力

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

N P
a_1 a_2 \dots a_N

出力

"不可" となった学生の人数を出力せよ。


入力例 1

4 50
80 60 40 0

出力例 1

2

学生 180 点、学生 260 点と、 50 点以上の点数を取っているので "不可" とならず単位を取得できています。
一方、学生 340 点、学生 40 点で、 50 点を下回る点数を取っているので "不可" となります。よって答えは 2 人です。


入力例 2

3 90
89 89 89

出力例 2

3

入力例 3

2 22
6 37

出力例 3

1

Score : 200 points

Problem Statement

N students took an exam. The students are labeled as Student 1, Student 2, \dots, Student N, and Student i scored a_i points.

A student who scored less than P points are considered to have failed the exam and cannot earn the credit. Find the number of students who failed the exam.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq P \leq 100
  • 0 \leq a_i \leq 100 (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N P
a_1 a_2 \dots a_N

Output

Print the number of students who failed the exam.


Sample Input 1

4 50
80 60 40 0

Sample Output 1

2

Students 1 and 2, who scored 80 and 60 points, respectively, succeeded in scoring at least 50 points to earn the credit.
On the other hand, Students 3 and 4, who scored 40 and 0 points, respectively, fell below 50 points and failed the exam. Thus, the answer is 2.


Sample Input 2

3 90
89 89 89

Sample Output 2

3

Sample Input 3

2 22
6 37

Sample Output 3

1
C - Swiss-System Tournament

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

1 から 2N の番号がついた 2N 人でじゃんけん大会をします。

大会は M ラウンドからなり、各ラウンドは、全ての人が 1 度ずつ参加するような 11N 試合からなります。

i=0,1,\ldots,M について、i ラウンド目の終了時点での順位を次のように決めます。

  • i ラウンド目までの勝数が多い方が上位
  • i ラウンド目までの勝数が同じときは、番号が小さい方が上位

また、i=1,\ldots,M について、i ラウンド目の各試合の組み合わせを次のように決めます。

  • k=1,2,\ldots,N について、i-1 ラウンド目終了時点の順位が 2k-1 位の人と 2k 位の人が試合をする

各試合では、対戦する 2 人がそれぞれ 1 度だけ手を出し、勝ち・負け・引き分けのいずれかの結果が発生します。

未来予知ができる高橋君は、人 ij ラウンド目の試合で出す手が A_{i,j} であることを知っています。
A_{i,j}G, C, P のいずれかであり、それぞれグー、チョキ、パーを表します。

M ラウンド目終了時点の順位を求めてください。

じゃんけんのルール じゃんけんの結果は、2 人の出した手に応じて次のように決まります。
  • 一方がグーで他方がチョキのとき、グーを出した人が勝ち、チョキを出した人は負け
  • 一方がチョキで他方がパーのとき、チョキを出した人が勝ち、パーを出した人は負け
  • 一方がパーで他方がグーのとき、パーを出した人が勝ち、グーを出した人は負け
  • 両者が同じ手を出したとき、引き分け

制約

  • 1 \leq N \leq 50
  • 1 \leq M \leq 100
  • A_{i,j}G, C, P のいずれか

入力

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

N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}

出力

2N 行出力せよ。

i 行目には、M ラウンド目終了時点での順位が i 位である人の番号を出力せよ。


入力例 1

2 3
GCP
PPP
CCC
PPC

出力例 1

3
1
2
4

1 ラウンド目では人 1234 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2314 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 1 が勝ちます。
3 ラウンド目では人 3124 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 4 が勝ちます。
よって最終的な順位は、上位から順に人 3,1,2,4 となります。


入力例 2

2 2
GC
PG
CG
PP

出力例 2

1
2
3
4

1 ラウンド目では人 1234 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2314 がそれぞれ試合をし、前者の試合は引き分け、後者の試合は人 1 が勝ちます。
よって最終的な順位は、上位から順に人 1,2,3,4 となります。

Score : 300 points

Problem Statement

2N players, with ID numbers 1 through 2N, will participate in a rock-scissors-paper contest.

The contest has M rounds. Each round has N one-on-one matches, where each player plays in one of them.

For each i=0, 1, \ldots, M, the players' ranks at the end of the i-th round are determined as follows.

  • A player with more wins in the first i rounds ranks higher.
  • Ties are broken by ID numbers: a player with a smaller ID number ranks higher.

Additionally, for each i=1, \ldots, M, the matches in the i-th round are arranged as follows.

  • For each k=1, 2, \ldots, N, a match is played between the players who rank (2k-1)-th and 2k-th at the end of the (i-1)-th round.

In each match, the two players play a hand just once, resulting in one player's win and the other's loss, or a draw.

Takahashi, who can foresee the future, knows that Player i will play A_{i, j} in their match in the j-th round, where A_{i,j} is G, C, or P.
Here, G stands for rock, C stands for scissors, and P stands for paper. (These derive from Japanese.)

Find the players' ranks at the end of the M-th round.

Rules of rock-scissors-paper The result of a rock-scissors-paper match is determined as follows, based on the hands played by the two players.
  • If one player plays rock (G) and the other plays scissors (C), the player playing rock (G) wins.
  • If one player plays scissors (C) and the other plays paper (P), the player playing scissors (C) wins.
  • If one player plays paper (P) and the other plays rock (G), the player playing paper (P) wins.
  • If the players play the same hand, the match is drawn.

Constraints

  • 1 \leq N \leq 50
  • 1 \leq M \leq 100
  • A_{i,j} is G, C, or P.

Input

Input is given from Standard Input in the following format:

N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}

Output

Print 2N lines.

The i-th line should contain the ID number of the player who ranks i-th at the end of the M-th round.


Sample Input 1

2 3
GCP
PPP
CCC
PPC

Sample Output 1

3
1
2
4

In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. Player 3 wins the former, and Player 1 wins the latter.
In the third round, matches are played between Players 3 and 1, and between Players 2 and 4. Player 3 wins the former, and Player 4 wins the latter.
Therefore, in the end, the players are ranked in the following order: 3,1,2,4, from highest to lowest.


Sample Input 2

2 2
GC
PG
CG
PP

Sample Output 2

1
2
3
4

In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. The former is drawn, and Player 1 wins the latter.
Therefore, in the end, the players are ranked in the following order: 1,2,3,4, from highest to lowest.

D - Between Two Arrays

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ n の数列 S = (s_1, s_2, \dots, s_n) がすべての i (1 \leq i \leq n - 1) に対して s_i \leq s_{i+1} を満たすとき、かつそのときに限り「数列 S は広義単調増加である」と呼びます。

広義単調増加な長さ N の整数列 A = (a_1, a_2, \dots, a_N), B = (b_1, b_2, \dots, b_N) が与えられます。
このとき、次の条件を満たす広義単調増加な長さ N の整数列 C = (c_1, c_2, \dots, c_N) を考えます。

  • すべての i (1 \leq i \leq N) に対して a_i \leq c_i \leq b_i が成り立つ。

整数列 C としてあり得る数列の個数を 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq N \leq 3000
  • 0 \leq a_i \leq b_i \leq 3000 (1 \leq i \leq N)
  • 整数列 A,B は広義単調増加である。
  • 入力はすべて整数である。

入力

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

N
a_1 a_2 \dots a_N
b_1 b_2 \dots b_N

出力

C としてあり得る数列の個数を 998244353 で割ったあまりを出力せよ。


入力例 1

2
1 1
2 3

出力例 1

5

C としてあり得る数列は次の 5 個です。

  • (1, 1)
  • (1, 2)
  • (1, 3)
  • (2, 2)
  • (2, 3)

数列 (2, 1) は広義単調増加でないため条件を満たさないことに注意してください。


入力例 2

3
2 2 2
2 2 2

出力例 2

1

C としてあり得る数列は次の 1 個です。

  • (2, 2, 2)

入力例 3

10
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100

出力例 3

978222082

個数を 998244353 で割ったあまりを求めることに注意してください。

Score : 400 points

Problem Statement

A sequence of n numbers, S = (s_1, s_2, \dots, s_n), is said to be non-decreasing if and only if s_i \leq s_{i+1} holds for every i (1 \leq i \leq n - 1).

Given are non-decreasing sequences of N integers each: A = (a_1, a_2, \dots, a_N) and B = (b_1, b_2, \dots, b_N).
Consider a non-decreasing sequence of N integers, C = (c_1, c_2, \dots, c_N), that satisfies the following condition:

  • a_i \leq c_i \leq b_i for every i (1 \leq i \leq N).

Find the number, modulo 998244353, of sequences that can be C.

Constraints

  • 1 \leq N \leq 3000
  • 0 \leq a_i \leq b_i \leq 3000 (1 \leq i \leq N)
  • The sequences A and B are non-decreasing.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \dots a_N
b_1 b_2 \dots b_N

Output

Print the number, modulo 998244353, of sequences that can be C.


Sample Input 1

2
1 1
2 3

Sample Output 1

5

There are five sequences that can be C, as follows.

  • (1, 1)
  • (1, 2)
  • (1, 3)
  • (2, 2)
  • (2, 3)

Note that (2, 1) does not satisfy the condition since it is not non-decreasing.


Sample Input 2

3
2 2 2
2 2 2

Sample Output 2

1

There is one sequence that can be C, as follows.

  • (2, 2, 2)

Sample Input 3

10
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100

Sample Output 3

978222082

Be sure to find the count modulo 998244353.

E - Red and Blue Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点の木と、長さ M の数列 A=(A_1,\ldots,A_M)、整数 K が与えられます。
木の頂点には 1 から N の番号がつけられており、i 番目の辺は頂点 U_iV_i を結んでいます。

この木の N-1 個の辺をそれぞれ赤か青のどちらかに塗ります。そのような方法は 2^{N-1} 通りありますが、そのうち次の条件を満たすような塗り方の個数を 998244353 で割った余りを求めてください。

条件:
最初、駒を頂点 A_1 におく。i=1,\ldots,M-1 の順に、駒を頂点 A_i から頂点 A_{i+1} まで、辺をたどって最短経路で動かす。 これらの操作を最後まで行ったとき、赤く塗られた辺を通過した回数を R、青く塗られた辺を通過した回数を B とすると、R-B=K である。

制約

  • 2 \leq N \leq 1000
  • 2 \leq M \leq 100
  • |K| \leq 10^5
  • 1 \leq A_i \leq N
  • 1\leq U_i,V_i\leq N
  • 与えられるグラフは木である
  • 入力に含まれる値は全て整数である

入力

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

N M K
A_1 A_2 \ldots A_M
U_1 V_1
\vdots
U_{N-1} V_{N-1}

出力

答えを出力せよ。


入力例 1

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

出力例 1

2

1,3 番目の辺を赤く、2 番目の辺を青く塗ったとき、

  • 頂点 2 から頂点 3 への移動で赤い辺を 0 回、青い辺を 1
  • 頂点 3 から頂点 2 への移動で赤い辺を 0 回、青い辺を 1
  • 頂点 2 から頂点 1 への移動で赤い辺を 1 回、青い辺を 0
  • 頂点 1 から頂点 4 への移動で赤い辺を 2 回、青い辺を 1

それぞれ通過し、全体では赤い辺を 3 回、青い辺を 3 回通るため、条件を満たします。

図

この他、1,3 番目の辺を青く、2 番目の辺を赤く塗るときも条件を満たし、これら以外の塗り方は条件を満たさないため、答えは 2 通りです。


入力例 2

3 10 10000
1 2 1 2 1 2 2 1 1 2
1 2
1 3

出力例 2

0

条件を満たす塗り方が存在しないこともあります。


入力例 3

10 2 -1
1 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

出力例 3

126

入力例 4

5 8 -1
1 4 1 4 2 1 3 5
1 2
4 1
3 1
1 5

出力例 4

2

Score : 500 points

Problem Statement

Given are a tree with N vertices, a sequence of M numbers A=(A_1,\ldots,A_M), and an integer K.
The vertices are numbered 1 through N, and the i-th edge connects Vertices U_i and V_i.

We will paint each of the N-1 edges of this tree red or blue. Among the 2^{N-1} such ways, find the number of ones that satisfies the following condition, modulo 998244353.

Condition:
Let us put a piece on Vertex A_1, and for each i=1,\ldots,M-1 in this order, move it from Vertex A_i to Vertex A_{i+1} along the edges in the shortest path. After all of these movements, R-B=K holds, where R and B are the numbers of times the piece traverses a red edge and a blue edge, respectively.

Constraints

  • 2 \leq N \leq 1000
  • 2 \leq M \leq 100
  • |K| \leq 10^5
  • 1 \leq A_i \leq N
  • 1\leq U_i,V_i\leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_M
U_1 V_1
\vdots
U_{N-1} V_{N-1}

Output

Print the answer.


Sample Input 1

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

Sample Output 1

2

If we paint the 1-st and 3-rd edges red and the 2-nd edge blue, the piece will traverse the following numbers of red and blue edges:

  • 0 red edges and 1 blue edge when moving from Vertex 2 to 3,
  • 0 red edges and 1 blue edge when moving from Vertex 3 to 2,
  • 1 red edge and 0 blue edges when moving from Vertex 2 to 1,
  • 2 red edges and 1 blue edge when moving from Vertex 1 to 4,

for a total of 3 red edges and 3 blue edges, satisfying the condition.

Figure

Another way to satisfy the condition is to paint the 1-st and 3-rd edges blue and the 2-nd edge red. There is no other way to satisfy it, so the answer is 2.


Sample Input 2

3 10 10000
1 2 1 2 1 2 2 1 1 2
1 2
1 3

Sample Output 2

0

There may be no way to paint the tree to satisfy the condition.


Sample Input 3

10 2 -1
1 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

Sample Output 3

126

Sample Input 4

5 8 -1
1 4 1 4 2 1 3 5
1 2
4 1
3 1
1 5

Sample Output 4

2
F - Expensive Expense

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder 王国は N 個の街と N-1 個の道路からなります。
街には 街 1, 街 2, \dots, 街 N と番号がついています。道路にも同様に 道路 1, 道路 2, \dots, 道路 N-1 と番号が付いています。 道路 i は街 A_iB_i を双方向に結んでいて、通過するときに C_i の通行料がかかります。すべての異なる街の組 (i, j) に対して、道路を経由して街 i から街 j に行くことができます。

今、列 D = (D_1, D_2, \dots, D_N) が与えられます。 D_i は街 i を観光するときにかかる費用です。 このとき、街 i から街 j への旅費 E_{i,j} を、(同じ道を 2 回以上使わずに街 i から街 j へ向かうときにかかる通行料の和) に D_{j} を足したものとして定めます。

  • 厳密に言い換えると、i - j 間の最短パスを i = p_0, p_1, \dots, p_{k-1}, p_k = j として、街 p_{l} と街 p_{l+1} を結ぶ道路の通行料を c_l と置いたときに E_{i,j} = D_j + \displaystyle\sum_{l=0}^{k-1} c_l と定義します。

すべての i に対して、街 i を始点として他の街へ旅行したときにありえる旅費の最大値を求めてください。

  • 厳密に言い換えると、すべての i に対して \max_{1 \leq j \leq N, j \neq i} E_{i,j} を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N-1)
  • 1 \leq B_i \leq N (1 \leq i \leq N-1)
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N-1)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 整数の組 (i,j)1 \leq i \lt j \leq N を満たすならば、街 i から街 j へいくつかの道路を通ることで移動できる。
  • 入力はすべて整数である。

入力

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

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_{N-1} B_{N-1} C_{N-1}
D_1 D_2 \dots D_N

出力

N 行出力せよ。i 行目には \displaystyle \max_{1 \leq j \leq N, j \neq i} E_{i,j} を出力せよ。


入力例 1

3
1 2 2
2 3 3
1 2 3

出力例 1

8
6
6

すべての街の順序つき組 (i,j) に対して E_{i,j} を計算すると次のようになります。

  • E_{1,2} = 2 + 2 = 4
  • E_{1,3} = 5 + 3 = 8
  • E_{2,1} = 2 + 1 = 3
  • E_{2,3} = 3 + 3 = 6
  • E_{3,1} = 5 + 1 = 6
  • E_{3,2} = 3 + 2 = 5

入力例 2

6
1 2 3
1 3 1
1 4 4
1 5 1
1 6 5
9 2 6 5 3 100

出力例 2

105
108
106
109
106
14

入力例 3

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

出力例 3

5000000006
4000000006
3000000006
3000000001
4000000001
5000000001

Score : 500 points

Problem Statement

The Kingdom of AtCoder is composed of N towns and N-1 roads.
The towns are labeled as Town 1, Town 2, \dots, Town N. Similarly, the roads are labeled as Road 1, Road 2, \dots, Road N-1. Road i connects Towns A_i and B_i bidirectionally, and you have to pay the toll of C_i to go through it. For every pair of different towns (i, j), it is possible to go from Town A_i to Town B_j via the roads.

You are given a sequence D = (D_1, D_2, \dots, D_N), where D_i is the cost to do sightseeing in Town i. Let us define the travel cost E_{i,j} from Town i to Town j as the total toll incurred when going from Town i to Town j, plus D_{j}.

  • More formally, E_{i,j} is defined as D_j + \displaystyle\sum_{l=0}^{k-1} c_l, where the shortest path between i and j is i = p_0, p_1, \dots, p_{k-1}, p_k = j and the toll for the road connecting Towns p_{l} and p_{l+1} is c_l.

For every i, find the maximum possible travel cost when traveling from Town i to another town.

  • More formally, for every i, find \max_{1 \leq j \leq N, j \neq i} E_{i,j}.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N (1 \leq i \leq N-1)
  • 1 \leq B_i \leq N (1 \leq i \leq N-1)
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N-1)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • It is possible to travel from Town i to Town j via some number of roads, for a pair of integers (i,j) such that 1 \leq i \lt j \leq N.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_{N-1} B_{N-1} C_{N-1}
D_1 D_2 \dots D_N

Output

Print N lines. The i-th line should contain \displaystyle \max_{1 \leq j \leq N, j \neq i} E_{i,j}.


Sample Input 1

3
1 2 2
2 3 3
1 2 3

Sample Output 1

8
6
6

The value of E_{i,j} for every ordered pair of towns (i,j) is as follows.

  • E_{1,2} = 2 + 2 = 4
  • E_{1,3} = 5 + 3 = 8
  • E_{2,1} = 2 + 1 = 3
  • E_{2,3} = 3 + 3 = 6
  • E_{3,1} = 5 + 1 = 6
  • E_{3,2} = 3 + 2 = 5

Sample Input 2

6
1 2 3
1 3 1
1 4 4
1 5 1
1 6 5
9 2 6 5 3 100

Sample Output 2

105
108
106
109
106
14

Sample Input 3

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

Sample Output 3

5000000006
4000000006
3000000006
3000000001
4000000001
5000000001
G - 222

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

2,22,222,2222,\ldots という数列があります。この数列の第 i 項は、全ての桁が 2 である i 桁の整数です。

この数列に初めて K の倍数が登場するのは何項目ですか? 存在しない場合は代わりに -1 と答えてください。

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

制約

  • 1 \leq T \leq 200
  • 1 \leq K \leq 10^8
  • 入力に含まれる値は全て整数である

入力

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

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

各ケースは以下の形式で与えられる。

K

出力

T 行出力せよ。i 行目には \text{case}_i の答えを出力せよ。


入力例 1

4
1
7
10
999983

出力例 1

1
6
-1
999982

4 個のケースが与えられています。

  • 21 の倍数です
  • 2,22,222,2222,222227 の倍数ではありませんが、2222227 の倍数です
  • 2,22,\ldots10 の倍数になることはありません

Score : 600 points

Problem Statement

We have a sequence 2,22,222,2222,\ldots, where the i-th term is an i-digit integer whose digits are all 2.

Where does a multiple of K appear in this sequence for the first time? If the first multiple of K is the x-th term of the sequence, print x; if there is no multiple of K, print -1.

Given T cases, solve each of them.

Constraints

  • 1 \leq T \leq 200
  • 1 \leq K \leq 10^8
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Each case is in the following format:

K

Output

Print T lines. The i-th line should contain the answer for \text{case}_i.


Sample Input 1

4
1
7
10
999983

Sample Output 1

1
6
-1
999982

We have four cases.

  • 2 is a multiple of 1.
  • None of 2,22,222,2222,22222 is a multiple of 7, but 222222 is.
  • None of 2,22,\ldots is a multiple of 10.
H - Beautiful Binary Tree

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

正の整数 N に対して、次の条件を満たす根付き二分木を N 次の美しい二分木 と定めます。

  • 頂点には 01 が書かれている。
  • 頂点が葉ならば、必ず 1 が書かれている。
  • 次の操作を高々 N-1 回行うことで、根に書かれている数を N に、それ以外の頂点に書かれている数を 0 にすることができる。
    • 頂点 u, v を選ぶ。ここで vu の子、あるいは u の子の子である必要がある。 u,v に書かれている数を a_u, a_v としたとき、 a_u \gets a_u + a_v, a_v \gets 0 とする。

N が与えられるので、N 次の美しい二分木の個数を 998244353 で割ったあまりを答えてください。

制約

  • 1 \leq N \leq 10^7
  • 入力はすべて整数である。

入力

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

N

出力

答えを出力せよ。


入力例 1

1

出力例 1

1

条件を満たす二分木は、根に 1 が書かれている 1 頂点の木のみです。


入力例 2

2

出力例 2

6

条件を満たす二分木は次の 6 通りです。

image


入力例 3

222

出力例 3

987355927

入力例 4

222222

出力例 4

675337738

Score : 600 points

Problem Statement

For a positive integer N, a rooted binary tree that satisfies the following conditions is said to be a beautiful binary tree of degree N.

  • Each vertex has 0 or 1 written on it.
  • Each vertex that is a leaf has 1 written on it.
  • It is possible to do the following operation at most N-1 times so that the root has N written on it and the other vertices have 0 written on them.
    • Choose vertices u and v, where v must be a child of u or a child of "a child of u." Let a_u \gets a_u + a_v, a_v \gets 0, where a_u and a_v are the numbers written on u and v, respectively.

Given N, find the number, modulo 998244353, of beautiful binary trees of degree N.

Constraints

  • 1 \leq N \leq 10^7
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

1

Sample Output 1

1

The only binary tree that satisfies the condition is a tree with one vertex whose root has 1 written on it.


Sample Input 2

2

Sample Output 2

6

The binary trees that satisfy the condition are the six trees shown below.

image


Sample Input 3

222

Sample Output 3

987355927

Sample Input 4

222222

Sample Output 4

675337738