#K83787. Make Array Elements Equal

    ID: 36275 Type: Default 1000ms 256MiB

Make Array Elements Equal

Make Array Elements Equal

You are given an array of n integers and an index k (0-indexed). In one operation, you can add or subtract an integer multiple of some base value to every element, such that eventually all array elements become equal. It turns out that the number of different ways to achieve this goal is equal to the greatest common divisor (gcd) of the differences between each element and the element at index k.

More formally, let the array be \(a = [a_0, a_1, \dots, a_{n-1}]\) and let \(b = a_k\). Compute the list of differences \(d_i = a_i - b\) for all \(0 \le i < n\). If all differences are zero then there are infinitely many ways (output inf), and if n is 1 return -1. Otherwise, the answer is \(|\gcd(d_0, d_1, \dots, d_{n-1})|\).

The calculations involving the gcd can be expressed in \(\LaTeX\) as follows:

\[ \text{result} = \begin{cases} -1, & \text{if } n = 1, \\ \infty, & \text{if } d_i = 0 \text{ for all } i, \\ \left|\gcd(d_0, d_1, \dots, d_{n-1})\right|, & \text{otherwise.} \end{cases} \]

inputFormat

The input is read from standard input and consists of three lines:

  • The first line contains a single integer \(n\) denoting the length of the array.
  • The second line contains \(n\) space-separated integers representing the array elements.
  • The third line contains a single integer \(k\), the index (0-indexed) used as the reference.

outputFormat

Output a single line to standard output containing the number of different ways to make all the array elements equal. If the array is already equal (i.e. all differences are 0), output inf. If \(n = 1\), output -1.

## sample
3
1 5 3
1
2