A - Four TSP

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

配点 : 400

問題文

1, 2, 3, 44 頂点からなる完全グラフがあります。

あなたはこれから 6 つの各辺に重みを割り当てます。いずれの重みも正整数であり、6 つの重みの和がちょうど K になるように割り当てます。
より形式的には、\sum_{1\leq i<j\leq 4}x_{i,j}=K となる正整数 x_{i,j} \ (1\leq i<j\leq 4) を選び、i, j を結ぶ辺に重み x_{i,j} を割り当てます。

この辺に重みをつけたグラフ G全ての頂点を通る サイクルにおける辺の重みの総和の最小値を f(G) とします。 考えうる全ての G に対する f(G) の総和を 998244353 で割ったあまりを求めて下さい。

完全グラフとは

任意の異なる 2 頂点間に辺があるグラフを完全グラフと言います。

制約

  • 6 \le K \le 5000
  • 入力される値は全て整数

入力

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

K

出力

答えを出力せよ。


入力例 1

7

出力例 1

24

6 つの辺のうち 1 つの重みが 2 であり、それ以外の辺の重みが 1 であるようなグラフが考えられます。 そのようなグラフ G6 通りあり、いずれの場合も f(G)=4 であるため、答えは 24 です。


入力例 2

2026

出力例 2

513760748

Score : 400 points

Problem Statement

There is a complete graph with four vertices numbered 1, 2, 3, 4.

You will now assign weights to each of the six edges. Each weight should be a positive integer, and the sum of the six weights should be exactly K.
More formally, you choose positive integers x_{i,j} \ (1\leq i<j\leq 4) such that \sum_{1\leq i<j\leq 4}x_{i,j}=K, and assign weight x_{i,j} to the edge connecting i and j.

For this weighted graph G, let f(G) be the minimum sum of edge weights in a cycle that passes through all vertices. Find the sum, modulo 998244353, of f(G) over all possible G.

What is a complete graph?

A complete graph is a graph with an edge between every pair of two distinct vertices.

Constraints

  • 6 \le K \le 5000
  • All input values are integers.

Input

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

K

Output

Output the answer.


Sample Input 1

7

Sample Output 1

24

The possible graphs are the ones where one of the six edges has weight 2 and the other edges have weight 1. There are six such graphs G, and we have f(G)=4 for each of them, so the answer is 24.


Sample Input 2

2026

Sample Output 2

513760748
B - Stones on Grid

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

配点 : 500

問題文

NN 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と呼びます。 あなたは i=1,2,\ldots,M の順に以下を行います。

  • 操作 i : マス (x_i, y_i) に石を 1 つ置くか、石を置かないかどちらかを選ぶ。石を置いた場合はコストが c_i かかり、置かなかった場合はコストがかからない。

ただし、「操作 1 では必ず石を置く」という条件を守る必要があります。

以下の目標を達成できるか判定し、達成できる場合はかかるコストの和の最小値を求めてください。

  • 目標 : 任意の i \ (1 \leq i \leq N) について、i 行目に置かれた石の数と i 列目に置かれた石の数が等しい。

制約

  • 1 \le N, M \le 2 \times 10^5
  • 1 \le x_i, y_i \le N
  • 1 \le c_i \le 10^9
  • (x_i, y_i) は相異なる
  • 入力される値は全て整数

入力

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

N M
x_1 y_1 c_1
x_2 y_2 c_2
\vdots
x_M y_M c_M

出力

目標を達成することが不可能なら -1 と出力せよ。 達成できる場合はかかるコストの和の最小値を出力せよ。


入力例 1

4 5
2 4 5
4 1 2
2 1 3
3 3 9
1 2 4

出力例 1

11

操作 1,2,5 で「石を置く」を選ぶことで、目標を達成することができます。

このときのコストの和は 5+2+4=11 となります。目標を達成するためにコストを 11 未満にすることはできないため、答えは 11 です。


入力例 2

3 3
1 1 1000000000
2 2 100000000
3 3 10000000

出力例 2

1000000000

操作 1 では必ず石を置くという条件に従う必要があります。


入力例 3

2 1
1 2 10

出力例 3

-1

Score : 500 points

Problem Statement

There is a grid with N rows and N columns. The cell at the i-th row from the top and j-th column from the left is called cell (i,j). You perform the following in order of i=1,2,\ldots,M:

  • Operation i : Choose either to place one stone on cell (x_i, y_i) or not. If you place a stone, a cost of c_i is incurred; otherwise, no cost is incurred.

However, you must place a stone in operation 1.

Determine whether the following goal can be achieved, and if it can, find the minimum sum of costs incurred.

  • Goal : For every i \ (1 \leq i \leq N), the number of stones placed in the i-th row is equal to the number of stones placed in the i-th column.

Constraints

  • 1 \le N, M \le 2 \times 10^5
  • 1 \le x_i, y_i \le N
  • 1 \le c_i \le 10^9
  • (x_i, y_i) are distinct.
  • All input values are integers.

Input

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

N M
x_1 y_1 c_1
x_2 y_2 c_2
\vdots
x_M y_M c_M

Output

If it is impossible to achieve the goal, output -1. If it is possible, output the minimum sum of costs incurred.


Sample Input 1

4 5
2 4 5
4 1 2
2 1 3
3 3 9
1 2 4

Sample Output 1

11

The goal can be achieved by choosing to place a stone in operations 1,2,5.

The sum of costs in this case is 5+2+4=11. The total cost cannot be less than 11 to achieve the goal, so the answer is 11.


Sample Input 2

3 3
1 1 1000000000
2 2 100000000
3 3 10000000

Sample Output 2

1000000000

You must place a stone in operation 1.


Sample Input 3

2 1
1 2 10

Sample Output 3

-1
C - ABS Ball

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

配点 : 600

問題文

N 個の白いボールがあります。はじめに各ボールを赤か青どちらかの色で塗ります。

これら N 個の赤か青で塗られたボールを、M 個の区別できる箱のいずれかに入れます。

i 番目の箱に入っている赤、青のボールの数をそれぞれ a_i, b_i とします。

全てのボールの入れ方に対する \prod_{1\leq i \le M}|a_i-b_i| の総和を 998244353 で割ったあまりを求めて下さい。

ただし、2 つのボールの入れ方が異なるとは、ある i について a_i,b_i の少なくとも一方が異なることを言います。

特に、ボール同士は区別しないことに注意してください。

制約

  • 1 \le N,M \le 10^7
  • 入力される値は全て整数

入力

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

N M

出力

答えを出力せよ。


入力例 1

2 1

出力例 1

4

1 にボールを入れる方法は 3 通りあります。
赤と青のボールを 1 個ずつ入れる場合、 |a_1-b_1|=0 です。
赤と青どちらかを 2 個入れる場合、|a_1-b_1|=2 です。
よって、求めるべき答えは 0 + 2 + 2 = 4 となります。


入力例 2

5 7

出力例 2

0

入力例 3

10000000 5000000

出力例 3

965172629

Score : 600 points

Problem Statement

There are N white balls. First, you paint each ball red or blue.

Then, you place these N red or blue painted balls in one of M distinguishable boxes.

Let a_i and b_i be the number of red and blue balls in the i-th box, respectively.

Find the sum, modulo 998244353, of \prod_{1\leq i \le M}|a_i-b_i| over all ways to place the balls.

Here, two ways of placing balls are different if and only if at least one of a_i and b_i is different for some i.

Particularly, balls are not distinguished from each other.

Constraints

  • 1 \le N,M \le 10^7
  • All input values are integers.

Input

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

N M

Output

Output the answer.


Sample Input 1

2 1

Sample Output 1

4

There are three ways to place balls in box 1.
If you place one red ball and one blue ball, |a_1-b_1|=0.
If you place two red balls or two blue balls, |a_1-b_1|=2.
Therefore, the answer is 0 + 2 + 2 = 4.


Sample Input 2

5 7

Sample Output 2

0

Sample Input 3

10000000 5000000

Sample Output 3

965172629
D - Two Rooms

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

配点 : 600

問題文

1,2,\ldots,N の番号が付けられた N 人の人が居ます。i\neq j に対して、人 i と人 j の親密度は A_{i,j} です。

N 人それぞれに対して、部屋 X, 部屋 Y のどちらか一方を割り当てます。N 人はそれぞれ割り当てられた部屋に移動します。誰も居ない部屋が存在しても構いません。

i良い状態 であるとは、以下を満たすことと定義します。

(人 i と同じ部屋に居る人全員の、人 i との親密度の和) \geq (人 i と異なる部屋に居る人全員の、人 i との親密度の和)

N 人全員が良い状態になるような部屋の割り当てを 1 つ出力してください。

なお、そのような割り当ては必ず存在することが証明できます。

制約

  • 2 \leq N \leq 50
  • -50 \leq A_{i,j} \leq 50
  • A_{i,j} = A_{j,i}
  • A_{i,i}=0
  • 入力される値は全て整数

入力

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

N
A_{1,1} A_{1,2} \cdots A_{1,N}
A_{2,1} A_{2,2} \cdots A_{2,N}
\vdots
A_{N,1} A_{N,2} \cdots A_{N,N}

出力

条件を満たす割り当てを 1 つ出力してください。答えが複数存在する場合はどれを出力しても構いません。 割り当ては長さ N の文字列 S で表し、人 i を部屋 X に割り当てる場合は Si 文字目を X, 部屋 Y に割り当てる場合は Si 文字目を Y にしてください。


入力例 1

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

出力例 1

XXYY

1,2 を部屋 X、人 3,4 を部屋 Y に割り当てるとします。

1 は、同じ部屋に居る人(人 2)との親密度の和は 4、異なる部屋に居る人(人 3 と人 4)との親密度の和は (-2)+(-1)=-3 であるため、良い状態です。

他の人も全て良い状態であるため、この割り当ては条件を満たします。

他に YYXX を出力した場合も正解となります。


入力例 2

3
0 0 0
0 0 0
0 0 0

出力例 2

XXX

誰も居ない部屋が存在しても構いません。

Score : 600 points

Problem Statement

There are N people numbered 1,2,\ldots,N. For i\neq j, the intimacy between persons i and j is A_{i,j}.

For each of the N people, assign room X or room Y. Each of the N people moves to their assigned room. It is acceptable if there is an empty room.

Person i is in a good state if the following is satisfied:

(Sum of intimacy with person i of all people in the same room as person i) \geq (Sum of intimacy with person i of all people in a different room from person i).

Output one assignment of rooms such that all N people are in a good state.

It can be proved that such an assignment always exists.

Constraints

  • 2 \leq N \leq 50
  • -50 \leq A_{i,j} \leq 50
  • A_{i,j} = A_{j,i}
  • A_{i,i}=0
  • All input values are integers.

Input

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

N
A_{1,1} A_{1,2} \cdots A_{1,N}
A_{2,1} A_{2,2} \cdots A_{2,N}
\vdots
A_{N,1} A_{N,2} \cdots A_{N,N}

Output

Output one assignment that satisfies the condition. If there are multiple solutions, you may output any of them. Represent the assignment by a string S of length N. If person i is assigned to room X, the i-th character of S should be X; if assigned to room Y, the i-th character of S should be Y.


Sample Input 1

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

Sample Output 1

XXYY

Suppose persons 1,2 are assigned to room X and persons 3,4 are assigned to room Y.

For person 1, the sum of intimacy with people in the same room (person 2) is 4, and the sum of intimacy with people in different rooms (persons 3 and 4) is (-2)+(-1)=-3, so they are in a good state.

All other people are also in a good state, so this assignment satisfies the condition.

Outputting YYXX is also correct.


Sample Input 2

3
0 0 0
0 0 0
0 0 0

Sample Output 2

XXX

It is acceptable if there is an empty room.

E - Drop Min

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

配点 : 700

問題文

(1,2,\ldots,N) の順列 P と、空の配列 A があります。 P に対して以下の操作を N-1 回行います。

  • 隣接する 2 要素を選ぶ。選んだうち小さい方の要素を P から削除し、削除した要素の前後を結合する。削除した要素を A の末尾に追加する。

最終的にできる長さ N-1 の数列 A としてあり得るものの数を 998244353 で割ったあまりを求めてください。

制約

  • 2 \le N \le 2\times 10^5
  • 1 \le P_i \le N
  • P(1,2,\ldots,N) の順列
  • 入力される値は全て整数

入力

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

N
P_1 P_2 \ldots P_N

出力

答えを出力せよ。


入力例 1

4
1 2 3 4

出力例 1

6

最終的な A としては (1,2,3) の順列 6 通りの全てがあり得ます。

例えば、A=(2,1,3) とする操作手順は以下の通りです。

  • 2,3 を選ぶ。2 が削除され、P=(1,3,4), A=(2) となる。
  • 1,3 を選ぶ。1 が削除され、P=(3,4), A=(2,1) となる。
  • 3,4 を選ぶ。3 が削除され、P=(4), A=(2,1,3) となる。

入力例 2

3
3 1 2

出力例 2

1

入力例 3

24
10 24 3 4 8 14 5 2 22 9 21 1 15 6 13 23 18 12 7 17 19 16 20 11

出力例 3

303178128

Score : 700 points

Problem Statement

There is a permutation P of (1,2,\ldots,N) and an empty array A. Perform the following operation N-1 times on P:

  • Choose two adjacent elements. Remove the smaller of the chosen elements from P and concatenate the parts before and after the removed element. Append the removed element to the end of A.

Find the number, modulo 998244353, of possible final sequences A of length N-1.

Constraints

  • 2 \le N \le 2\times 10^5
  • 1 \le P_i \le N
  • P is a permutation of (1,2,\ldots,N).
  • All input values are integers.

Input

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

N
P_1 P_2 \ldots P_N

Output

Output the answer.


Sample Input 1

4
1 2 3 4

Sample Output 1

6

All six permutations of (1,2,3) are possible as the final A.

For example, A=(2,1,3) can be obtained as follows:

  • Choose 2,3. Remove 2, and now P=(1,3,4), A=(2).
  • Choose 1,3. Remove 1, and now P=(3,4), A=(2,1).
  • Choose 3,4. Remove 3, and now P=(4), A=(2,1,3).

Sample Input 2

3
3 1 2

Sample Output 2

1

Sample Input 3

24
10 24 3 4 8 14 5 2 22 9 21 1 15 6 13 23 18 12 7 17 19 16 20 11

Sample Output 3

303178128
F - Add Integer

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

配点 : 700

問題文

整数 N,M,X が与えられます。

以下の一連の操作を行い、長さ N の非負整数列 A を作ります。

  • 長さ 2 の整数列 A=(A_1,A_2) を自由に決める。
  • その後、A に対して以下の操作を N-2 回行う。
    • k=|A| とする。x=A_{k-1}, y=A_k とする。A の末尾に x+y または y-x を追加する。

また、整数列 A が良い数列であるとは、以下を満たすことを言います。

  • 0 \le A_i \le M \ (1 \le i \le N)
  • A_N=X

操作によって得られる全ての良い数列に対する A_1 \times A_2 の総和を 998244353 で割ったあまりを求めてください。

制約

  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq X \leq M \leq 2 \times 10^5
  • 入力される値は全て整数

入力

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

N M X

出力

答えを出力せよ。


入力例 1

3 4 3

出力例 1

8

(0,3,3),(1,4,3),(2,1,3) などが考えられます。

あり得る数列全ての A_1 \times A_2 の和は 8 となります。


入力例 2

150000 160000 140000

出力例 2

791841701

Score : 700 points

Problem Statement

You are given integers N,M,X.

Perform the following series of operations to create a sequence A of length N consisting of non-negative integers.

  • Freely decide an integer sequence A=(A_1,A_2) of length 2.
  • Then, perform the following operation N-2 times on A.
    • Let k=|A|. Let x=A_{k-1}, y=A_k. Append either x+y or y-x to the end of A.

A sequence A is a good sequence if and only if it satisfies the following:

  • 0 \le A_i \le M \ (1 \le i \le N)
  • A_N=X

Find the sum, modulo 998244353, of A_1 \times A_2 over all good sequences that can be obtained by the operations.

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 1 \leq X \leq M \leq 2 \times 10^5
  • All input values are integers.

Input

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

N M X

Output

Output the answer.


Sample Input 1

3 4 3

Sample Output 1

8

Some possible sequences are (0,3,3),(1,4,3),(2,1,3).

The sum of A_1 \times A_2 over all possible sequences is 8.


Sample Input 2

150000 160000 140000

Sample Output 2

791841701