出处:

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. 生产系统ELK日志采集系统

    总结下,生产在运转的日志采集系统!后续的扩展在于elasticsearch节点与logstash节点与kafka+zookeeper,目的提高吞吐量!

  2. Linux内核调试方法总结

    Linux内核调试方法总结 一  调试前的准备 二  内核中的bug 三  内核调试配置选项 1  内核配置 2  调试原子操作 四  引发bug并打印信息 1  BUG()和BUG_ON() 2   ...

  3. Python中的高级数据结构详解

    这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...

  4. c语言搜索子字符串

    c字符串功能练习: 获取一行字符串,然后,在这行字符串中搜索是否包含一个小字符串 #include <cstdio> #include <cstring> #define N ...

  5. [MySQL TroubleShooting] 服务启动报错

    有个朋友发了一段启动错误的stack,当启动Skip_Grant_Table就不报错: 群里的大神找出来了因为udf_initv这个自定义函数报错. 但是一直想不通为啥服务启动要去运行自定义函数呢? ...

  6. vue.js购物车

    vue.js https://cn.vuejs.org/v2/guide/ 简单购物车 <html> <head> <script src="https://c ...

  7. 【iCore4 双核心板_ARM】例程三十四:U_DISK_IAP_ARM实验——更新升级STM32

    实验现象及操作说明: 1.本例程共有两个代码包,APP和IAP,IAP程序功能实现将APP程序升级至STM32中. 2.直接上电或烧写程序将执行升级的APP应用程序. 3.按下按键上电或写程序将进行升 ...

  8. windows下局域网文件共享,不需要登录账号密码

    基于局域网中,有时候需要传输一个大的文件都需要用到U盘,很麻烦,所以优选文件共享.但是有时候会出现需要登录账户密码,所以需要设置第三步,步骤如下: 1.选中要共享的文件夹,右键有一个共享选项,点击出现 ...

  9. Swagger UI 与SpringMVC的整合 II

    pom.xml <!-- swagger开始 --> <dependency> <groupId>io.springfox</groupId> < ...

  10. datatable to List<T>带缓存

    public class DataHelper { //datarow 转换的类型缓存 private static MemoryCache modelCash = MemoryCache.Defau ...