#GL0000342. GCD lớn nhất sau các phép chuyển đơn vị (Max GCD)
Bài tập thuộc lộ trình
Mục lục đề ⌄
GCD lớn nhất sau các phép chuyển đơn vị (Max GCD)
Max GCD
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given positive integers . You may perform at most operations:
- Choose two distinct positions .
- Increase by and decrease by .
Values are allowed to become zero or negative. Each operation preserves the total sum.
Find the largest positive integer for which all entries can be made divisible by using at most operations.
Input
The first line contains ; the second contains integers .
Output
Print the maximum achievable positive divisor .
Subtasks
- Subtask 1 (20 points): , , .
- Subtask 2 (30 points): , , .
- Subtask 3 (50 points): , , .
Examples
Example 1
Input
2 3
8 20
Output
7
Explanation
Transfer one unit from the first entry to the second: . Both entries are divisible by 7, and no greater common divisor is reachable with at most three operations.
Example 2
Input
2 10
3 5
Output
8
Explanation
Transfer three units from the first entry to the second, yielding . Any common divisor must divide the unchanged total 8, so 8 is optimal.