#P8809. [蓝桥杯 2022 国 C] 近似 GCD

[蓝桥杯 2022 国 C] 近似 GCD

Problem Description

Xiao Lan has an array A=(a1,a2,,an)A = (a_1,a_2,\cdots,a_n) of length nn. A subarray is defined as an array formed by selecting one or more consecutive elements from the original array. The greatest common divisor (GCD) of an array refers to the GCD of all elements in the array. If, after changing at most one element in the array, the GCD of the array becomes gg, then gg is called the approximate GCD of this array. An array may have multiple possible approximate GCD values.

Specifically, to determine whether gg is an approximate GCD of a subarray:

  1. If the GCD of this subarray is exactly gg, then gg is its approximate GCD.
  2. If, after modifying one element in this subarray (it can be changed to any value), the GCD of the subarray becomes gg, then gg is the approximate GCD of this subarray.

Xiao Lan wants to know how many subarrays of AA with length at least 22 have an approximate GCD equal to gg.

Input Format

The first line contains two integers n,gn,g, separated by a space, representing the length of array AA and the value of gg.

The second line contains nn integers a1,a2,,ana_1,a_2,\cdots,a_n, with a space between adjacent integers.

Output Format

Output one line containing one integer, representing how many subarrays of array AA with length at least 22 have an approximate GCD equal to gg.

5 3
1 3 6 4 10
5

Hint

Sample Explanation

There are 55 subarrays that satisfy the condition:

[1,3][1,3]: After changing 11 to 33, the GCD of this subarray becomes 33, so it satisfies the condition.

[1,3,6][1,3,6]: After changing 11 to 33, the GCD of this subarray becomes 33, so it satisfies the condition.

[3,6][3,6]: The GCD of this subarray is 33, so it satisfies the condition.

[3,6,4][3,6,4]: After changing 44 to 33, the GCD of this subarray becomes 33, so it satisfies the condition.

[6,4][6,4]: After changing 44 to 33, the GCD of this subarray becomes 33, so it satisfies the condition.

Constraints

For 20%20\% of the testdata, 2n1002\le n\le100.

For 40%40\% of the testdata, 2n10002\le n\le1000.

For all testdata, 2n1052\le n\le10^5, 1g,ai1091\le g,a_i \le10^9.

Lanqiao Cup 2022 National Contest, Group C, Problem F.

Translated by ChatGPT 5