CF1732A Bestie
思路
观察数据\(n \le 20\)
直接暴力。
我们直接算所有数的\(GCD\),然后枚举\(1\)~\(n\)的每一个数要不要选,然后选的话,就把原来的\(GCD\)和当前枚举的数\(GCD\)一下,最后求最小值就好了。
代码
#include <iostream>
using namespace std;
const int N = 30;
int n;
int a[N];
int ans,cost;
int gcd (int a,int b) {
if (!b) return a;
return gcd (b,a % b);
}
void dfs (int u,int g,int cost) {
if (g == 1) {
ans = min (ans,cost);
return ;
}
if (u > n) return ;
dfs (u + 1,gcd (g,u),cost + n - u + 1);
dfs (u + 1,g,cost);
}
int main () {
int T;
cin >> T;
while (T--) {
cin >> n;
int g;
for (int i = 1;i <= n;i++) {
cin >> a[i];
if (i == 1) g = a[i];
else g = gcd (g,a[i]);
}
if (n == 1) {
if (a[1] == 1) puts ("0");
else puts ("1");
continue;
}
if (g == 1) {
puts ("0");
continue;
}
ans = 2e9;
dfs (1,g,0);
cout << ans << endl;
}
return 0;
}
CF1732A Bestie的更多相关文章
随机推荐
- NIO 缓冲区 ByteBuffer 基本认识
一.差别 java.nio.HeapByteBuffer 0. 获取方式:ByteBuffer.allocate(int value); 1. java堆内存,读写效率较低,但分配内存较块. 2. 受 ...
- UE4.27 新版本像素流送插件配置
UE4.27 像素流送新版插件部署命令 以下内容参考自UE5官方文档:https://docs.unrealengine.com/5.0/en-US/unreal-engine-pixel-strea ...
- 关于npm和yarn的坑
遇到下载出错的情况,这两个两个双管齐下
- Houdini_Python笔记
目录 Gemetry 用Stash节点预览模型 判断文件是否存在 PDG Gemetry 用Stash节点预览模型 import hou stashParm = stashNode.parm(&quo ...
- linux修改网络
如何修改ip 临时方法: ifconfig DIVICE IP netmask NETMASK 知识临时修改ip,重启或重启网络恢复 在一个网卡上设置多个ip ifconfig DEVICE:NUMB ...
- 新手:git回滚代码,合并代码解决冲突
回滚到某个tag: git reset --hard 70438034dc git push -f 合并---解决冲突---提交: Step 1. Fetch and check out the br ...
- Vue中的样式穿透,修改element-ui组件样式不生效
在Vue项目中用的比较多的就是组件,为了实现组件的样式模块化.我们通常会在style标签中添加一个scoped属性,这样css样式只能作用于当前的Vue组件.使组件之间的样式相互独立,当调用该组件的时 ...
- 使用@Param注解时注意org.springframework.data.repository.query.Param与org.apache.ibatis.annotations.Param的区别
注解@Param有以下两种: 1.Spring org.springframework.data.repository.query.Param 2.mybatis org.apache.ibatis. ...
- 基于redis设计的秒杀活动
FlashSale 意为 秒杀,是电子网上商城促销活动的一种形式 本项目依赖redis,使用redis的缓存以及原子操作实现秒杀活动 依赖的包 StackExchange.Redis 该包的作用类似 ...
- binlog2sql 实战心得
原创:binlog2sql在GitHub的地址:https://github.com/danfengcao/binlog2sql 作者:danfengcao 功能:从MySQL binlog解析出你要 ...