CF1547F.Array Stabilization (GCD version)

普及+/提高

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array of positive integers a=[a0,a1,…,an−1]a = [a_0, a_1, \dots, a_{n - 1}] (n≥2n \ge 2).

In one step, the array aa is replaced with another array of length nn, in which each element is the greatest common divisor (GCD) of two neighboring elements (the element itself and its right neighbor; consider that the right neighbor of the (n−1)(n - 1)-th element is the 00-th element).

Formally speaking, a new array b=[b0,b1,…,bn−1]b = [b_0, b_1, \dots, b_{n - 1}] is being built from array a=[a0,a1,…,an−1]a = [a_0, a_1, \dots, a_{n - 1}] such that bib_i =gcd⁡(ai,a(i+1)mod  n)= \gcd(a_i, a_{(i + 1) \mod n}), where gcd⁡(x,y)\gcd(x, y) is the greatest common divisor of xx and yy, and xmod  yx \mod y is the remainder of xx dividing by yy. In one step the array bb is built and then the array aa is replaced with bb (that is, the assignment aa := bb is taking place).

For example, if a=[16,24,10,5]a = [16, 24, 10, 5] then b=[gcd⁡(16,24)b = [\gcd(16, 24), gcd⁡(24,10)\gcd(24, 10), gcd⁡(10,5)\gcd(10, 5), gcd⁡(5,16)]\gcd(5, 16)] =[8,2,5,1]= [8, 2, 5, 1]. Thus, after one step the array a=[16,24,10,5]a = [16, 24, 10, 5] will be equal to [8,2,5,1][8, 2, 5, 1].

For a given array aa, find the minimum number of steps after which all values aia_i become equal (that is, a0=a1=⋯=an−1a_0 = a_1 = \dots = a_{n - 1}). If the original array aa consists of identical elements then consider the number of steps is equal to 00.

给你一个正整数数组 a=[a0,a1,…,an−1]a = [a_0, a_1, \dots, a_{n - 1}](其中 n≥2n \ge 2)。

每一步操作中,数组 aa 被替换为另一个长度为 nn 的数组,其中每个元素是其自身与其右侧相邻元素的最大公约数(GCD);特别地,第 (n−1)(n - 1) 个元素的右侧相邻元素定义为第 00 个元素。

形式化地,由数组 a=[a0,a1,…,an−1]a = [a_0, a_1, \dots, a_{n - 1}] 构造新数组 b=[b0,b1,…,bn−1]b = [b_0, b_1, \dots, b_{n - 1}],使得

bi=gcd⁡(ai,a(i+1)mod  n),b_i = \gcd(a_i, a_{(i + 1) \mod n}),

其中 gcd⁡(x,y)\gcd(x, y) 表示 xx 与 yy 的最大公约数,xmod  yx \mod y 表示 xx 除以 yy 的余数。每一步中先构造出数组 bb,然后将数组 aa 替换为 bb(即执行赋值操作 a:=ba := b)。

例如,若 a=[16,24,10,5]a = [16, 24, 10, 5],则

b=[gcd⁡(16,24), gcd⁡(24,10), gcd⁡(10,5), gcd⁡(5,16)]=[8,2,5,1].b = [\gcd(16, 24),\ \gcd(24, 10),\ \gcd(10, 5),\ \gcd(5, 16)] = [8, 2, 5, 1].

因此,经过一步操作后,数组 a=[16,24,10,5]a = [16, 24, 10, 5] 将变为 [8,2,5,1][8, 2, 5, 1]。

对给定的数组 aa,求使所有元素 aia_i 全部相等(即 a0=a1=⋯=an−1a_0 = a_1 = \dots = a_{n - 1})所需的最少操作步数。若原始数组 aa 的所有元素已全相同,则认为所需步数为 00。

输入格式

The first line contains an integer tt (1≤t≤1041 \le t \le 10^4). Then tt test cases follow.

Each test case contains two lines. The first line contains an integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — length of the sequence aa. The second line contains nn integers a0,a1,…,an−1a_0, a_1, \dots, a_{n - 1} (1≤ai≤1061 \le a_i \le 10^6).

It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)。随后是 tt 组测试数据。

每组测试数据包含两行。第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 表示序列 aa 的长度。第二行包含 nn 个整数 a0,a1,…,an−1a_0, a_1, \dots, a_{n - 1}(1≤ai≤1061 \le a_i \le 10^6)。

保证所有测试数据的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

Print tt numbers — answers for each test case.

输出 tt 个数字——每个测试用例的答案。

输入输出样例

  • 输入#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

输入解题思路,AI测评打分。不知道怎么写?

首页