#P8940. [DTOI 2023] C. 不见故人
[DTOI 2023] C. 不见故人
Background
Although luanmenglei is already a grown-up high school student, every time someone mentions luanmenglei's girlfriend from 8th grade, luanmenglei will sink into those wonderful memories and cannot pull himself out.
Problem Description
Given , , and a sequence , you also have a temporary variable . You may perform the following operation any number of times (possibly times). One operation works as follows:
- Choose an interval . For all , set .
- For all , set .
In short, each time you can choose an interval and change every number in it into the of that interval.
The cost of one operation is . Now you want to make every number in the sequence equal. Find the minimum total cost.
If you do not understand the meaning of or multi-argument , you may refer to the following definition:
- means the greatest common divisor of , i.e., the largest positive integer that can divide at the same time.
Input Format
The first line contains two non-negative integers .
The second line contains numbers, representing .
Output Format
Output one number in one line, representing the answer.
10 3
2 2 2 1 2 2 2 1 2 2
13
10 0
2 2 2 1 2 2 2 1 2 3
9
11 0
2 2 2 1 2 2 2 1 1 3 3
10
Hint
Sample 1 Explanation.
Perform one operation and choose the interval .
Sample 4.
See old/old4.in and old/old4.out in the attached files.
This sample satisfies the constraints of test points .
Sample 5.
See old/old5.in and old/old5.out in the attached files.
This sample satisfies the constraints of test points .
Constraints and Hints.
For all data, it is guaranteed that , , and .
The specific limits for each test point are shown in the table below:
| Test Point ID | Special Property | ||
|---|---|---|---|
| All numbers are equal | |||
| None | |||
The input size of this problem is large. Please choose a fast input method. A suggested strategy is provided below:
Please add this line at the beginning of your code: std::ios::sync_with_stdio(false);std::cin.tie(0);.
Note that after adding this line, the efficiency of cin/cout will be greatly improved, ensuring that all data can be read within 250 ms. However, after using it you can only use cin/cout streams to read input.
Translated by ChatGPT 5