当前位置: 首页 > web >正文

Codeforces Round 1028 (Div. 2) C. Gellyfish and Flaming Peony

Codeforces Round 1028 (Div. 2) C. Gellyfish and Flaming Peony

题目

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

Gellyfish is given an array of n n n positive integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an.

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

  1. Select two indexes i i i, j j j satisfying 1 ≤ i , j ≤ n 1 \leq i, j \leq n 1i,jn and i ≠ j i \neq j i=j.
  2. Replace a i a_i ai with gcd ⁡ ( a i , a j ) \gcd(a_i, a_j) 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.

Input

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

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

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,,an ( 1 ≤ a i ≤ 5000 1 \leq a_i \leq 5000 1ai5000) — the elements of the array.

It is guaranteed that the sum of n n n over all test cases does not exceed 5000 5000 5000.

Output

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

题目解析及思路

题目要求不断执行操作,使数组所有元素相等,输出最少的操作数

操作:选择两个下标i,j,将a[i]替换为gcd(a[i],a[j])

样例输入

3
12 20 30

样例输出

4

在这里插入图片描述

代码

/*
不难想到,最终相同的数一定是所有元素的gcd,因此先求出这个最终数,如果他本身就存在,那么其他所有数变到最终数只需要一次操作,如果不存在,再考虑将某个元素变成这个最终数的最少次数
*/
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;void solve(){int n;cin>>n;vector<int> a(n);//哈希表记录次数map<int,int> mp;int g = 0;for(int &i:a){cin>>i;mp[i] ++;g = gcd(g,i);}//如果g本身存在,输出所有不为g的数的数量if(mp[g]){cout<<n-mp[g]<<endl;return;}int N = 5001;//暴力枚举所有可能数(因为只有5000就暴力了,如果更多要用dp)vector<int> op(N,1e9);for(int x:a){//x存在,操作数为0op[x] = 0;//枚举所有可能数与x求gcdfor(int i=1;i<N;i++){int gg = gcd(i,x);op[gg] = min(op[gg],op[i] + 1);}}cout<<op[g] + n-1<<endl;
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int t;cin>>t;while(t--){solve();}
}

(0);
cout.tie(0);

int t;
cin>>t;
while(t--){solve();
}

}


http://www.xdnf.cn/news/10479.html

相关文章:

  • java synchronized关键字用法
  • STM32Cubemx-H7-17-麦克纳姆轮驱动
  • 关于神经网络中的梯度和神经网络的反向传播以及梯度与损失的关系
  • 用Python打开不同联类型的文件
  • 【xmb】】内部文档148344599
  • 大数据学习(126)-窗口函数范围
  • 通过WiFi无线连接小米手机摄像头到电脑的方法
  • AI炼丹日志-27 - Anubis 通过 PoW工作量证明的反爬虫组件 上手指南 原理解析
  • Java数值处理常见错误解析
  • java多线程与JUC
  • nt!MiDispatchFault函数分析之nt!MiCompleteProtoPteFault函数的作用
  • sqli-labs靶场32-37关(宽字节注入)
  • 历年苏州大学计算机保研上机真题
  • 语音转文字工具
  • Git 入门学习教程
  • Redis 缓存穿透、缓存击穿、缓存雪崩详解与解决方案
  • Ansible 进阶 - Roles 与 Inventory 的高效组织
  • uni-app学习笔记十八--uni-app static目录简介
  • YOLOv5-入门篇笔记
  • 算法打开13天
  • 焦虑而烦躁的上午
  • HTTPS
  • VeriFree:无需Verifier的通用RL框架
  • 【GPT入门】第40课 vllm与ollama特性对比,与模型部署
  • wsl安装linux
  • 测试总结(二)
  • Python 验证码识别(使用pytesseract库)
  • JVM——JVM运行时数据区的内部机制是怎样的?
  • unix/linux source 命令,在当前的 Shell 会话中读取并执行指定文件中的命令
  • 【AI学习】检索增强生成(Retrieval Augmented Generation,RAG)