luogu#P8155. 「PMOI-5」公约数变换

「PMOI-5」公约数变换

Problem Description

You are given a constant mm and a sequence aa of length nn.

Define one "common divisor transformation" as follows:
First set biai+gcd(m,a1,...,ai)b_i \leftarrow a_i + \gcd(m, a_1, ..., a_i), and finally set aibia_i \leftarrow b_i. (Note that you must compute all bib_i first and then assign them to aia_i one by one, rather than computing and assigning at the same time.)

Find the minimum number of "common divisor transformations" needed so that i[1,n]\forall i \in [1,n], maim \mid a_i.

It can be proven that a solution always exists.

Input Format

The first line contains two integers nn and mm, representing the length of the sequence and the given constant.
The next line contains nn integers, where the ii-th integer is aia_i.

Output Format

Output one integer in a single line, representing the answer.

5 5
5 4 3 2 1
4
10 6154
1 3 6 4 2 5 7 8 1 2

263

Hint

[Sample Explanation 1]

After the first "common divisor transformation", the sequence becomes 10,5,4,3,210, 5, 4, 3, 2.
After the second "common divisor transformation", the sequence becomes 15,10,5,4,315, 10, 5, 4, 3.
After the third "common divisor transformation", the sequence becomes 20,15,10,5,420, 15, 10, 5, 4.
After the fourth "common divisor transformation", the sequence becomes 25,20,15,10,525, 20, 15, 10, 5.
So the answer is 44.

[Constraints]
This problem uses bundled testdata.

  • Subtask 1 (5 pts): m=1m = 1.
  • Subtask 2 (10 pts): m=2m = 2.
  • Subtask 3 (10 pts): n=1n = 1 and m500m \le 500.
  • Subtask 4 (15 pts): n=1n = 1.
  • Subtask 5 (10 pts): n5000n \le 5000.
  • Subtask 6 (20 pts): n5×104n \le 5 \times 10^4.
  • Subtask 7 (10 pts): m,ai109m, a_i \le 10^9.
  • Subtask 8 (20 pts): No special constraints.

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1m10141 \le m \le 10^{14}, 1ai10181 \le a_i \le 10^{18}. It is guaranteed that the answer fits in long long.

Translated by ChatGPT 5