A - Election 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

AtCoder 市では市長選挙が行われています。候補者は高橋氏と青木氏です。

2 人のどちらかに投じられた有効票は N 票あり、現在開票が行われています。なお、 N は奇数です。

現在の開票作業の途中経過は高橋氏に T 票、青木氏に A 票です。

現時点で勝敗が確定しているかを判定してください。

制約

  • 1 \leq N \leq 99
  • N は奇数
  • 0 \leq T,A \leq N
  • T+A \leq N
  • 入力はすべて整数

入力

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

N T A

出力

現時点で勝敗が確定しているならば Yes 、そうでなければ No と出力せよ。


入力例 1

7 4 2

出力例 1

Yes

残りの 1 票が青木氏に入っても、高橋氏は勝利します。高橋氏の勝利が確定しているため、Yes と出力します。


入力例 2

99 12 48

出力例 2

No

現時点では青木氏が多く票を獲得していますが、高橋氏が残りの 39 票を獲得すると高橋氏が勝利します。よって、No と出力します。


入力例 3

1 0 0

出力例 3

No

Score : 100 points

Problem Statement

A mayoral election is being held in AtCoder City. The candidates are Takahashi and Aoki.

There are N valid votes cast for either of the two candidates, and the counting is currently underway. Here, N is an odd number.

The current vote count is T votes for Takahashi and A votes for Aoki.

Determine if the outcome of the election is already decided at this point.

Constraints

  • 1 \leq N \leq 99
  • N is an odd number.
  • 0 \leq T, A \leq N
  • T + A \leq N
  • All input values are integers.

Input

The input is given from standard input in the following format:

N T A

Output

Print Yes if the outcome of the election is already decided, and No otherwise.


Sample Input 1

7 4 2

Sample Output 1

Yes

Even if the remaining one vote goes to Aoki, Takahashi will still win. That is, his victory is decided, so print Yes.


Sample Input 2

99 12 48

Sample Output 2

No

Although Aoki currently has more votes, Takahashi would win if he receives the remaining 39 votes. Therefore, print No.


Sample Input 3

1 0 0

Sample Output 3

No
B - Horizon

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

地上 x メートルの高さから見える水平線は \sqrt{x(12800000+x)} メートル先にあるとするとき、 地上 H メートルの高さから見える水平線が何メートル先にあるか求めてください。

制約

  • 1 \leq H \leq 10^5
  • H は整数である

入力

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

H

出力

答えを出力せよ。
なお、想定解との絶対誤差または相対誤差が 10^{-6} 以下であれば、正解として扱われる。


入力例 1

333

出力例 1

65287.907678222

\sqrt{333(12800000+333)} = 65287.9076782\ldots です。65287.91 などの出力でも正解となります。


入力例 2

634

出力例 2

90086.635834623

\sqrt{634(12800000+634)} = 90086.6358346\ldots です。

Score : 100 points

Problem Statement

Assuming that the horizon seen from a place x meters above the ground is \sqrt{x(12800000+x)} meters away, find how many meters away the horizon seen from a place H meters above the ground is.

Constraints

  • 1 \leq H \leq 10^5
  • H is an integer.

Input

Input is given from Standard Input in the following format:

H

Output

Print the answer.
Your answer will be considered correct when the absolute or relative error from the judge's answer is at most 10^{-6}.


Sample Input 1

333

Sample Output 1

65287.907678222

We have \sqrt{333(12800000+333)} = 65287.9076782\ldots. Outputs such as 65287.91 would also be accepted.


Sample Input 2

634

Sample Output 2

90086.635834623

We have \sqrt{634(12800000+634)} = 90086.6358346\ldots.

C - Ticket Counter

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

AtCoder Land の入り口には 1 つのチケット売り場があり、来園客はこのチケット売り場の前に一列に並んで順にチケットを購入します。 チケットの購入手続きには一人当たり A 秒かかり、列の先頭の人がチケットを購入し終わると、(存在すれば)次の人がすぐさま購入手続きを開始します。

現在チケット売り場に並んでいる人はおらず、今から N 人の人が順にチケットを買いに来ます。 具体的には、i 番目の人は今から T_i 秒後にチケット売り場を訪れ、既に列が存在すればその最後尾に並び、存在しなければすぐさま購入手続きを開始します。 ここで、T_1< T_2< \dots < T_N です。

各 i\ (1\leq i\leq N) について、i 番目の人がチケットを購入し終わるのは今から何秒後か求めてください。

制約

  • 1\leq N \leq 100
  • 0\leq T_1< T_2< \dots < T_N\leq 10^6
  • 1\leq A\leq 10^6
  • 入力は全て整数

入力

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

N A
T_1 T_2 \dots T_N

出力

N 行出力せよ。 i\ (1\leq i \leq N) 行目には、i 番目の人がチケットを購入し終わるのは今から何秒後かを整数として出力せよ。


入力例 1

3 4
0 2 10

出力例 1

4
8
14

時系列順に以下のように物事が進行します。

  • 0 秒後:1 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 2 秒後:2 番目の人がチケット売り場を訪れ、1 番目の人の後ろに並ぶ。
  • 4 秒後:1 番目の人がチケットを購入し終え、2 番目の人が購入手続きを開始する。
  • 8 秒後:2 番目の人がチケットを購入し終える。
  • 10 秒後:3 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 14 秒後:3 番目の人がチケットを購入し終える。

入力例 2

3 3
1 4 7

出力例 2

4
7
10

時系列順に以下のように物事が進行します。

  • 1 秒後:1 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 4 秒後:1 番目の人がチケットを購入し終えると同時に、2 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 7 秒後:2 番目の人がチケットを購入し終えると同時に、3 番目の人がチケット売り場を訪れ、チケットの購入手続きを開始する。
  • 10 秒後:3 番目の人がチケットを購入し終える。

入力例 3

10 50000
120190 165111 196897 456895 540000 552614 561627 743796 757613 991216

出力例 3

170190
220190
270190
506895
590000
640000
690000
793796
843796
1041216

Score : 200 points

Problem Statement

At the entrance of AtCoder Land, there is a single ticket booth where visitors line up to purchase tickets one by one. The purchasing process takes A seconds per person. Once the person at the front of the line finishes purchasing their ticket, the next person (if any) immediately starts their purchasing process.

Currently, there is no one in line at the ticket booth, and N people will come to buy tickets one after another. Specifically, the i-th person will arrive at the ticket booth T_i seconds from now. If there is already a line, they will join the end of it; if not, they will start the purchasing process immediately. Here, T_1 < T_2 < \dots < T_N.

For each i\ (1 \leq i \leq N), determine how many seconds from now the i-th person will finish purchasing their ticket.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq T_1 < T_2 < \dots < T_N \leq 10^6
  • 1 \leq A \leq 10^6
  • All input values are integers.

Input

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

N A
T_1 T_2 \dots T_N

Output

Print N lines. The i-th line should contain the number of seconds from now that the i-th person will finish purchasing their ticket.


Sample Input 1

3 4
0 2 10

Sample Output 1

4
8
14

The events proceed in the following order:

  • At 0 seconds: The 1st person arrives at the ticket booth and starts the purchasing process.
  • At 2 seconds: The 2nd person arrives at the ticket booth and joins the line behind the 1st person.
  • At 4 seconds: The 1st person finishes purchasing their ticket, and the 2nd person starts the purchasing process.
  • At 8 seconds: The 2nd person finishes purchasing their ticket.
  • At 10 seconds: The 3rd person arrives at the ticket booth and starts the purchasing process.
  • At 14 seconds: The 3rd person finishes purchasing their ticket.

Sample Input 2

3 3
1 4 7

Sample Output 2

4
7
10

The events proceed in the following order:

  • At 1 second: The 1st person arrives at the ticket booth and starts the purchasing process.
  • At 4 seconds: The 1st person finishes purchasing their ticket, and the 2nd person arrives at the ticket booth and starts the purchasing process.
  • At 7 seconds: The 2nd person finishes purchasing their ticket, and the 3rd person arrives at the ticket booth and starts the purchasing process.
  • At 10 seconds: The 3rd person finishes purchasing their ticket.

Sample Input 3

10 50000
120190 165111 196897 456895 540000 552614 561627 743796 757613 991216

Sample Output 3

170190
220190
270190
506895
590000
640000
690000
793796
843796
1041216
D - Everyone is Friends

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

1,2,\ldots,N の番号がついた N 人の人がいます。

M 回の舞踏会が行われました。 i (1\leq i \leq M) 回目の舞踏会には k_i 人が参加し、参加した人は人 x_{i,1},x_{i,2},\ldots,x_{i,k_i} でした。

どの二人も少なくとも 1 回同じ舞踏会に参加したか判定してください。

制約

  • 2\leq N \leq 100
  • 1\leq M \leq 100
  • 2\leq k_i \leq N
  • 1\leq x_{i,1}<x_{i,2}<\ldots < x_{i,k_i}\leq N
  • 入力は全て整数

入力

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

N M
k_1 x_{1,1} x_{1,2} \ldots x_{1,k_1}
\vdots
k_M x_{M,1} x_{M,2} \ldots x_{M,k_M}

出力

どの二人も少なくとも 1 回同じ舞踏会に参加した場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3 3
2 1 2
2 2 3
2 1 3

出力例 1

Yes

人 1 と人 2 は共に 1 回目の舞踏会に参加しています。

人 2 と人 3 は共に 2 回目の舞踏会に参加しています。

人 1 と人 3 は共に 3 回目の舞踏会に参加しています。

以上よりどの二人も少なくとも 1 回同じ舞踏会に参加したので、答えは Yes です。


入力例 2

4 2
3 1 2 4
3 2 3 4

出力例 2

No

人 1 と人 3 は 1 回も同じ舞踏会に参加していないので、答えは No です。

Score : 200 points

Problem Statement

There are N people numbered 1,2,\ldots,N.

M parties were held. k_i people attended the i-th (1\leq i \leq M) party, and they were People x_{i,1},x_{i,2},\ldots,x_{i,k_i}.

Determine if every two people attended the same party at least once.

Constraints

  • 2\leq N \leq 100
  • 1\leq M \leq 100
  • 2\leq k_i \leq N
  • 1\leq x_{i,1}<x_{i,2}<\ldots < x_{i,k_i}\leq N
  • All values in the input are integers.

Input

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

N M
k_1 x_{1,1} x_{1,2} \ldots x_{1,k_1}
\vdots
k_M x_{M,1} x_{M,2} \ldots x_{M,k_M}

Output

Print Yes if every two people attended the same party at least once; print No otherwise.


Sample Input 1

3 3
2 1 2
2 2 3
2 1 3

Sample Output 1

Yes

Both Person 1 and Person 2 attended the 1-st party.

Both Person 2 and Person 3 attended the 2-nd party.

Both Person 1 and Person 3 attended the 3-rd party.

Therefore, every two people attended the same party at least once, so the answer is Yes.


Sample Input 2

4 2
3 1 2 4
3 2 3 4

Sample Output 2

No

Person 1 and Person 3 did not attend the same party, so the answer is No.

E - Ideal Sheet

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

高橋君は黒いマスと透明なマスからなるシート A,B を 1 枚ずつと、透明なマスのみからなる無限に広がるシート C を持っています。
また、高橋君には黒いマスと透明なマスからなる、理想とするシート X が存在します。

シート A,B,X の大きさはそれぞれ縦 H_A マス \times 横 W_A マス、縦 H_B マス \times 横 W_B マス、縦 H_X マス \times 横 W_X マスです。
シート A の各マスは . と # からなる長さ W_A の文字列 H_A 個 A_1,A_2,\ldots,A_{H_A} によって表され、
A_i (1\leq i\leq H_A) の j 文字目 (1\leq j\leq W_A) が、 . のときシート A の上から i 行目かつ左から j 列目のマスは透明なマスであり、 # のとき黒いマスです。
シート B,X の各マスも、同様に長さ W_B の文字列 H_B 個 B_1,B_2,\ldots,B_{H_B} および長さ W_X の文字列 H_X 個 X_1,X_2,\ldots,X_{H_X} によって表されます。

高橋君の目標は、次の手順で、シート A,B,C から、A,B に存在する すべての黒いマスを使って シート X を作り出すことです。

  1. シート A,B をマス目に沿ってシート C に貼り付ける。この時、シート A,B はそれぞれ好きな場所に平行移動させて貼って良いが、シートを切り分けたり、回転させたりしてはいけない。
  2. シート C からマス目に沿って H_X\times W_X マスの領域を切り出す。ここで、切り出されたシートの各マスは、シート A または B の黒いマスが貼り付けられていれば黒いマスに、そうでなければ透明なマスとなる。

このとき、貼り付ける位置と切り出す領域をうまくとることで高橋君は目標を達成できるか、すなわち次の条件をともにみたすことにできるか判定してください。

  • 切り出されたシートはシート A,B の 黒いマスをすべて 含む。切り出されたシートの上でシート A,B の黒いマスどうしが重なって存在していても構わない。
  • 切り出されたシートは、回転させたり裏返したりすることなくシート X と一致する。

制約

  • 1\leq H_A,W_A,H_B,W_B,H_X,W_X\leq 10
  • H_A,W_A,H_B,W_B,H_X,W_X は整数
  • A_i は . と # のみからなる長さ W_A の文字列
  • B_i は . と # のみからなる長さ W_B の文字列
  • X_i は . と # のみからなる長さ W_X の文字列
  • シート A,B,X はそれぞれ少なくとも 1 つ以上の黒いマスを含む。

入力

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

出力

高橋君が問題文中の目標を達成できるならば Yes を、できないならば No を出力せよ。


入力例 1

3 5
#.#..
.....
.#...
2 2
#.
.#
5 3
...
#.#
.#.
.#.
...

出力例 1

Yes

まず、シート A をシート C に貼り付けると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

さらに、シート B をシート A と左上を合わせて貼ってみると下図のようになります。

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

ここで、上で具体的に図示されている範囲のうち、上から 1 行目かつ左から 2 列目のマスを左上として 5\times 3 マスを切り出すと下図のようになります。

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

これはシート A,B のすべての黒いマスを含んでおり、また、シート X と一致しているため条件を満たしています。

よって、Yes を出力します。


入力例 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

出力例 2

No

シート A や B を回転させて貼ってはいけないことに注意してください。


入力例 3

1 1
#
1 2
##
1 1
#

出力例 3

No

どのように貼ったり切り出したりしても、シート B の黒いマスをすべて含むように切り出すことはできないため、1 つめの条件をみたすことができません。 よって、No を出力します。


入力例 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

出力例 4

Yes

Score : 300 points

Problem Statement

Takahashi has two sheets A and B, each composed of black squares and transparent squares, and an infinitely large sheet C composed of transparent squares.
There is also an ideal sheet X for Takahashi composed of black squares and transparent squares.

The sizes of sheets A, B, and X are H_A rows \times W_A columns, H_B rows \times W_B columns, and H_X rows \times W_X columns, respectively.
The squares of sheet A are represented by H_A strings of length W_A, A_1, A_2, \ldots, A_{H_A} consisting of . and #.
If the j-th character (1\leq j\leq W_A) of A_i (1\leq i\leq H_A) is ., the square at the i-th row from the top and j-th column from the left is transparent; if it is #, that square is black.
Similarly, the squares of sheets B and X are represented by H_B strings of length W_B, B_1, B_2, \ldots, B_{H_B}, and H_X strings of length W_X, X_1, X_2, \ldots, X_{H_X}, respectively.

Takahashi's goal is to create sheet X using all black squares in sheets A and B by following the steps below with sheets A, B, and C.

  1. Paste sheets A and B onto sheet C along the grid. Each sheet can be pasted anywhere by translating it, but it cannot be cut or rotated.
  2. Cut out an H_X\times W_X area from sheet C along the grid. Here, a square of the cut-out sheet will be black if a black square of sheet A or B is pasted there, and transparent otherwise.

Determine whether Takahashi can achieve his goal by appropriately choosing the positions where the sheets are pasted and the area to cut out, that is, whether he can satisfy both of the following conditions.

  • The cut-out sheet includes all black squares of sheets A and B. The black squares of sheets A and B may overlap on the cut-out sheet.
  • The cut-out sheet coincides sheet X without rotating or flipping.

Constraints

  • 1\leq H_A, W_A, H_B, W_B, H_X, W_X\leq 10
  • H_A, W_A, H_B, W_B, H_X, W_X are integers.
  • A_i is a string of length W_A consisting of . and #.
  • B_i is a string of length W_B consisting of . and #.
  • X_i is a string of length W_X consisting of . and #.
  • Sheets A, B, and X each contain at least one black square.

Input

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

H_A W_A
A_1
A_2
\vdots
A_{H_A}
H_B W_B
B_1
B_2
\vdots
B_{H_B}
H_X W_X
X_1
X_2
\vdots
X_{H_X}

Output

If Takahashi can achieve the goal described in the problem statement, print Yes; otherwise, print No.


Sample Input 1

3 5
#.#..
.....
.#...
2 2
#.
.#
5 3
...
#.#
.#.
.#.
...

Sample Output 1

Yes

First, paste sheet A onto sheet C, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots.......\cdots
  ..#....  
  .......  
     \vdots

Next, paste sheet B so that its top-left corner aligns with that of sheet A, as shown in the figure below.

     \vdots
  .......  
  .#.#...  
\cdots..#....\cdots
  ..#....  
  .......  
     \vdots

Now, cut out a 5\times 3 area with the square in the first row and second column of the range illustrated above as the top-left corner, as shown in the figure below.

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

This includes all black squares of sheets A and B and matches sheet X, satisfying the conditions.

Therefore, print Yes.


Sample Input 2

2 2
#.
.#
2 2
#.
.#
2 2
##
##

Sample Output 2

No

Note that sheets A and B may not be rotated or flipped when pasting them.


Sample Input 3

1 1
#
1 2
##
1 1
#

Sample Output 3

No

No matter how you paste or cut, you cannot cut out a sheet that includes all black squares of sheet B, so you cannot satisfy the first condition. Therefore, print No.


Sample Input 4

3 3
###
...
...
3 3
#..
#..
#..
3 3
..#
..#
###

Sample Output 4

Yes
F - Paint to make a rectangle

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

H 行 W 列のマス目が与えられます。
以下、上から i 行目 (1\leq i\leq H) かつ左から j 列目 (1\leq j\leq W) のマスをマス (i,j) で表します。
マス目の状態は H 個の長さ W の文字列 S_1,S_2, \ldots, S_H によって以下のように表されます。

  • S_i の j 文字目が # のとき、マス (i,j) は黒く塗られている。
  • S_i の j 文字目が . のとき、マス (i,j) は白く塗られている。
  • S_i の j 文字目が ? のとき、マス (i,j) は塗られていない。

高橋君はまだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにしたいです。
より具体的には、ある 4 つの整数の組 (a,b,c,d) (1\leq a\leq b\leq H, 1\leq c\leq d\leq W) が存在して、次が成り立つようにしたいです。

マス (i,j) (1\leq i\leq H, 1\leq j\leq W) は、 a\leq i\leq b かつ c\leq j\leq d をみたすとき、黒く塗られている。
そうでないとき、白く塗られている。

そのようなことが可能か判定してください。

制約

  • 1\leq H,W\leq 1000
  • H, W は整数
  • S_i は #, ., ? のみからなる長さ W の文字列
  • 黒く塗られたマスが 1 つ以上存在する。

入力

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

H W
S_1
S_2
\vdots
S_H

出力

まだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにできるならば Yes を、そうでないならば No を出力せよ。


入力例 1

3 5
.#?#.
.?#?.
?...?

出力例 1

Yes

マス目は以下の状態になっています。? のマスがまだ塗られていないマスです。

マス (1,3), (2,2), (2,4) を黒く塗り、マス (3,1), (3,5) を白く塗ることで、 以下のように黒マス全体が長方形をなすようにできます。

よって、Yes を出力します。


入力例 2

3 3
?##
#.#
##?

出力例 2

No

黒マス全体が長方形をなすためには、少なくともマス (2,2) を黒く塗る必要がありますがすでに白く塗られています。
よって、黒マス全体が長方形をなすようにマス目を塗ることはできないため、No を出力します。


入力例 3

1 1
#

出力例 3

Yes

Score : 300 points

Problem Statement

You are given a grid of H rows and W columns.
Let (i,j) denote the cell at row i (1 \leq i \leq H) from the top and column j (1 \leq j \leq W) from the left.
The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W, as follows:

  • If the j-th character of S_i is #, cell (i,j) is painted black.
  • If the j-th character of S_i is ., cell (i,j) is painted white.
  • If the j-th character of S_i is ?, cell (i,j) is not yet painted.

Takahashi wants to paint each not-yet-painted cell white or black so that all the black cells form a rectangle.
More precisely, he wants there to exist a quadruple of integers (a,b,c,d) (1 \leq a \leq b \leq H, 1 \leq c \leq d \leq W) such that:

For each cell (i,j) (1 \leq i \leq H, 1 \leq j \leq W), if a \leq i \leq b and c \leq j \leq d, the cell is black;
otherwise, the cell is white.

Determine whether this is possible.

Constraints

  • 1 \leq H, W \leq 1000
  • H and W are integers.
  • Each S_i is a string of length W consisting of #, ., ?.
  • There is at least one cell that is already painted black.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

If it is possible to paint all the not-yet-painted cells so that the black cells form a rectangle, print Yes; otherwise, print No.


Sample Input 1

3 5
.#?#.
.?#?.
?...?

Sample Output 1

Yes

The grid is in the following state. ? indicates a cell that are not yet painted.

By painting cells (1,3), (2,2), and (2,4) black and cells (3,1) and (3,5) white, the black cells can form a rectangle as follows:

Therefore, print Yes.


Sample Input 2

3 3
?##
#.#
##?

Sample Output 2

No

To form a rectangle with all black cells, you would need to paint cell (2,2) black, but it is already painted white.
Therefore, it is impossible to make all black cells form a rectangle, so print No.


Sample Input 3

1 1
#

Sample Output 3

Yes
G - XOR Shortest Walk

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

頂点に 1 から N の、辺に 1 から M の番号がついたN 頂点 M 辺の有向グラフがあります。辺 i は頂点 A_i から頂点 B_i への重み W_i の有向辺です。

頂点 1 から頂点 N への walk のうち、walk に含まれる辺の重みのビット単位 \mathrm{XOR} の最小値を求めてください。

頂点 1 から頂点 N への walk とは

直感的には、「頂点 1 から頂点 N への経路であって、同じ頂点や同じ辺を複数回通っても良いもの」です。 正確には、辺の列 (e_1,\ldots,e_k) であって、以下の条件を全て満たすものです。

  • e_1 は頂点 1 を始点とする。
  • 全ての 1 \leq i < k について、e_i の終点と e_{i+1} の始点は一致する。
  • e_k は頂点 N を終点とする。

ビット単位 \mathrm{XOR} 演算とは

非負整数 A, B のビット単位 \mathrm{XOR} 、A\ \mathrm{XOR}\ B は、以下のように定義されます。

  • A\ \mathrm{XOR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3\ \mathrm{XOR}\ 5 = 6 となります (二進表記すると: 011\ \mathrm{XOR}\ 101 = 110)。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR} は (\dots ((p_1\ \mathrm{XOR}\ p_2)\ \mathrm{XOR}\ p_3)\ \mathrm{XOR}\ \dots\ \mathrm{XOR}\ p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。

制約

  • 2 \leq N \leq 1000
  • 0 \leq M \leq 1000
  • 1 \leq A_i,B_i \leq N
  • 0 \leq W_i < 2^{10}
  • 入力は全て整数

入力

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

N M
A_1 B_1 W_1
A_2 B_2 W_2
\vdots
A_M B_M W_M

出力

頂点 1 から頂点 N への walk が存在しない場合、-1 と出力せよ。

頂点 1 から頂点 N への walk が存在する場合、そのうち、walk に含まれる辺の重みのビット単位 \mathrm{XOR} の最小値を出力せよ。


入力例 1

3 3
1 2 4
2 3 5
1 3 2

出力例 1

1

(辺 1, 辺 2) という walk に含まれる辺の重みのビット単位 \mathrm{XOR} は 1 となります。


入力例 2

4 4
1 4 7
4 2 2
2 3 4
3 4 1

出力例 2

0

(辺 1, 辺 2, 辺 3, 辺 4) という walk に含まれる辺の重みのビット単位 \mathrm{XOR} は 0 となります。

途中に頂点 N を含んでも良いことに注意してください。


入力例 3

999 4
1 2 9
2 1 8
1 2 7
1 1 6

出力例 3

-1

頂点 1 から頂点 N への walk が存在しない場合、-1 と出力してください。

Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges, where vertices are numbered from 1 to N and edges are numbered from 1 to M. Edge i is a directed edge from vertex A_i to vertex B_i with weight W_i.

Find the minimum value of the bitwise \mathrm{XOR} of the weights of edges included in a walk from vertex 1 to vertex N.

What is a walk from vertex 1 to vertex N?

Intuitively, it is "a path from vertex 1 to vertex N that may visit the same vertex or edge multiple times." Formally, it is a sequence of edges (e_1,\ldots,e_k) that satisfies all of the following conditions:

  • e_1 starts at vertex 1.
  • For all 1 \leq i < k, the endpoint of e_i and the starting point of e_{i+1} are the same.
  • e_k ends at vertex N.

What is the bitwise \mathrm{XOR} operation?

The bitwise \mathrm{XOR} of non-negative integers A and B, denoted A\ \mathrm{XOR}\ B, is defined as follows:

  • When A\ \mathrm{XOR}\ B is written in binary, the digit at the 2^k place (k \geq 0) is 1 if exactly one of the digits at the 2^k place of A and B in binary is 1, and 0 otherwise.
For example, 3\ \mathrm{XOR}\ 5 = 6 (in binary: 011\ \mathrm{XOR}\ 101 = 110).
In general, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1\ \mathrm{XOR}\ p_2)\ \mathrm{XOR}\ p_3)\ \mathrm{XOR}\ \dots\ \mathrm{XOR}\ p_k), and it can be proved that this does not depend on the order of p_1, p_2, p_3, \dots p_k.

Constraints

  • 2 \leq N \leq 1000
  • 0 \leq M \leq 1000
  • 1 \leq A_i,B_i \leq N
  • 0 \leq W_i < 2^{10}
  • All input values are integers.

Input

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

N M
A_1 B_1 W_1
A_2 B_2 W_2
\vdots
A_M B_M W_M

Output

If there is no walk from vertex 1 to vertex N, output -1.

If there is a walk from vertex 1 to vertex N, output the minimum value of the bitwise \mathrm{XOR} of the weights of edges included in such a walk.


Sample Input 1

3 3
1 2 4
2 3 5
1 3 2

Sample Output 1

1

The bitwise \mathrm{XOR} of the weights of edges included in the walk (edge 1, edge 2) is 1.


Sample Input 2

4 4
1 4 7
4 2 2
2 3 4
3 4 1

Sample Output 2

0

The bitwise \mathrm{XOR} of the weights of edges included in the walk (edge 1, edge 2, edge 3, edge 4) is 0.

Note that the walk may include vertex N in the middle.


Sample Input 3

999 4
1 2 9
2 1 8
1 2 7
1 1 6

Sample Output 3

-1

If there is no walk from vertex 1 to vertex N, output -1.

H - Revenge of "The Salary of AtCoder Inc."

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 450 点

問題文

AtCoder社の社員である青木さんの今月の給料は、整数 N と長さ N の数列 A を用いて以下のように決められます。
まず、青木さんに 1 から N までの整数が等確率で出る N 面ダイスと変数 x=0 を渡します。

その後、以下の手順を終了まで繰り返します。

  • ダイスを 1 度振り、出た目を y とする。
    • もし x<y なら A_y 円支給し、 x=y と更新する。
    • そうでないなら終了する。

青木さんの今月の給料は、この手順によって支給された金額の合計です。
青木さんの今月の給料の期待値を {}\bmod{998244353} で求めてください。

期待値 {}\bmod{998244353} の定義 この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac yx で表したときに x が 998244353 で割り切れないことが保証されます。 このとき、y\equiv xz\pmod{998244353} を満たす 0\leq z\lt998244353 がただ一つ存在するので、z を出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 0 \le A_i < 998244353

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
3 2 6

出力例 1

776412280

手順の一例は、以下の通りです。

  • 最初、 x=0 である。
  • ダイスを 1 度振り、出た目が 1 であった。 0<1 であるため、 A_1 = 3 円支給し、 x=1 とする。
  • ダイスを 1 度振り、出た目が 3 であった。 1<3 であるため、 A_3 = 6 円支給し、 x=3 とする。
  • ダイスを 1 度振り、出た目が 1 であった。 3 \ge 1 であるため、終了する。

この例では、青木さんの今月の給料は 9 円です。

なお、青木さんの今月の給料の期待値は \frac{49}{9} 円と求めることができ、これを {}\bmod{998244353} 上で表現すると 776412280 となります。


入力例 2

1
998244352

出力例 2

998244352

入力例 3

9
3 14 159 2653 58979 323846 2643383 27950288 419716939

出力例 3

545252774

Score : 450 points

Problem Statement

Aoki, an employee at AtCoder Inc., has his salary for this month determined by an integer N and a sequence A of length N as follows.
First, he is given an N-sided die (dice) that shows the integers from 1 to N with equal probability, and a variable x=0.

Then, the following steps are repeated until terminated.

  • Roll the die once and let y be the result.
    • If x<y, pay him A_y yen and let x=y.
    • Otherwise, terminate the process.

Aoki's salary for this month is the total amount paid through this process.
Find the expected value of Aoki's salary this month, modulo 998244353.

How to find an expected value modulo 998244353 It can be proved that the sought expected value in this problem is always a rational number. Also, the constraints of this problem guarantee that if the sought expected value is expressed as a reduced fraction \frac yx, then x is not divisible by 998244353. Here, there is exactly one 0\leq z\lt998244353 such that y\equiv xz\pmod{998244353}. Print this z.

Constraints

  • All inputs are integers.
  • 1 \le N \le 3 \times 10^5
  • 0 \le A_i < 998244353

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3
3 2 6

Sample Output 1

776412280

Here is an example of how the process goes.

  • Initially, x=0.
  • Roll the die once, and it shows 1. Since 0<1, pay him A_1 = 3 yen and let x=1.
  • Roll the die once, and it shows 3. Since 1<3, pay him A_3 = 6 yen and let x=3.
  • Roll the die once, and it shows 1. Since 3 \ge 1, terminate the process.

In this case, his salary for this month is 9 yen.

It can be calculated that the expected value of his salary this month is \frac{49}{9} yen, whose representation modulo 998244353 is 776412280.


Sample Input 2

1
998244352

Sample Output 2

998244352

Sample Input 3

9
3 14 159 2653 58979 323846 2643383 27950288 419716939

Sample Output 3

545252774
I - Manhattan Christmas Tree 2

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 500 点

問題文

二次元平面上に N 個のクリスマスツリーがあります。i 番目 (1\le i\le N) のクリスマスツリーは座標 (X_i,Y_i) に存在します。

Q 個のクエリが与えられるので、順にクエリを処理してください。各クエリは、以下のいずれかです。

  • タイプ 1 : 1 i x y の形式で与えられる。i 番目のクリスマスツリーの座標を (x,y) に変更する。
  • タイプ 2 : 2 L R x y の形式で与えられる。L,L+1,\ldots,R 番目のクリスマスツリーのうち、座標 (x,y) からマンハッタン距離で最も遠いクリスマスツリーまでの距離を出力する。

ただし、座標 (x_1,y_1) と座標 (x_2,y_2) のマンハッタン距離は |x_1-x_2|+|y_1-y_2| で定義されます。

制約

  • 1\le N,Q\le 2\times 10^5
  • -10^9\le X_i,Y_i\le 10^9
  • 1\le i\le N
  • 1\le L\le R\le N
  • -10^9\le x,y\le 10^9
  • 入力される値は全て整数

入力

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

N Q
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ここで、 i 番目のクエリ \text{query}_i は以下のいずれかの形式で与えられる。

1 i x y
2 L R x y

出力

問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。


入力例 1

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

出力例 1

3
3
2

はじめ、1,2,3 番目のクリスマスツリーはそれぞれ座標 (-1,-1),(1,2),(-2,1) に存在します。

各クエリは以下のように処理されます。

  • 1,2 番目のクリスマスツリーと座標 (0,0) のマンハッタン距離はそれぞれ 2,3 です。したがって、 2,3 の最大値である 3 を出力します。
  • 1,2,3 番目のクリスマスツリーと座標 (-1,2) のマンハッタン距離はそれぞれ 3,2,2 です。したがって、 3,2,2 の最大値である 3 を出力します。
  • 1 番目のクリスマスツリーの座標を (0,1) に変更します。1,2,3 番目のクリスマスツリーの座標はそれぞれ (0,1),(1,2),(-2,1) になります。
  • 1,2,3 番目のクリスマスツリーと座標 (-1,2) のマンハッタン距離はそれぞれ 2,2,2 です。したがって、 2,2,2 の最大値である 2 を出力します。

入力例 2

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

出力例 2

24
24
22
30
9

Score : 500 points

Problem Statement

There are N Christmas trees on a two-dimensional plane. The i-th (1\le i\le N) Christmas tree is located at coordinates (X_i,Y_i).

You are given Q queries. Process the queries in order. Each query is one of the following:

  • Type 1 : Given in the form 1 i x y. Change the coordinates of the i-th Christmas tree to (x,y).
  • Type 2 : Given in the form 2 L R x y. Output the Manhattan distance from the coordinates (x,y) to the farthest Christmas tree among the L,L+1,\ldots,R-th Christmas trees.

Here, the Manhattan distance between coordinates (x_1,y_1) and coordinates (x_2,y_2) is defined as |x_1-x_2|+|y_1-y_2|.

Constraints

  • 1\le N,Q\le 2\times 10^5
  • -10^9\le X_i,Y_i\le 10^9
  • 1\le i\le N
  • 1\le L\le R\le N
  • -10^9\le x,y\le 10^9
  • All input values are integers.

Input

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

N Q
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, the i-th query \text{query}_i is given in one of the following formats:

1 i x y
2 L R x y

Output

Output the answers to the queries, separated by newlines, according to the instructions in the problem statement.


Sample Input 1

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

Sample Output 1

3
3
2

Initially, the 1st, 2nd, 3rd Christmas trees are located at coordinates (-1,-1), (1,2), (-2,1), respectively.

Each query is processed as follows:

  • The Manhattan distances from the 1st and 2nd Christmas trees to coordinates (0,0) are 2 and 3, respectively. Thus, output 3, which is the maximum value among 2,3.
  • The Manhattan distances from the 1st, 2nd, 3rd Christmas trees to coordinates (-1,2) are 3, 2, 2, respectively. Thus, output 3, which is the maximum value among 3,2,2.
  • Change the coordinates of the 1st Christmas tree to (0,1). The coordinates of the 1st, 2nd, 3rd Christmas trees become (0,1),(1,2),(-2,1), respectively.
  • The Manhattan distances from the 1st, 2nd, 3rd Christmas trees to coordinates (-1,2) are 2, 2, 2, respectively. Thus, output 2, which is the maximum value among 2,2,2.

Sample Input 2

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

Sample Output 2

24
24
22
30
9