思路

观察数据\(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的更多相关文章

随机推荐

  1. C++实现双向链表的相关操作代码

    #include<iostream>#include<cstdlib>using namespace std;#define OK 1#define ERROR 0#defin ...

  2. jquery 事件方法大全

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <style type ...

  3. Redis缓存问题排查

    一.缓存穿透 概念:缓存穿透是指查询的数据不存在,redis和mysql(或其他持久存储的数据库)都不能命中.工作中出于容错的考虑,如果从数据库内不能查到数据则不会写入缓存,缓存穿透将导致不存在的数据 ...

  4. nodejs express 服务代理

    //方法1 var proxy = require('express-http-proxy'); app.use('/map', proxy('https://test.baidu.com/', { ...

  5. HDOJ--1010题C++

    有两点需要注意,第一点就是,题意是指在第T秒时门才打开,并不是越早到门口就能越早出去,而是只要存在指定长的路径即可以出去. 第二点,每次深搜发现答案不符都必须将点重新初始化.#include<i ...

  6. ZSTUOJ刷题⑨:Problem H.--尖兵

    Problem H: 尖兵 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 4691  Solved: 2112 Description 星光人已经出发了 ...

  7. 31 ModelForm常用功能--扩充

    ModelForm自定义字段的显示效果 class ChargeModelForm(BootStrapModelForm, forms.ModelForm): # 静态变量 # charge_type ...

  8. vi/vim 命令

    vim 文件路径 编辑一个文件,英文模式, 按i:输入模式 按Esc:命令模式 输入模式 dd 删除一行 gg 跳到开头 shift+g 跳到结尾 U 撤销 shift+U 恢复撤销 命令模式 :wq ...

  9. 【闫式dp分析法】

  10. Ignore warnings

    import warnings warnings.filterwarnings("ignore")