#P8809. Approximate GCD Subarrays
Approximate GCD Subarrays
Approximate GCD Subarrays
Given an array \(A = (a_1, a_2, \cdots, a_n)\) of length \(n\), a subarray is defined as a contiguous segment of \(A\) with one or more elements. The greatest common divisor (GCD) of an array is the GCD of all its elements.
We call an integer \(g\) an approximate GCD of a subarray if at least one of the following holds:
- The GCD of the subarray is exactly \(g\).
- By modifying at most one element in the subarray (i.e. replacing it with any integer of your choice), the GCD of the resulting subarray becomes \(g\). Equivalently, there exists an index in the subarray such that if that element were replaced (for example, by \(g\)), the GCD of the subarray of the remaining elements combined with the replacement equals \(g\). Note that for the modification to work, it is necessary that the GCD of the subarray excluding that element is divisible by \(g\) (i.e. \(g\) divides the GCD of the other elements).
Your task is to count the number of subarrays of \(A\) with length at least 2 that have \(g\) as an approximate GCD.
Note: When \(g = 1\), the second condition is always satisfied for any subarray (of length at least 2) since \(1\) divides every integer.
inputFormat
The first line contains two integers (n) and (g) ((2 \le n \le 100,\ 1 \le g \le 10^9)). The second line contains (n) positive integers representing the array (A) (each element is at most (10^9)).
outputFormat
Output a single integer: the number of subarrays with length at least 2 for which (g) is an approximate GCD.
sample
3 2
2 4 6
3