A14438 | Array Stabilization (GCD version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ .
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$ .
输入格式
The first line contains an integer $t$ ( $1 \le t \le 10^4$ ). Then $t$ test cases follow.
Each test case contains two lines. The first line contains an integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ) — length of the sequence $a$ . The second line contains $n$ integers $a_0, a_1, \dots, a_{n - 1}$ ( $1 \le a_i \le 10^6$ ).
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$ .
Each test case contains two lines. The first line contains an integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ) — length of the sequence $a$ . The second line contains $n$ integers $a_0, a_1, \dots, a_{n - 1}$ ( $1 \le a_i \le 10^6$ ).
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$ .
输出格式
Print $t$ numbers — answers for each test case.
输入输出样例
输入 #1
5 4 16 24 10 5 4 42 42 42 42 3 4 6 4 5 1 2 3 4 5 6 9 9 27 9 9 63
输出 #1
3 0 2 1 1
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted