E - Cash Register

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は、レジ打ちの仕事をしています。

レジの機械には 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 911 個のボタンがあります。 レジの機械には、はじめ 0 が表示されています。 ボタン 00 を押すと、表示されている数が 100 倍されます。 それ以外のボタンを押すと、表示されている数が 10 倍されたあとに、押されたボタンに書かれている数が加算されます。

高橋君は、レジに整数 S を表示させたいです。 レジに S が表示されている状態にするためには、少なくとも何回ボタンを押す必要があるか求めてください。

制約

  • 1\leq S\leq 10^{100000}
  • S は整数

入力

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

S

出力

答えを 1 行で出力せよ。


入力例 1

40004

出力例 1

4

例えば、次のように操作することでボタンを 4 回押して 40004 を表示させることができます。 はじめ、レジには 0 が表示されています。

  • ボタン 4 を押す。レジに表示されている数は 4 となる。
  • ボタン 00 を押す。レジに表示されている数は 400 となる。
  • ボタン 0 を押す。レジに表示されている数は 4000 となる。
  • ボタン 4 を押す。レジに表示されている数は 40004 となる。

3 回までボタンを押すことでレジに 40004 を表示させることはできないので、出力すべき値は 4 です。


入力例 2

1355506027

出力例 2

10

入力例 3

10888869450418352160768000001

出力例 3

27

S64\operatorname{bit} 整数に収まらない場合があることに注意してください。

Score : 300 points

Problem Statement

Takahashi is a cashier.

There is a cash register with 11 keys: 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The cash register initially displays 0. Whenever he types the key 00, the displayed number is multiplied by 100; whenever he types one of the others, the displayed number is multiplied by 10, and then added by the number written on the key.

Takahashi wants the cash register to display an integer S. At least how many keystrokes are required to make it display S?

Constraints

  • 1\leq S\leq 10^{100000}
  • S is an integer.

Input

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

S

Output

Print the answer in a line.


Sample Input 1

40004

Sample Output 1

4

For example, the following four keystrokes make the cash register display 40004. Initially, the cash register displays 0.

  • Type the key 4. It now displays 4.
  • Type the key 00. It now displays 400.
  • Type the key 0. It now displays 4000.
  • Type the key 4. It now displays 40004.

He cannot make it display 40004 with three or fewer keystrokes, so 4 should be printed.


Sample Input 2

1355506027

Sample Output 2

10

Sample Input 3

10888869450418352160768000001

Sample Output 3

27

Note that S may not fit into a 64-\operatorname{bit} integer type.

F - Product

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 個の袋があります。
i には L_i 個のボールが入っていて、袋 ij(1\leq j\leq L_i) 番目のボールには正の整数 a_{i,j} が書かれています。

それぞれの袋から 1 つずつボールを取り出します。
取り出したボールに書かれた数の総積が X になるような取り出し方は何通りありますか?

ただし、書かれた数が同じであっても全てのボールは区別します。

制約

  • N \geq 2
  • L_i \geq 2
  • 袋に入っているボールの個数の総積は 10^5 を超えない。すなわち、\displaystyle\prod_{i=1}^{N}L_i \leq 10^5
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • 入力に含まれる値は全て整数である。

入力

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

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

出力

答えを出力せよ。


入力例 1

2 40
3 1 8 4
2 10 5

出力例 1

2

13 番目のボールと袋 21 番目のボールを選ぶと、a_{1,3} \times a_{2,1} = 4 \times 10 = 40 となります。
12 番目のボールと袋 22 番目のボールを選ぶと、a_{1,2} \times a_{2,2} = 8 \times 5 = 40 となります。
これ以外に総積が 40 になる取り出し方は存在しないので、答えは 2 です。


入力例 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

出力例 2

45

書かれた数が同じであっても全てのボールは区別することに注意してください。


入力例 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

出力例 3

0

総積が X になる取り出し方が 1 つも存在しないこともあります。

Score : 300 points

Problem Statement

We have N bags.
Bag i contains L_i balls. The j-th ball (1\leq j\leq L_i) in Bag i has a positive integer a_{i,j} written on it.

We will pick out one ball from each bag.
How many ways are there to pick the balls so that the product of the numbers written on the picked balls is X?

Here, we distinguish all balls, even with the same numbers written on them.

Constraints

  • N \geq 2
  • L_i \geq 2
  • The product of the numbers of balls in the bags is at most 10^5: \displaystyle\prod_{i=1}^{N}L_i \leq 10^5.
  • 1 \leq a_{i,j} \leq 10^9
  • 1 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}

Output

Print the answer.


Sample Input 1

2 40
3 1 8 4
2 10 5

Sample Output 1

2

When choosing the 3-rd ball in Bag 1 and 1-st ball in Bag 2, we have a_{1,3} \times a_{2,1} = 4 \times 10 = 40.
When choosing the 2-nd ball in Bag 1 and 2-nd ball in Bag 2, we have a_{1,2} \times a_{2,2} = 8 \times 5 = 40.
There are no other ways to make the product 40, so the answer is 2.


Sample Input 2

3 200
3 10 10 10
3 10 10 10
5 2 2 2 2 2

Sample Output 2

45

Note that we distinguish all balls, even with the same numbers written on them.


Sample Input 3

3 1000000000000000000
2 1000000000 1000000000
2 1000000000 1000000000
2 1000000000 1000000000

Sample Output 3

0

There may be no way to make the product X.

G - Impartial Gift

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は青木君とすぬけ君に 1 つずつ贈り物を送ることにしました。
青木君への贈り物の候補は N 個あり、 それぞれの価値は A_1, A_2, \ldots,A_N です。
すぬけ君への贈り物の候補は M 個あり、 それぞれの価値は B_1, B_2, \ldots,B_M です。

高橋君は 2 人への贈り物の価値の差が D 以下になるようにしたいと考えています。

条件をみたすように贈り物を選ぶことが可能か判定し、可能な場合はそのような選び方における贈り物の価値の和の最大値を求めてください。

制約

  • 1\leq N,M\leq 2\times 10^5
  • 1\leq A_i,B_i\leq 10^{18}
  • 0\leq D \leq 10^{18}
  • 入力はすべて整数

入力

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

N M D
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

高橋君が条件をみたすように贈り物を選ぶことができる場合、 条件をみたし、かつ価値の和が最大になるように贈り物を選んだ時の価値の和を出力せよ。 高橋君が条件をみたすように選ぶことができない場合、-1 を出力せよ。


入力例 1

2 3 2
3 10
2 5 15

出力例 1

8

高橋君は贈り物の価値の差を 2 以下にする必要があります。
青木君に価値 3, すぬけ君に価値 5 の贈り物を渡すと条件をみたし、価値の和としてはこのときが最大となります。
よって、3+5=8 を出力します。


入力例 2

3 3 0
1 3 3
6 2 7

出力例 2

-1

条件をみたすように贈り物を選ぶことは不可能です。 また、同一人物に対して、同じ価値の贈り物が複数存在することもあります。


入力例 3

1 1 1000000000000000000
1000000000000000000
1000000000000000000

出力例 3

2000000000000000000

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


入力例 4

8 6 1
2 5 6 5 2 1 7 9
7 2 5 5 2 4

出力例 4

14

Score : 400 points

Problem Statement

Takahashi has decided to give one gift to Aoki and one gift to Snuke.
There are N candidates of gifts for Aoki, and their values are A_1, A_2, \ldots,A_N.
There are M candidates of gifts for Snuke, and their values are B_1, B_2, \ldots,B_M.

Takahashi wants to choose gifts so that the difference in values of the two gifts is at most D.

Determine if he can choose such a pair of gifts. If he can, print the maximum sum of values of the chosen gifts.

Constraints

  • 1\leq N,M\leq 2\times 10^5
  • 1\leq A_i,B_i\leq 10^{18}
  • 0\leq D \leq 10^{18}
  • All values in the input are integers.

Input

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

N M D
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

If he can choose gifts to satisfy the condition, print the maximum sum of values of the chosen gifts. If he cannot satisfy the condition, print -1.


Sample Input 1

2 3 2
3 10
2 5 15

Sample Output 1

8

The difference of values of the two gifts should be at most 2.
If he gives a gift with value 3 to Aoki and another with value 5 to Snuke, the condition is satisfied, achieving the maximum possible sum of values.
Thus, 3+5=8 should be printed.


Sample Input 2

3 3 0
1 3 3
6 2 7

Sample Output 2

-1

He cannot choose gifts to satisfy the condition. Note that the candidates of gifts for a person may contain multiple gifts with the same value.


Sample Input 3

1 1 1000000000000000000
1000000000000000000
1000000000000000000

Sample Output 3

2000000000000000000

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


Sample Input 4

8 6 1
2 5 6 5 2 1 7 9
7 2 5 5 2 4

Sample Output 4

14
H - Arithmetic Number

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

以下の条件を満たす正の整数 n を、 等差数 と呼びます。

  • (n を先頭に余計な 0 を付けずに 10 進法で表記した際、) n の上から i 桁目を d_i とする。このとき、 nk 桁の整数であったとすると、 (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) が成立する。
    • この条件は、「 数列 (d_1,d_2,\dots,d_k) が等差数列である」と言い換えることができる。
    • 但し、 n1 桁の整数である時、 n は等差数であるものとする。

たとえば、 234,369,86420,17,95,8,11,777 は等差数ですが、 751,919,2022,246810,2356 は等差数ではありません。

等差数のうち、 X 以上で最小のものを求めてください。

制約

  • X1 以上 10^{17} 以下の整数である

入力

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

X

出力

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


入力例 1

152

出力例 1

159

152 以上で最小の等差数は 159 です。


入力例 2

88

出力例 2

88

X 自身が等差数である場合もあります。


入力例 3

8989898989

出力例 3

9876543210

Score : 500 points

Problem Statement

Let us call a positive integer n that satisfies the following condition an arithmetic number.

  • Let d_i be the i-th digit of n from the top (when n is written in base 10 without unnecessary leading zeros.) Then, (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) holds, where k is the number of digits in n.
    • This condition can be rephrased into the sequence (d_1,d_2,\dots,d_k) being arithmetic.
    • If n is a 1-digit integer, it is assumed to be an arithmetic number.

For example, 234,369,86420,17,95,8,11,777 are arithmetic numbers, while 751,919,2022,246810,2356 are not.

Find the smallest arithmetic number not less than X.

Constraints

  • X is an integer between 1 and 10^{17} (inclusive).

Input

Input is given from Standard Input in the following format:

X

Output

Print the answer as an integer.


Sample Input 1

152

Sample Output 1

159

The smallest arithmetic number not less than 152 is 159.


Sample Input 2

88

Sample Output 2

88

X itself may be an arithmetic number.


Sample Input 3

8989898989

Sample Output 3

9876543210
I - A Certain Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

とあるゲームの大会に、プレイヤー 1 、プレイヤー 2\ldots 、プレイヤー NN 人のプレイヤーが参加します。 大会の開始直前、各プレイヤーはそれぞれ 1 人のみからなるチームをなし、全部で N 個のチームがあります。

大会では全部で N−1 回の試合があり、各試合では 2 つの異なるチームが選ばれ、一方が先攻を、もう一方が後攻を受け持って対戦し、その結果ちょうど一方のチームが勝ちます。 具体的には、i = 1, 2, \ldots, N-1 について i 回目の試合は下記の通りに進行します。

  • プレイヤー p_i の属するチームが先攻、プレイヤー q_i の属するチームが後攻として、対戦を行う。
  • その結果、先攻チームの人数を a 、後攻チームの人数を b として、\frac{a}{a+b} の確率で先攻のチームが、\frac{b}{a+b} の確率で後攻のチームが勝つ。
  • その後、勝負した 2 チームは 1 つのチームに併合される。

なお、各試合の対戦結果は他の試合の対戦結果とは独立です。

N 人のプレイヤーそれぞれについて、大会全体で自分が所属するチームが勝つという出来事が起こる回数の期待値 \text{mod } 998244353 を出力してください。

期待値 \text{mod } 998244353 の定義

この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq p_i, q_i \leq N
  • i 回目の試合の直前、プレイヤー p_i が属するチームとプレイヤー q_i が属するチームは異なる。
  • 入力はすべて整数

入力

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

N
p_1 q_1
p_2 q_2
\vdots
p_{N-1} q_{N-1}

出力

i = 1, 2, \ldots, N について、大会全体でプレイヤー i が所属するチームが勝つという出来事が起こる回数の期待値 \text{mod } 998244353 である E_i を、 下記の形式にしたがって空白区切りで出力せよ。

E_1 E_2 \ldots E_N

入力例 1

5
1 2
4 3
5 3
1 4

出力例 1

698771048 698771048 964969543 964969543 133099248

チームに所属するプレイヤーの番号が x_1, x_2, \ldots, x_k であるチームを、チーム \lbrace x_1, x_2, \ldots, x_k \rbrace と呼びます。

  • 1 回目の試合では、プレイヤー 1 が所属するチーム \lbrace 1 \rbrace とプレイヤー 2 が所属するチーム \lbrace 2 \rbrace が対戦し、 \frac{1}{2} の確率でチーム \lbrace 1 \rbrace が、\frac{1}{2} の確率でチーム \lbrace 2 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 1, 2 \rbrace になります。
  • 2 回目の試合では、プレイヤー 4 が所属するチーム \lbrace 4 \rbrace とプレイヤー 3 が所属するチーム \lbrace 3 \rbrace が対戦し、 \frac{1}{2} の確率でチーム \lbrace 4 \rbrace が、\frac{1}{2} の確率でチーム \lbrace 3 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 3, 4 \rbrace になります。
  • 3 回目の試合では、プレイヤー 5 が所属するチーム \lbrace 5 \rbrace とプレイヤー 3 が所属するチーム \lbrace 3, 4 \rbrace が対戦し、 \frac{1}{3} の確率でチーム \lbrace 5 \rbrace が、\frac{2}{3} の確率でチーム \lbrace 3, 4 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 3, 4, 5 \rbrace になります。
  • 4 回目の試合では、プレイヤー 1 が所属するチーム \lbrace 1, 2 \rbrace とプレイヤー 4 が所属するチーム \lbrace 3, 4, 5 \rbrace が対戦し、 \frac{2}{5} の確率でチーム \lbrace 1, 2 \rbrace が、\frac{3}{5} の確率でチーム \lbrace 3, 4, 5 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 1, 2, 3, 4, 5 \rbrace になります。

プレイヤー 1, 2, 3, 4, 5 それぞれの、大会全体で自分が所属するチームが勝つという出来事が起こる回数の期待値 E_1, E_2, E_3, E_4, E_5 は、それぞれ \frac{9}{10}, \frac{9}{10}, \frac{53}{30}, \frac{53}{30}, \frac{14}{15} です。


入力例 2

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

出力例 2

43970290 310168785 806914186 501498951 950708909 272140427 335124893 168750835 310168785 168750835 280459129 280459129 272140427 476542843 43970290

Score : 475 points

Problem Statement

N players, player 1, player 2, ..., player N, participate in a game tournament. Just before the tournament starts, each player forms a one-person team, so there are N teams in total.

The tournament has a total of N-1 matches. In each match, two different teams are chosen. One team goes first, and the other goes second. Each match will result in exactly one team winning. Specifically, for each i = 1, 2, \ldots, N-1, the i-th match proceeds as follows.

  • The team with player p_i goes first, and the team with player q_i goes second.
  • Let a and b be the numbers of players in the first and second teams, respectively. The first team wins with probability \frac{a}{a+b}, and the second team wins with probability \frac{b}{a+b}.
  • Then, the two teams are combined into a single team.

The result of each match is independent of those of the others.

For each of the N players, print the expected number of times the team with that player wins throughout the tournament, modulo 998244353.

How to print an expected value modulo 998244353

It can be proved that the sought expected value is always rational. Also, the constraints of this problem guarantee that if the sought expected value is expressed as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.

Now, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq p_i, q_i \leq N
  • Just before the i-th match, player p_i and player q_i belong to different teams.
  • All input values are integers.

Input

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

N
p_1 q_1
p_2 q_2
\vdots
p_{N-1} q_{N-1}

Output

For each i = 1, 2, \ldots, N, print E_i, the expected number, modulo 998244353, of times the team with player i wins throughout the tournament, separated by spaces, in the following format:

E_1 E_2 \ldots E_N

Sample Input 1

5
1 2
4 3
5 3
1 4

Sample Output 1

698771048 698771048 964969543 964969543 133099248

We call a team formed by player x_1, player x_2, \ldots, player x_k as team \lbrace x_1, x_2, \ldots, x_k \rbrace.

  • The first match is played by team \lbrace 1 \rbrace, with player 1, and team \lbrace 2 \rbrace, with player 2. Team \lbrace 1 \rbrace wins with probability \frac{1}{2}, and team \lbrace 2 \rbrace wins with probability \frac{1}{2}. Then, the two teams are combined into a single team \lbrace 1, 2 \rbrace.
  • The second match is played by team \lbrace 4 \rbrace, with player 4, and team \lbrace 3 \rbrace, with player 3. Team \lbrace 4 \rbrace wins with probability \frac{1}{2}, and team \lbrace 3 \rbrace wins with probability \frac{1}{2}. Then, the two teams are combined into a single team \lbrace 3, 4 \rbrace.
  • The third match is played by team \lbrace 5 \rbrace, with player 5, and team \lbrace 3, 4 \rbrace, with player 3. Team \lbrace 5 \rbrace wins with probability \frac{1}{3}, and team \lbrace 3, 4 \rbrace wins with probability \frac{2}{3}. Then, the two teams are combined into a single team \lbrace 3, 4, 5 \rbrace.
  • The fourth match is played by team \lbrace 1, 2 \rbrace, with player 1, and team \lbrace 3, 4, 5 \rbrace, with player 4. Team \lbrace 1, 2 \rbrace wins with probability \frac{2}{5}, and team \lbrace 3, 4, 5 \rbrace wins with probability \frac{3}{5}. Then, the two teams are combined into a single team \lbrace 1, 2, 3, 4, 5 \rbrace.

The expected numbers of times the teams with players 1, 2, 3, 4, 5 win throughout the tournament, E_1, E_2, E_3, E_4, E_5, are \frac{9}{10}, \frac{9}{10}, \frac{53}{30}, \frac{53}{30}, \frac{14}{15}, respectively.


Sample Input 2

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

Sample Output 2

43970290 310168785 806914186 501498951 950708909 272140427 335124893 168750835 310168785 168750835 280459129 280459129 272140427 476542843 43970290