出处:

ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest
Arab Academy for Science, Technology and Maritime Transport, 2017

想法题:n=1e5. 有n*n/2条边,不能kruskal。

但是考虑一下,边权都是gcd,而gcd(x,y)<min(x,y),所以权值不同的数只有1e5个。所以依然用kruskal的想法,枚举所有不同权值的边,然后暴力枚举gcd为该边的两个数,将他们连起来。具体做法就是枚举该边的所有倍数

#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include<cstdio>
#include<vector>
#include<ctime> #define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 1e6+;
const double PI = acos(-1.0);
int a[maxn], vis[maxn], f[maxn];
int find(int x) { if (f[x] == x) { return x; }return f[x] = find(f[x]); } int main()
{
freopen("dream.in", "r", stdin);
int t;
cin >> t;
int n;
int x;
rep(k, , t) { mmm(vis, );
cin >> n;
ll ans = ;
int tot = ;
int mx = ;
rep(i, , n)
{
scanf("%d", &x);
if (vis[x]) { ans += x; continue; }
vis[x] = ;
f[x] = x;
a[++tot] = x;
mx = max(mx, x);
}
int p = ;
for (int i = mx; i&&p < tot - ; i--) {
int x = , y;
for (int k = , tmp; k*i <= mx && p < tot - ; k++) {
if (!vis[tmp = k * i])continue;
y = find(tmp);
if (!x)x = y; else if (y != x)f[y] = x, ans += i, ++p;
}
}
printf("Case %d: ", k);
cout << ans << endl;
}
//cin >> t;
}
/*
1
3 4 2
1 2 3 1
2 1 1 4
7 8 9 3
1 1 1 1
1 2 3 4
*/
/**/

gcd最大生成树模板的更多相关文章

  1. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

  2. gcd和lcm模板

    long long gcd(long long b,long long c)//计算最大公约数{ return c==0?b:gcd(c,b%c);} long long lcm(long long ...

  3. 最小生成树&&最大生成树模板

    #include<bits/stdc++.h> using namespace std; int n,m; struct edge { int x; int y; int len; }ed ...

  4. Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)

    链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS   Memory Limit: 131072K To ...

  5. gcd 与 扩gcd 总结

    gcd 定理的证明: 模板: ll gcd(ll a,ll b) { ) return a; else return gcd(b,a%b); } 扩gcd证明: 模板: ll extgcd(ll a, ...

  6. 25个增强iOS应用程序性能的提示和技巧(初级篇)

    25个增强iOS应用程序性能的提示和技巧(初级篇) 标签: ios内存管理性能优化 2013-12-13 10:53 916人阅读 评论(0) 收藏 举报  分类: IPhone开发高级系列(34)  ...

  7. 25 个增强iOS应用程序性能的提示和技巧 应用程序性能的提示和技巧

    初级 在开发过程中,下面这些初级技巧需要时刻注意: 1.使用ARC进行内存管理2.在适当的情况下使用reuseIdentifier3.尽可能将View设置为不透明(Opaque)4.避免臃肿的XIBs ...

  8. 增强iOS应用程序性能的提示和技巧(25个)

    转自 http://www.cocoachina.com/newbie/basic/2013/0522/6259.html 在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的 ...

  9. (转)25个增强iOS应用程序性能的提示和技巧--初级篇

    在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的,如果你的程序运行迟钝或缓慢,会招致用户的差评.然而由于iOS设备的局限性,有时候要想获得良好的性能,是很困难的.在开发过程 ...

随机推荐

  1. Android报“android.content.res.Resources$NotFoundException: String resource ID #0x2”错误

    Android报“android.content.res.Resources$NotFoundException: String resource ID #0x2”错误 当调用setText()方法时 ...

  2. 通过配置JVM参数解决生成日志存在乱码问题

    项目上生产环境时出现一个问题,生成的日志文件存在乱码,服务器编码为 LANG=zh_US.UTF-8,按理说中文不会存在乱码,这里存在一个问题,就是在SIT等其他环境我们使用的连接工具为Xshell, ...

  3. 实战UITableview深度优化

    演示项目下载地址:https://github.com/YYProgrammer/YYTableViewDemo 项目里的低性能版是常规写法实现的tableview,高性能版是做了相关优化后的tabl ...

  4. 关于APP,原生和H5开发技术的争论 APP开发技术选型判断依据

    关于APP,原生和H5开发技术的争论 App的开发技术,目前流行的两种方式,原生和Html5.原生分了安卓平台和ios平台(还有小众的黑莓.死去的塞班就不说了),H5就是Html5. 目前争论不休的问 ...

  5. class<T>和 class<?>类型

    ? 表示不确定的java类型. T 表示java类型 Class<T>在实例化的时候,T要替换成具体类Class<?>它是个通配泛型,?可以代表任何类型<? extend ...

  6. docker 命令集

    1.提交本地镜像到远程cd to dockerfile directorysudo docker build -t orange5 ./sudo docker psdocker tag 1adec2c ...

  7. 深入了解View(一)—— measure測量流程分析

    欢迎使用Markdown编辑器写博客 本Markdown编辑器使用StackEdit改动而来,用它写博客.将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接 ...

  8. phpunit assert断言分类整理

    布尔类型 方法名 含义 参数 返回值 assertTrue 断言为真     assertFalse 断言为假     NULL类型 方法名 含义 参数 返回值 assertNull 断言为NULL ...

  9. Caffe 分类问题 Check failed: error == cudaSuccess (2 vs. 0) out of memory

    如果图片过大,需要适当缩小batch_size的值,否则使用GPU时可能超出其缓存大小而报错

  10. python3命令行ImportError: No module named 'xxxx'的问题

    主要原因:启动脚本不在当前目录下,无法找到上一层 在pycharm写好的脚本程序,在命令行无法运行,报错 Traceback (most recent call last): File "t ...