E - Socks 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

高橋君は N 組の靴下を持っており、i 番目の組は色 i の靴下 2 枚からなります。 ある日タンスの中を整理した高橋君は、色 A_1,A_2,\dots,A_K の靴下を 1 枚ずつなくしてしまったことに気づいたので、残っている 2N-K 枚の靴下を使って、靴下 2 枚ずつからなる \lfloor\frac{2N-K}{2}\rfloor 個の組を新たに作り直すことにしました。 色 i の靴下と色 j の靴下からなる組の奇妙さ|i-j| として定義され、高橋君は奇妙さの総和をできるだけ小さくしたいです。

残っている靴下をうまく組み合わせて \lfloor\frac{2N-K}{2}\rfloor 個の組を作ったとき、奇妙さの総和が最小でいくつになるか求めてください。 なお、2N-K が奇数のとき、どの組にも含まれない靴下が 1 枚存在することに注意してください。

制約

  • 1\leq K\leq N \leq 2\times 10^5
  • 1\leq A_1 < A_2 < \dots < A_K \leq N
  • 入力は全て整数

入力

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

N K
A_1 A_2 \dots A_K

出力

奇妙さの総和の最小値を整数として出力せよ。


入力例 1

4 2
1 3

出力例 1

2

以下、色 i の靴下と色 j の靴下からなる組を (i,j) と表記します。

1,2,3,4 の靴下がそれぞれ 1,2,1,2 枚ずつあります。 (1,2),(2,3),(4,4)3 組を作ると、奇妙さの総和は |1-2|+|2-3|+|4-4|=2 となり、これが最小です。


入力例 2

5 1
2

出力例 2

0

(1,1),(3,3),(4,4),(5,5)4 組を作り、色 2 の靴下を 1 枚余らせる(どの組にも入れない)のが最適です。


入力例 3

8 5
1 2 4 7 8

出力例 3

2

Score : 350 points

Problem Statement

Takahashi has N pairs of socks, and the i-th pair consists of two socks of color i. One day, after organizing his chest of drawers, Takahashi realized that he had lost one sock each of colors A_1, A_2, \dots, A_K, so he decided to use the remaining 2N-K socks to make \lfloor\frac{2N-K}{2}\rfloor new pairs of socks, each pair consisting of two socks. The weirdness of a pair of a sock of color i and a sock of color j is defined as |i-j|, and Takahashi wants to minimize the total weirdness.

Find the minimum possible total weirdness when making \lfloor\frac{2N-K}{2}\rfloor pairs from the remaining socks. Note that if 2N-K is odd, there will be one sock that is not included in any pair.

Constraints

  • 1\leq K\leq N \leq 2\times 10^5
  • 1\leq A_1 < A_2 < \dots < A_K \leq N
  • All input values are integers.

Input

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

N K
A_1 A_2 \dots A_K

Output

Print the minimum total weirdness as an integer.


Sample Input 1

4 2
1 3

Sample Output 1

2

Below, let (i,j) denote a pair of a sock of color i and a sock of color j.

There are 1, 2, 1, 2 socks of colors 1, 2, 3, 4, respectively. Creating the pairs (1,2),(2,3),(4,4) results in a total weirdness of |1-2|+|2-3|+|4-4|=2, which is the minimum.


Sample Input 2

5 1
2

Sample Output 2

0

The optimal solution is to make the pairs (1,1),(3,3),(4,4),(5,5) and leave one sock of color 2 as a surplus (not included in any pair).


Sample Input 3

8 5
1 2 4 7 8

Sample Output 3

2
F - Medicine

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

高橋君は医者のすぬけ君から N 種類の薬を処方されました。i 種類目の薬は(処方された日を含めて) a_i 日間、毎日 b_i 錠ずつ飲む必要があります。また、高橋君はこれ以外の薬を飲む必要がありません。

薬を処方された日を 1 日目とします。1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのは何日目かを求めてください。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq K \leq 10^9
  • 1 \leq a_i,b_i \leq 10^9
  • 入力はすべて整数

入力

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

N K
a_1 b_1
\vdots
a_N b_N

出力

1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのが X 日目の時、 X を出力せよ。


入力例 1

4 8
6 3
2 5
1 9
4 2

出力例 1

3

1 日目には、高橋君は 1,2,3,4 種類目の薬をそれぞれ 3,5,9,2 錠飲む必要があります。よってこの日は 19 錠飲む必要があり、K(=8) 錠以下ではありません。
2 日目には、高橋君は 1,2,4 種類目の薬をそれぞれ 3,5,2 錠飲む必要があります。よってこの日は 10 錠飲む必要があり、K(=8) 錠以下ではありません。
3 日目には、高橋君は 1,4 種類目の薬をそれぞれ 3,2 錠飲む必要があります。よってこの日は 5 錠飲む必要があり、初めて K(=8) 錠以下になります。

以上より、3 が答えです。


入力例 2

4 100
6 3
2 5
1 9
4 2

出力例 2

1

入力例 3

15 158260522
877914575 2436426
24979445 61648772
623690081 33933447
476190629 62703497
211047202 71407775
628894325 31963982
822804784 50968417
430302156 82631932
161735902 80895728
923078537 7723857
189330739 10286918
802329211 4539679
303238506 17063340
492686568 73361868
125660016 50287940

出力例 3

492686569

Score : 350 points

Problem Statement

Snuke the doctor prescribed N kinds of medicine for Takahashi. For the next a_i days (including the day of the prescription), he has to take b_i pills of the i-th medicine. He does not have to take any other medicine.

Let the day of the prescription be day 1. On or after day 1, when is the first day on which he has to take K pills or less?

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq K \leq 10^9
  • 1 \leq a_i,b_i \leq 10^9
  • All input values are integers.

Input

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

N K
a_1 b_1
\vdots
a_N b_N

Output

If Takahashi has to take K pills or less on day X for the first time on or after day 1, print X.


Sample Input 1

4 8
6 3
2 5
1 9
4 2

Sample Output 1

3

On day 1, he has to take 3,5,9, and 2 pills of the 1-st, 2-nd, 3-rd, and 4-th medicine, respectively. In total, he has to take 19 pills on this day, which is not K(=8) pills or less.
On day 2, he has to take 3,5, and 2 pills of the 1-st, 2-nd, and 4-th medicine, respectively. In total, he has to take 10 pills on this day, which is not K(=8) pills or less.
On day 3, he has to take 3 and 2 pills of the 1-st and 4-th medicine, respectively. In total, he has to take 5 pills on this day, which is K(=8) pills or less for the first time.

Thus, the answer is 3.


Sample Input 2

4 100
6 3
2 5
1 9
4 2

Sample Output 2

1

Sample Input 3

15 158260522
877914575 2436426
24979445 61648772
623690081 33933447
476190629 62703497
211047202 71407775
628894325 31963982
822804784 50968417
430302156 82631932
161735902 80895728
923078537 7723857
189330739 10286918
802329211 4539679
303238506 17063340
492686568 73361868
125660016 50287940

Sample Output 3

492686569
G - Strange Balls

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は 2 以上の整数が書かれた N 個のボールを持っており、これらを細長い筒の中に落としていきます。i \, (1 \leq i \leq N) 回目には、a_i が書かれたボールを落とします。

ボールは特殊な材質でできており、筒の中において k \, (k \geq 2) が書かれたボールが k 個連続すると、それら k 個のボールは全て消えてしまいます。

i \, (1 \leq i \leq N) について、i 個目のボールを筒の中に落とした後、筒の中に何個のボールがあるか求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq a_i \leq 2 \times 10^5 \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N
a_1 \ldots a_N

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、i 個目のボールを筒の中に落とした後、筒の中にあるボールの個数を出力せよ。


入力例 1

5
3 2 3 2 2

出力例 1

1
2
3
4
3

筒の中は次のように変化します。

  • 1 個目のボールを落とす。筒の中にあるボールに書かれた整数は 3 である。
  • 2 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2 である。
  • 3 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3 である。
  • 4 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3, 2 である。
  • 5 個目のボールを落とす。筒の中にあるボールに書かれた整数は下から順に 3, 2, 3, 2, 2 となるが、2 が書かれたボールが 2 個連続しているのでこれらは消え、下から順に 3, 2, 3 となる。


入力例 2

10
2 3 2 3 3 3 2 3 3 2

出力例 2

1
2
3
4
5
3
2
3
1
0

Score : 400 points

Problem Statement

Takahashi has N balls. Each ball has an integer not less than 2 written on it. He will insert them in a cylinder one by one. The integer written on the i-th ball is a_i.

The balls are made of special material. When k balls with k (k \geq 2) written on them line up in a row, all these k balls will disappear.

For each i (1 \leq i \leq N), find the number of balls after inserting the i-th ball.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq a_i \leq 2 \times 10^5 \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the number of balls after inserting the i-th ball.


Sample Input 1

5
3 2 3 2 2

Sample Output 1

1
2
3
4
3

The content of the cylinder changes as follows.

  • After inserting the 1-st ball, the cylinder contains the ball with 3.
  • After inserting the 2-nd ball, the cylinder contains 3, 2 from bottom to top.
  • After inserting the 3-rd ball, the cylinder contains 3, 2, 3 from bottom to top.
  • After inserting the 4-th ball, the cylinder contains 3, 2, 3, 2 from bottom to top.
  • After inserting the 5-th ball, the cylinder momentarily has 3, 2, 3, 2, 2 from bottom to top. The two consecutive balls with 2 disappear, and the cylinder eventually contains 3, 2, 3 from bottom to top.


Sample Input 2

10
2 3 2 3 3 3 2 3 3 2

Sample Output 2

1
2
3
4
5
3
2
3
1
0
H - Don't Isolate Elements

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

各要素の値が 0 または 1 である HW 列の行列 A が与えられます。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i,j) について、Ai 行目 j 列目の要素を A_{i,j} で表します。

行列 A に対し、以下の操作を 0 回以上の好きな回数行うことができます。

  • 1 \leq i \leq H を満たす整数 i を選び、1 \leq j \leq W を満たす全ての整数 j に対して A_{i,j} の値を 1-A_{i,j} で置き換える。

また、A_{i,j} は行列において上下左右に同じ値が存在しない、すなわち 4 つの整数組 (x,y) = (i-1,j),(i+1,j),(i,j-1),(i,j+1) のいずれかであって、 1 \leq x \leq H, 1 \leq y \leq W かつ A_{i,j} = A_{x,y} を満たすものが存在しないとき、またそのときに限り孤立した要素であると定義されます。

操作を繰り返し行列 A の任意の要素が孤立した要素でない状態にすることが可能か判定し、可能な場合は行う操作回数の最小値を求めてください。

制約

  • 2 \leq H,W \leq 1000
  • A_{i,j} = 0 または A_{i,j} = 1
  • 入力はすべて整数

入力

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

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W} 
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W} 

出力

操作を繰り返すことにより孤立した要素が存在しないようにできる場合は操作回数の最小値を、できない場合は -1 を出力せよ。


入力例 1

3 3
1 1 0
1 0 1
1 0 0

出力例 1

1

i = 1 を選択し操作を行うと、A = ((0,0,1),(1,0,1),(1,0,0)) となり、孤立した要素は存在しなくなります。


入力例 2

4 4
1 0 0 0
0 1 1 1
0 0 1 0
1 1 0 1

出力例 2

2

入力例 3

2 3
0 1 0
0 1 1

出力例 3

-1

Score : 500 points

Problem Statement

You are given a matrix A with H rows and W columns. The value of each of its elements is 0 or 1. For an integer pair (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, we denote by A_{i,j} the element at the i-th row and j-th column.

You can perform the following operation on the matrix A any number of times (possibly zero):

  • Choose an integer i such that 1 \leq i \leq H. For every integer j such that 1 \leq j \leq W, replace the value of A_{i,j} with 1-A_{i,j}.

A_{i,j} is said to be isolated if and only if there is no adjacent element with the same value; in other words, if and only if none of the four integer pairs (x,y) = (i-1,j),(i+1,j),(i,j-1),(i,j+1) satisfies 1 \leq x \leq H, 1 \leq y \leq W, and A_{i,j} = A_{x,y}.

Determine if you can make the matrix A in such a state that no element is isolated by repeating the operation. If it is possible, find the minimum number of operations required to do so.

Constraints

  • 2 \leq H,W \leq 1000
  • A_{i,j} = 0 or A_{i,j} = 1
  • All values in the input are integers.

Input

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

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W} 
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W} 

Output

If you can make it in such a state that no element is isolated by repeating the operation, print the minimum number of operations required to do so; otherwise, print -1.


Sample Input 1

3 3
1 1 0
1 0 1
1 0 0

Sample Output 1

1

An operation with i = 1 makes A = ((0,0,1),(1,0,1),(1,0,0)), where there is no longer an isolated element.


Sample Input 2

4 4
1 0 0 0
0 1 1 1
0 0 1 0
1 1 0 1

Sample Output 2

2

Sample Input 3

2 3
0 1 0
0 1 1

Sample Output 3

-1
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