CF2115A.Gellyfish and Flaming Peony

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Gellyfish hates math problems, but she has to finish her math homework:

Gellyfish is given an array of nn positive integers a1,a2,…,ana_1, a_2, \ldots, a_n.

She needs to do the following two-step operation until all elements of aa are equal:

  1. Select two indexes ii, jj satisfying 1≤i,j≤n1 \leq i, j \leq n and i≠ji \neq j.
  2. Replace aia_i with gcd⁡(ai,aj)\gcd(a_i, a_j).

Now, Gellyfish asks you for the minimum number of operations to achieve her goal.

It can be proven that Gellyfish can always achieve her goal.

水母讨厌数学题,但她必须完成她的数学作业:

水母得到了一个由 nn 个正整数组成的数组 a1,a2,…,ana_1, a_2, \ldots, a_n。

她需要重复执行以下两步操作,直到数组 aa 中所有元素都相等为止:

  1. 选择两个下标 ii、jj,满足 1≤i,j≤n1 \leq i, j \leq n 且 i≠ji \neq j;
  2. 将 aia_i 替换为 gcd⁡(ai,aj)\gcd(a_i, a_j)。

现在,水母请你求出达成目标所需的最少操作次数。

可以证明:水母总能达成目标。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50001 \le t \le 5000). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤50001 \leq n \leq 5000) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤50001 \leq a_i \leq 5000) — the elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤50001 \le t \le 5000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤50001 \leq n \leq 5000)—— 数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤50001 \leq a_i \leq 5000)—— 数组的元素。

保证所有测试用例中 nn 的总和不超过 50005000。

输出格式

For each test case, output a single integer — the minimum number of operations to achieve her goal.

对于每个测试用例,输出一个整数——达成目标所需的最少操作次数。

输入输出样例

  • 输入#1

    3
    3
    12 20 30
    6
    1 9 1 9 8 1
    3
    6 14 15

    输出#1

    4
    3
    3

说明/提示

In the first test case, the following is a way that minimizes the number of operations:

  1. Choose i=3i = 3 and j=2j=2 and replace a3a_3 with gcd⁡(a3,a2)=gcd⁡(30,20)=10\gcd(a_3,a_2) = \gcd(30, 20) = 10, then aa becomes [12,20,10][12, 20, 10].
  2. Choose i=1i=1 and j=3j=3 and replace a1a_1 with gcd⁡(a1,a3)=gcd⁡(12,10)=2\gcd(a_1,a_3) = \gcd(12, 10) = 2, then aa becomes [2,20,10][2, 20, 10].
  3. Choose i=2i=2 and j=1j=1 and replace a2a_2 with gcd⁡(a2,a1)=gcd⁡(20,2)=2\gcd(a_2,a_1) = \gcd(20, 2) = 2, then aa becomes [2,2,10][2, 2, 10].
  4. Choose i=3i=3 and j=1j=1 and replace a3a_3 with gcd⁡(a3,a1)=gcd⁡(10,2)=2\gcd(a_3,a_1) = \gcd(10, 2) = 2, then aa becomes [2,2,2][2, 2, 2].

在第一个测试用例中,以下是一种使操作次数最少的方法:

  1. 选择 i=3i = 3 和 j=2j=2,将 a3a_3 替换为 gcd⁡(a3,a2)=gcd⁡(30,20)=10\gcd(a_3,a_2) = \gcd(30, 20) = 10,此时 aa 变为 [12,20,10][12, 20, 10]。
  2. 选择 i=1i=1 和 j=3j=3,将 a1a_1 替换为 gcd⁡(a1,a3)=gcd⁡(12,10)=2\gcd(a_1,a_3) = \gcd(12, 10) = 2,此时 aa 变为 [2,20,10][2, 20, 10]。
  3. 选择 i=2i=2 和 j=1j=1,将 a2a_2 替换为 gcd⁡(a2,a1)=gcd⁡(20,2)=2\gcd(a_2,a_1) = \gcd(20, 2) = 2,此时 aa 变为 [2,2,10][2, 2, 10]。
  4. 选择 i=3i=3 和 j=1j=1,将 a3a_3 替换为 gcd⁡(a3,a1)=gcd⁡(10,2)=2\gcd(a_3,a_1) = \gcd(10, 2) = 2,此时 aa 变为 [2,2,2][2, 2, 2]。

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

首页