A - Sum of Two Integers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

合計が N となるように相異なる 2 つの正整数を選ぶ方法は何通りあるでしょうか (順序は考慮しません)。

制約

  • 1 ≦ N ≦ 10^6
  • N は整数である。

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

1

合計が 4 となるように相異なる 2 つの正整数を選ぶ方法は、13 を選ぶ 1 通りのみです。(31 を選ぶことはこれと区別しません。)


入力例 2

999999

出力例 2

499999

Score : 100 points

Problem Statement

How many ways are there to choose two distinct positive integers totaling N, disregarding the order?

Constraints

  • 1 \leq N \leq 10^6
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

1

There is only one way to choose two distinct integers totaling 4: to choose 1 and 3. (Choosing 3 and 1 is not considered different from this.)


Sample Input 2

999999

Sample Output 2

499999
B - Counting of Trees

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 要素からなる整数列 D_1,...,D_N が与えられます。頂点に 1 から N の番号が付けられた N 頂点からなる木であって、 以下の条件をみたすものの個数を 998244353 で割ったあまりを求めてください。

  • 1 以上 N 以下の任意の整数 i に対して、頂点 1 と頂点 i の距離が D_i である。

注記

  • N 頂点の木とは N 頂点 N-1 辺からなる連結無向グラフのことであり、2 頂点の距離とは一方から他方への最短路に用いられる辺の個数を指します。
  • 2 つの木が異なるとは、ある 2 頂点 x, y が存在して、xy の間に一方の木では辺が存在し、 もう一方の木では辺が存在しないことを指します。

制約

  • 1 ≦ N ≦ 10^5
  • 0 ≦ D_i ≦ N-1

入力

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

N
D_1 D_2 ... D_N

出力

答えを出力せよ。


入力例 1

4
0 1 1 2

出力例 1

2

例えば、(1,2), (1,3), (2,4) の間に辺があるような木が条件をみたします。


入力例 2

4
1 1 1 1

出力例 2

0

入力例 3

7
0 3 2 1 2 2 1

出力例 3

24

Score : 300 points

Problem Statement

Given is an integer sequence D_1,...,D_N of N elements. Find the number, modulo 998244353, of trees with N vertices numbered 1 to N that satisfy the following condition:

  • For every integer i from 1 to N, the distance between Vertex 1 and Vertex i is D_i.

Notes

  • A tree of N vertices is a connected undirected graph with N vertices and N-1 edges, and the distance between two vertices are the number of edges in the shortest path between them.
  • Two trees are considered different if and only if there are two vertices x and y such that there is an edge between x and y in one of those trees and not in the other.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq D_i \leq N-1

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the answer.


Sample Input 1

4
0 1 1 2

Sample Output 1

2

For example, a tree with edges (1,2), (1,3), and (2,4) satisfies the condition.


Sample Input 2

4
1 1 1 1

Sample Output 2

0

Sample Input 3

7
0 3 2 1 2 2 1

Sample Output 3

24
C - Swaps

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 要素からなる 2 つの整数列 A_1,...,A_N 及び B_1,...,B_N が与えられます。 以下の操作を N-2 回まで (0 回でもよい) 行うことで、1 以上 N 以下のすべての整数 i に対して A_i \leqq B_i となるようにできるかを判定してください。

  • 1 以上 N 以下の相異なる整数 x, y を選び、A_x の値と A_y の値を入れ替える。

制約

  • 2 ≦ N ≦ 10^5
  • 1 ≦ A_i,B_i ≦ 10^9

入力

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

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

出力

可能な場合は Yes を、不可能な場合は No を出力せよ。


入力例 1

3
1 3 2
1 2 3

出力例 1

Yes

A_2 の値と A_3 の値を入れ替えればよいです。


入力例 2

3
1 2 3
2 2 2

出力例 2

No

入力例 3

6
3 1 2 6 3 4
2 2 8 3 4 3

出力例 3

Yes

Score : 600 points

Problem Statement

Given are two integer sequences of N elements each: A_1,...,A_N and B_1,...,B_N. Determine if it is possible to do the following operation at most N-2 times (possibly zero) so that, for every integer i from 1 to N, A_i \leq B_i holds:

  • Choose two distinct integers x and y between 1 and N (inclusive), and swap the values of A_x and A_y.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If the objective is achievable, print Yes; if it is not, print No.


Sample Input 1

3
1 3 2
1 2 3

Sample Output 1

Yes

We should swap the values of A_2 and A_3.


Sample Input 2

3
1 2 3
2 2 2

Sample Output 2

No

Sample Input 3

6
3 1 2 6 3 4
2 2 8 3 4 3

Sample Output 3

Yes
D - Shortest Path on a Line

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

一直線上に N 個の点があり、順に 1 から N までの番号がついています。

高橋君はこれらの点を頂点として無向グラフを作ることにしました。 はじめはグラフに辺はないですが、M 回の操作によって辺を追加します。 i 回目の操作では次のように辺を追加します。

  • 1 以上 N 以下の整数 L_i, R_i 及び正整数 C_i を用いる。 L_i ≦ s < t ≦ R_i なる整数の組 (s,t) すべてに対し、頂点 s と頂点 t の間に長さ C_i の辺を追加する。

ただし、L_1,...,L_M, R_1,...,R_M, C_1,...,C_M はすべて入力で与えられます。

高橋君は最終的に得られたグラフ上で最短路問題を解きたいです。得られたグラフ上での頂点 1 から頂点 N までの最短路の長さを求めてください。

制約

  • 2 ≦ N ≦ 10^5
  • 1 ≦ M ≦ 10^5
  • 1 ≦ L_i < R_i ≦ N
  • 1 ≦ C_i ≦ 10^9

入力

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

N M
L_1 R_1 C_1
:
L_M R_M C_M

出力

頂点 1 から頂点 N までの最短路の長さを出力せよ。 ただし、最短路が存在しない場合は -1 を出力せよ。


入力例 1

4 3
1 3 2
2 4 3
1 4 6

出力例 1

5

頂点 1 と頂点 2 の間に長さ 2 の辺があり、頂点 2 と頂点 4 の間に長さ 3 の辺があるので、頂点 1 と頂点 4 の間に長さ 5 のパスが存在します。


入力例 2

4 2
1 2 1
3 4 2

出力例 2

-1

入力例 3

10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3

出力例 3

28

Score : 600 points

Problem Statement

We have N points numbered 1 to N arranged in a line in this order.

Takahashi decides to make an undirected graph, using these points as the vertices. In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph. The i-th operation is as follows:

  • The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.

The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as input.

Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq L_i < R_i \leq N
  • 1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 C_1
:
L_M R_M C_M

Output

Print the length of the shortest path from Vertex 1 to Vertex N in the final graph. If there is no shortest path, print -1 instead.


Sample Input 1

4 3
1 3 2
2 4 3
1 4 6

Sample Output 1

5

We have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of length 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between Vertex 1 and Vertex 4.


Sample Input 2

4 2
1 2 1
3 4 2

Sample Output 2

-1

Sample Input 3

10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3

Sample Output 3

28
E - Non-triangular Triplets

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

正の整数 N 及び K が与えられます。

以下の条件をみたすように、3N 個の整数 K,K+1,...,K+3N-13 整数からなる N 個の組 (a_1,b_1,c_1),...,(a_N,b_N,c_N) に分割することが可能かを判定して下さい。K,K+1,...,K+3N-1 のうちどの整数も、N 個の組のうちちょうど 1 個に現れなければなりません。

  • 1 以上 N 以下のすべての整数 i に対して a_i + b_i \leqq c_i が成り立つ。

また、可能な場合はそのような分割を 1 つ構成してください。

制約

  • 1 ≦ N ≦ 10^5
  • 1 ≦ K ≦ 10^9

入力

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

N K

出力

条件をみたすように N 個の三つ組に分割することができない場合 -1 を出力せよ。可能な場合は N 個の三つ組を以下の形式で出力せよ。

a_1 b_1 c_1
:
a_N b_N c_N

入力例 1

1 1

出力例 1

1 2 3

入力例 2

3 3

出力例 2

-1

Score : 700 points

Problem Statement

Given are positive integers N and K.

Determine if the 3N integers K, K+1, ..., K+3N-1 can be partitioned into N triples (a_1,b_1,c_1), ..., (a_N,b_N,c_N) so that the condition below is satisfied. Any of the integers K, K+1, ..., K+3N-1 must appear in exactly one of those triples.

  • For every integer i from 1 to N, a_i + b_i \leq c_i holds.

If the answer is yes, construct one such partition.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq 10^9

Input

Input is given from Standard Input in the following format:

N K

Output

If it is impossible to partition the integers satisfying the condition, print -1. If it is possible, print N triples in the following format:

a_1 b_1 c_1
:
a_N b_N c_N

Sample Input 1

1 1

Sample Output 1

1 2 3

Sample Input 2

3 3

Sample Output 2

-1
F - Mirror Frame

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1400

問題文

二次元平面上に正方形の形をした枠があり、その 4 頂点の座標は (0,0), (N,0), (0,N), (N,N) です。 この枠は鏡でできており、枠の辺(頂点を除く)に光が当たると入射角と反射角が等しくなるように光が反射します。 ただし、頂点に光が当たると、光は入射してきた向きと逆向きに反射します。

ここで、枠の内部にある格子点 (i,j) (0<i,j<N) に対応するラインを次のように定義します。

  • (i,j) から (i-1,j-1), (i-1,j+1), (i+1,j-1), (i+1,j+1) への 4 方向に光を発したときに、 光が通る軌跡を (i,j) のラインとする。

図: 格子点に対応するラインの例

枠の内部にある各格子点上には電球が 1 つずつあります。それぞれの電球に対してonとoffのいずれかの状態を定めたとき、 以下の操作を繰り返して全ての電球をoffにできるならば、その電球の状態をきれいと呼ぶことにします。

  • 枠の内部にある格子点を 1 つ選び、そのライン上にある全ての電球のonとoffを切り替える。

高橋君はいくつかの電球のonとoffの状態を決めましたが、いくつかの電球の状態はまだ決めていません。 それらのonとoffを定める方法であって、電球の状態がきれいになるような方法の数を 998244353 で割ったあまりを求めてください。 格子点 (i,j) にある電球は A_{i,j}=o のときon、A_{i,j}=x のときoffであり、A_{i,j}=? のとき状態がまだ決まっていません。

制約

  • 2 ≦ N ≦ 1500
  • A_{i,j}o, x, ? のいずれか

入力

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

N
A_{1,1}...A_{1,N-1}
:
A_{N-1,1}...A_{N-1,N-1}

出力

答えを出力せよ。


入力例 1

4
o?o
???
?x?

出力例 1

1

以下のように各電球の状態を決めれば、電球の状態はきれいとなります。

oxo
xox
oxo

例えば (1,1) のライン上にある電球のonとoffを切り替えると、 (1,1), (1,3), (2,2), (3,1), (3,3) にある電球のonとoffが切り替わるので、 すべての電球がoffになります。


入力例 2

5
o?o?
????
o?x?
????

出力例 2

0

入力例 3

6
?o???
????o
??x??
o????
???o?

出力例 3

32

入力例 4

9
????o??x
?????x??
??o?o???
?o?x????
???????x
x?o?o???
????????
x?????x?

出力例 4

4

Score : 1400 points

Problem Statement

In a two-dimensional plane, there is a square frame whose vertices are at coordinates (0,0), (N,0), (0,N), and (N,N). The frame is made of mirror glass. A ray of light striking an edge of the frame (but not a vertex) will be reflected so that the angle of incidence is equal to the angle of reflection. A ray of light striking a vertex of the frame will be reflected in the direction opposite to the direction it is coming from.

We will define the path for a grid point (a point with integer coordinates) (i,j) (0<i,j<N) strictly within the frame, as follows:

  • The path for (i,j) is the union of the trajectories of four rays of light emitted from (i,j) to (i-1,j-1), (i-1,j+1), (i+1,j-1), and (i+1,j+1).

Figure: an example of a path for a grid point

There is a light bulb at each grid point strictly within the frame. We will assign a state - ON or OFF - to each bulb. The state of the whole set of bulbs are called beautiful if it is possible to turn OFF all the bulbs by repeating the following operation:

  • Choose a grid point strictly within the frame, and switch the states of all the bulbs on its path.

Takahashi has set the states of some of the bulbs, but not for the remaining bulbs. Find the number of ways to set the states of the remaining bulbs so that the state of the whole set of bulbs is beautiful, modulo 998244353. The state of the bulb at the grid point (i,j) is set to be ON if A_{i,j}=o, OFF if A_{i,j}=x, and unset if A_{i,j}=?.

Constraints

  • 2 \leq N \leq 1500
  • A_{ij} is o, x, or ?.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}...A_{1,N-1}
:
A_{N-1,1}...A_{N-1,N-1}

Output

Print the answer.


Sample Input 1

4
o?o
???
?x?

Sample Output 1

1

The state of the whole set of bulbs will be beautiful if we set the state of each bulb as follows:

oxo
xox
oxo

We can turn OFF all the bulbs by, for example, choosing the point (1, 1) and switching the states of the bulbs at (1,1), (1,3), (2,2), (3,1), and (3,3).


Sample Input 2

5
o?o?
????
o?x?
????

Sample Output 2

0

Sample Input 3

6
?o???
????o
??x??
o????
???o?

Sample Output 3

32

Sample Input 4

9
????o??x
?????x??
??o?o???
?o?x????
???????x
x?o?o???
????????
x?????x?

Sample Output 4

4