#GL0000356. GCD và cây khung nhỏ nhất (GCD and MST)
Bài tập thuộc lộ trình
Mục lục đề ⌄
GCD và cây khung nhỏ nhất (GCD and MST)
GCD and MST
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integers and , construct a weighted undirected graph on vertices . For every , add an edge of weight if
Additionally, add an edge of weight between each pair of consecutive vertices . Parallel edges are allowed. Find the minimum spanning tree weight.
PHOJ format: Each input file contains one case, without the original Codeforces leading test count .
Input
- First line: .
- Second line: integers .
Output
A single integer: the MST total weight, potentially exceeding 32-bit limits.
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- Subtask 3 (50 points): .
Examples
Example 1
Input
2 5
10 10
Output
5
Explanation
The two vertices have a range-edge of weight 10 and an adjacent edge of weight 5; choose weight 5.
Example 2
Input
2 5
3 3
Output
3
Explanation
Since gcd(3,3)=min(3,3)=3, the edge of weight 3 is cheaper than weight 5.
Example 3
Input
4 5
5 2 4 9
Output
12
Explanation
Edges (1,2), (2,3), (3,4) may have weights 5,2,5, totaling the optimal 12.