#GL0000384. Mảng và ước chung lớn nhất (Array and GCD)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Mảng và ước chung lớn nhất (Array and GCD)
Array and GCD
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of integers, each at least , and initially have zero coins. You may repeatedly perform either operation on the remaining array:
- Increase one element by , paying one coin (only if you have enough coins).
- Decrease one element by , gaining one coin.
An array is ideal if every element is at least and every pair of elements at distinct positions has GCD . For arrays of fewer than two elements, the pairwise condition holds automatically.
An array is beautiful if it can be transformed into an ideal array using these operations, starting from zero coins and never spending coins you do not have.
You may remove any number of elements beforehand, including all of them. Find the minimum number of removals making the remaining array beautiful.
PHOJ input convention: Each .in file contains one test case with no leading .
Input
The first line contains , and the second line contains .
Output
Print the minimum number of elements to remove.
Subtasks
-
Subtask 1 (20 points): , .
-
Subtask 2 (30 points): , .
-
Subtask 3 (50 points): , .
Examples
Example 1
Input
3
5 5 5
Output
0
Explanation
Reduce the first to , then another to , obtaining . All pairs are coprime, and decreasing costs no coins. Thus no removal is needed.
Example 2
Input
4
2 3 2 4
Output
2
Explanation
Keeping yields an ideal array, so two removals suffice. Keeping any three elements gives a total of at most , while the smallest possible sum of three pairwise coprime integers at least is . Starting with zero coins, operations cannot increase the total sum, so three elements cannot be kept.