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 n positive integers a1,a2,…,an.
She needs to do the following two-step operation until all elements of a are equal:
- Select two indexes i, j satisfying 1≤i,j≤n and i=j.
- Replace ai with gcd(ai,aj).
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.
水母讨厌数学题,但她必须完成她的数学作业:
水母得到了一个由 n 个正整数组成的数组 a1,a2,…,an。
她需要重复执行以下两步操作,直到数组 a 中所有元素都相等为止:
- 选择两个下标 i、j,满足 1≤i,j≤n 且 i=j;
- 将 ai 替换为 gcd(ai,aj)。
现在,水母请你求出达成目标所需的最少操作次数。
可以证明:水母总能达成目标。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤5000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤5000) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤5000) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 5000.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤5000)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤5000)—— 数组的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤5000)—— 数组的元素。
保证所有测试用例中 n 的总和不超过 5000。
输出格式
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:
- Choose i=3 and j=2 and replace a3 with gcd(a3,a2)=gcd(30,20)=10, then a becomes [12,20,10].
- Choose i=1 and j=3 and replace a1 with gcd(a1,a3)=gcd(12,10)=2, then a becomes [2,20,10].
- Choose i=2 and j=1 and replace a2 with gcd(a2,a1)=gcd(20,2)=2, then a becomes [2,2,10].
- Choose i=3 and j=1 and replace a3 with gcd(a3,a1)=gcd(10,2)=2, then a becomes [2,2,2].
在第一个测试用例中,以下是一种使操作次数最少的方法:
- 选择 i=3 和 j=2,将 a3 替换为 gcd(a3,a2)=gcd(30,20)=10,此时 a 变为 [12,20,10]。
- 选择 i=1 和 j=3,将 a1 替换为 gcd(a1,a3)=gcd(12,10)=2,此时 a 变为 [2,20,10]。
- 选择 i=2 和 j=1,将 a2 替换为 gcd(a2,a1)=gcd(20,2)=2,此时 a 变为 [2,2,10]。
- 选择 i=3 和 j=1,将 a3 替换为 gcd(a3,a1)=gcd(10,2)=2,此时 a 变为 [2,2,2]。
输入解题思路,AI测评打分。不知道怎么写?