#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 nn, kk, and a sequence {an}\{a_n\}, you also have a temporary variable xx. You may perform the following operation any number of times (possibly 00 times). One operation works as follows:

  1. Choose an interval [l,r][l, r]. For all i[l,r]i \in [l, r], set xgcd(al,al+1,,ar)x \leftarrow \gcd(a_l, a_{l+1}, \cdots, a_r).
  2. For all i[l,r]i \in [l, r], set aixa_i \leftarrow x.

In short, each time you can choose an interval and change every number in it into the gcd\gcd of that interval.

The cost of one operation is rl+1+kr - l + 1 + k. Now you want to make every number in the sequence equal. Find the minimum total cost.


If you do not understand the meaning of gcd\gcd or multi-argument gcd\gcd, you may refer to the following definition:

  • gcd(a1,a2,,ak)\gcd(a_1, a_2, \dots, a_k) means the greatest common divisor of a1,a2,,aka_1, a_2, \dots, a_k, i.e., the largest positive integer that can divide a1,a2,,aka_1, a_2, \dots, a_k at the same time.

Input Format

The first line contains two non-negative integers n,kn, k.

The second line contains nn numbers, representing {an}\{a_n\}.

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 [1,10][1, 10].

Sample 4.

See old/old4.in and old/old4.out in the attached files.

This sample satisfies the constraints of test points 9129 \sim 12.

Sample 5.

See old/old5.in and old/old5.out in the attached files.

This sample satisfies the constraints of test points 131613 \sim 16.

Constraints and Hints.

For all data, it is guaranteed that 1n4×1061 \leq n \leq 4 \times 10^6, 0k1090 \leq k \leq 10^9, and 1ai1091 \leq a_i \leq 10^9.

The specific limits for each test point are shown in the table below:

Test Point ID nn \leq k,aik, a_i \leq Special Property
11 10610^6 10910^9 All numbers are equal
242 \sim 4 44 None
585 \sim 8 100100
9129 \sim 12 10001000
131613 \sim 16 10610^6
172017 \sim 20 4×1064 \times 10^6

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