CF1547F Array Stabilization (GCD version)
Description
You are given an array of positive integers $ a = [a_0, a_1, \dots, a_{n - 1}] $ ( $ n \ge 2 $ ).
In one step, the array $ a $ is replaced with another array of length $ n $ , in which each element is the [greatest common divisor (GCD)](http://tiny.cc/tuy9uz) of two neighboring elements (the element itself and its right neighbor; consider that the right neighbor of the $ (n - 1) $ -th element is the $ 0 $ -th element).
Formally speaking, a new array $ b = [b_0, b_1, \dots, b_{n - 1}] $ is being built from array $ a = [a_0, a_1, \dots, a_{n - 1}] $ such that $ b_i $ $ = \gcd(a_i, a_{(i + 1) \mod n}) $ , where $ \gcd(x, y) $ is the greatest common divisor of $ x $ and $ y $ , and $ x \mod y $ is the remainder of $ x $ dividing by $ y $ . In one step the array $ b $ is built and then the array $ a $ is replaced with $ b $ (that is, the assignment $ a $ := $ b $ is taking place).
For example, if $ a = [16, 24, 10, 5] $ then $ b = [\gcd(16, 24) $ , $ \gcd(24, 10) $ , $ \gcd(10, 5) $ , $ \gcd(5, 16)] $ $ = [8, 2, 5, 1] $ . Thus, after one step the array $ a = [16, 24, 10, 5] $ will be equal to $ [8, 2, 5, 1] $ .
For a given array $ a $ , find the minimum number of steps after which all values $ a_i $ become equal (that is, $ a_0 = a_1 = \dots = a_{n - 1} $ ). If the original array $ a $ consists of identical elements then consider the number of steps is equal to $ 0 $ .
Input Format
N/A
Output Format
N/A