実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
それぞれ N 個、M 個の正整数からなる 2 つの数列 A=(A_1,A_2, \ldots ,A_N) と B=(B_1, \ldots ,B_M) が与えられます。
それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差の最小値、すなわち、 \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert を求めてください。
制約
- 1 \leq N,M \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
出力
答えを出力せよ。
入力例 1
2 2 1 6 4 9
出力例 1
2
それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差としてあり得るのは、 \lvert 1-4\rvert=3 、 \lvert 1-9\rvert=8 、 \lvert 6-4\rvert=2 、 \lvert 6-9\rvert=3 の 4 つです。 この中で最小である 2 を出力します。
入力例 2
1 1 10 10
出力例 2
0
入力例 3
6 8 82 76 82 82 71 70 17 39 67 2 45 35 22 24
出力例 3
3
Score : 300 points
Problem Statement
You are given two sequences: A=(A_1,A_2, \ldots ,A_N) consisting of N positive integers, and B=(B_1, \ldots ,B_M) consisting of M positive integers.
Find the minimum difference of an element of A and an element of B, that is, \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert.
Constraints
- 1 \leq N,M \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
Output
Print the answer.
Sample Input 1
2 2 1 6 4 9
Sample Output 1
2
Here is the difference for each of the four pair of an element of A and an element of B: \lvert 1-4\rvert=3, \lvert 1-9\rvert=8, \lvert 6-4\rvert=2, and \lvert 6-9\rvert=3. We should print the minimum of these values, or 2.
Sample Input 2
1 1 10 10
Sample Output 2
0
Sample Input 3
6 8 82 76 82 82 71 70 17 39 67 2 45 35 22 24
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。
注記
ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_N と B_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で A が B より早いと定義されます。
- ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。
ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。
- 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。
制約
- 1 \le N \le M \le 10
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。
入力例 1
2 3
出力例 1
1 2 1 3 2 3
条件を満たす数列は (1,2),(1,3),(2,3) の 3 個です。これらを辞書順で早い方から出力します。
入力例 2
3 5
出力例 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
Score : 300 points
Problem Statement
Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.
Notes
For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:
- there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.
An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:
- A_i < A_{i+1} for all integers i (1 \le i \le N-1).
Constraints
- 1 \le N \le M \le 10
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).
Sample Input 1
2 3
Sample Output 1
1 2 1 3 2 3
The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.
Sample Input 2
3 5
Sample Output 2
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N_1+N_2 頂点 M 辺の無向グラフがあります。i=1,2,\ldots,M に対し、i 番目の辺は頂点 a_i と頂点 b_i を結びます。
また、以下を満たすことが保障されます。
- 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
- N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
- 頂点 1 と頂点 N_1+N_2 は非連結
次の操作をちょうど 1 回行います。
- 1 \leq u \leq N_1 を満たす整数 u と N_1+1 \leq v \leq N_1+N_2 を満たす整数 v を選び、頂点 u と頂点 v を結ぶ辺を追加する
操作後のグラフにおいて、頂点 1 と頂点 N_1+N_2 は必ず連結であることが示せます。そこで、頂点 1 と頂点 N_1+N_2 を結ぶ経路の長さ(辺の本数)の最小値を d とします。
操作で追加する辺を適切に選んだ時にありえる d の最大値を求めてください。
連結とは?
無向グラフの頂点 u,v が連結であるとは、頂点 u と頂点 v を結ぶ経路が存在することをいいます。制約
- 1 \leq N_1,N_2 \leq 1.5 \times 10^5
- 0 \leq M \leq 3 \times 10^5
- 1 \leq a_i \leq b_i \leq N_1+N_2
- i \neq j ならば (a_i,b_i) \neq (a_j,b_j)
- 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
- N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
- 頂点 1 と頂点 N_1+N_2 は非連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N_1 N_2 M a_1 b_1 \vdots a_M b_M
出力
答えを出力せよ。
入力例 1
3 4 6 1 2 2 3 4 5 4 6 1 3 6 7
出力例 1
5
u=2,v=5 として操作することで d=5 と出来ます。これが最大値です。

入力例 2
7 5 20 10 11 4 5 10 12 1 2 1 5 5 6 2 4 3 5 9 10 2 5 1 4 11 12 9 12 8 9 5 7 3 7 3 6 3 4 8 12 9 11
出力例 2
4
Score : 400 points
Problem Statement
We have an undirected graph with (N_1+N_2) vertices and M edges. For i=1,2,\ldots,M, the i-th edge connects vertex a_i and vertex b_i.
The following properties are guaranteed:
- Vertex u and vertex v are connected, for all integers u and v with 1 \leq u,v \leq N_1.
- Vertex u and vertex v are connected, for all integers u and v with N_1+1 \leq u,v \leq N_1+N_2.
- Vertex 1 and vertex (N_1+N_2) are disconnected.
Consider performing the following operation exactly once:
- choose an integer u with 1 \leq u \leq N_1 and an integer v with N_1+1 \leq v \leq N_1+N_2, and add an edge connecting vertex u and vertex v.
We can show that vertex 1 and vertex (N_1+N_2) are always connected in the resulting graph; so let d be the minimum length (number of edges) of a path between vertex 1 and vertex (N_1+N_2).
Find the maximum possible d resulting from adding an appropriate edge to add.
Definition of "connected"
Two vertices u and v of an undirected graph are said to be connected if and only if there is a path between vertex u and vertex v.Constraints
- 1 \leq N_1,N_2 \leq 1.5 \times 10^5
- 0 \leq M \leq 3 \times 10^5
- 1 \leq a_i \leq b_i \leq N_1+N_2
- (a_i,b_i) \neq (a_j,b_j) if i \neq j.
- Vertex u and vertex v are connected for all integers u and v such that 1 \leq u,v \leq N_1.
- Vertex u and vertex v are connected for all integers u and v such that N_1+1 \leq u,v \leq N_1+N_2.
- Vertex 1 and vertex (N_1+N_2) are disconnected.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N_1 N_2 M a_1 b_1 \vdots a_M b_M
Output
Print the answer.
Sample Input 1
3 4 6 1 2 2 3 4 5 4 6 1 3 6 7
Sample Output 1
5
If we set u=2 and v=5, the operation yields d=5, which is the maximum possible.

Sample Input 2
7 5 20 10 11 4 5 10 12 1 2 1 5 5 6 2 4 3 5 9 10 2 5 1 4 11 12 9 12 8 9 5 7 3 7 3 6 3 4 8 12 9 11
Sample Output 2
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
N 頂点からなる重み付き無向グラフ G があります。 G の各頂点には 1 から N までの番号が付けられています。 最初、G には辺が 1 本も存在しません。
今から、M 回の操作を行うことによって G に辺を追加していきます。 i\ (1\leq i\leq M) 回目の操作は以下の通りです。
- K_i 頂点からなる頂点の部分集合 S_i=\lbrace A_{i,1},A_{i,2},\dots,A_{i,K_i}\rbrace が与えられる。 u,v\in S_i かつ u<v を満たす全ての u,v について、頂点 u と頂点 v の間に重み C_i の辺を追加する。
M 回の操作を全て行ったとき G が連結になるか判定し、連結になるならば G の最小全域木に含まれる辺の重みの総和を求めてください。
制約
- 2\leq N \leq 2\times 10^5
- 1\leq M \leq 2\times 10^5
- 2\leq K_i \leq N
- \displaystyle \sum_{i=1}^{M} K_i \leq 4\times 10^5
- 1\leq A_{i,1} < A_{i,2} < \dots < A_{i,K_i}\leq N
- 1\leq C_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
K_1 C_1
A_{1,1} A_{1,2} \dots A_{1,K_1}
K_2 C_2
A_{2,1} A_{2,2} \dots A_{2,K_2}
\vdots
K_M C_M
A_{M,1} A_{M,2} \dots A_{M,K_M}
出力
M 回の操作を全て行ったとき G が連結にならないならば -1 を、連結になるならば G の最小全域木に含まれる辺の重みの総和を出力せよ。
入力例 1
4 3 3 3 1 2 3 2 2 1 2 3 4 1 3 4
出力例 1
9

左の図は M 回の操作を全て行ったあとの G を、右の図はその最小全域木の 1 つを表しています(辺の横に書かれた数はその辺の重みを示します)。
最小全域木に含まれる辺の重みの総和は 3+2+4=9 です。
入力例 2
3 2 2 1 1 2 2 1 1 2
出力例 2
-1
M 回の操作を全て行っても G は連結になりません。
入力例 3
10 5 6 158260522 1 3 6 8 9 10 10 877914575 1 2 3 4 5 6 7 8 9 10 4 602436426 2 6 7 9 6 24979445 2 3 4 5 8 10 4 861648772 2 4 8 9
出力例 3
1202115217
Score: 450 points
Problem Statement
You are given a weighted undirected graph G with N vertices, numbered 1 to N. Initially, G has no edges.
You will perform M operations to add edges to G. The i-th operation (1 \leq i \leq M) is as follows:
- You are given a subset of vertices S_i=\lbrace A_{i,1},A_{i,2},\dots,A_{i,K_i}\rbrace consisting of K_i vertices. For every pair u, v such that u, v \in S_i and u < v, add an edge between vertices u and v with weight C_i.
After performing all M operations, determine whether G is connected. If it is, find the total weight of the edges in a minimum spanning tree of G.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 2 \leq K_i \leq N
- \sum_{i=1}^{M} K_i \leq 4 \times 10^5
- 1 \leq A_{i,1} < A_{i,2} < \dots < A_{i,K_i} \leq N
- 1 \leq C_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
K_1 C_1
A_{1,1} A_{1,2} \dots A_{1,K_1}
K_2 C_2
A_{2,1} A_{2,2} \dots A_{2,K_2}
\vdots
K_M C_M
A_{M,1} A_{M,2} \dots A_{M,K_M}
Output
If G is not connected after all M operations, print -1. If G is connected, print the total weight of the edges in a minimum spanning tree of G.
Sample Input 1
4 3 3 3 1 2 3 2 2 1 2 3 4 1 3 4
Sample Output 1
9

The left diagram shows G after all M operations, and the right diagram shows a minimum spanning tree of G (the numbers next to the edges indicate their weights).
The total weight of the edges in the minimum spanning tree is 3 + 2 + 4 = 9.
Sample Input 2
3 2 2 1 1 2 2 1 1 2
Sample Output 2
-1
G is not connected even after all M operations.
Sample Input 3
10 5 6 158260522 1 3 6 8 9 10 10 877914575 1 2 3 4 5 6 7 8 9 10 4 602436426 2 6 7 9 6 24979445 2 3 4 5 8 10 4 861648772 2 4 8 9
Sample Output 3
1202115217
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
N 個の整数 A_1,A_2,\dots,A_N が与えられます。
以下の条件を満たす整数の組 (i,j,k) の個数を求めてください。
- 1 \le i,j,k \le N
- A_i \times A_j = A_k
制約
- 1 \le N \le 1000
- \color{red}{1 \le A_i < 10^{1000}}
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \vdots A_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 6 12 24
出力例 1
6
問題文中の条件を満たす (i,j,k) の組は以下の 6 通りです。
- (1,2,3)
- (1,3,4)
- (1,4,5)
- (2,1,3)
- (3,1,4)
- (4,1,5)
入力例 2
11 1 2 3 4 5 6 123456789123456789 123456789123456789 987654321987654321 987654321987654321 121932631356500531347203169112635269
出力例 2
40
各整数 A_i の値が非常に大きくなりうることに注意してください。
入力例 3
9 4 4 4 2 2 2 1 1 1
出力例 3
162
A_i の値に重複がありうることに注意してください。
Score: 550 points
Problem Statement
You are given N integers A_1, A_2, \dots, A_N.
Find the number of triples of integers (i, j, k) that satisfy the following conditions:
- 1 \le i, j, k \le N
- A_i \times A_j = A_k
Constraints
- 1 \le N \le 1000
- \color{red}{1 \le A_i < 10^{1000}}
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \vdots A_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 6 12 24
Sample Output 1
6
The following six triples (i, j, k) satisfy the conditions in the problem statement:
- (1, 2, 3)
- (1, 3, 4)
- (1, 4, 5)
- (2, 1, 3)
- (3, 1, 4)
- (4, 1, 5)
Sample Input 2
11 1 2 3 4 5 6 123456789123456789 123456789123456789 987654321987654321 987654321987654321 121932631356500531347203169112635269
Sample Output 2
40
Note that the values of each integer A_i can be huge.
Sample Input 3
9 4 4 4 2 2 2 1 1 1
Sample Output 3
162
Note that there may be duplicates among the values of A_i.