出处:

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. Protobuf3 序列化

    在message_lite.h中定义了SerializeToString ,SerializeToArray ,SerializeToCodedStream ,SerializeToZeroCopyS ...

  2. js判断移动端是否安装某软软件,安装直接打开相应的链接,否则跳转到下载商店方法

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  3. iOS UITextView 设置 NSLinkAttributeName 属性,点击链接跳转

    @interface ViewController ()<UITextViewDelegate> - (void)viewDidLoad{    [super viewDidLoad];  ...

  4. shell脚本自动登录服务器

    #!/bin/sh function trapper(){ trap 'exit 1' EXIT QUIT; } serverArr=( guard-boot-001,10.1.17.12 guard ...

  5. JTable动态刷新数据

    http://www.cnblogs.com/fnlingnzb-learner/p/6025408.html 注意下面几个方法的应用场景,不限于JTable,其他swing组件一样 ———————— ...

  6. 在Linux下使用gcc编译mesa文件报undefined reference to symbol 'sin@@GLIBC_2.2.5和DSO missing from command line两个错误的解决方案

    一.概述 在Linux系统下使用gcc编译用C语言写的mesa的示例程序. 环境:Ubuntu Server 18.04.1 二.问题的出现 在Ubuntu下安装好mesa所需的库文件,将目标文件从g ...

  7. ScheduledThreadPoolExecutor实现原理

    ScheduledThreadPoolExecutor实现原理 博客分类: concurrency java   自jdk1.5开始,Java开始提供ScheduledThreadPoolExecut ...

  8. JavaScript数组删除指定元素

    ^_^ function arrayRemoveItem(arr, delVal) { if (arr instanceof Array) { var index = arr.indexOf(delV ...

  9. ABAP 文件选择框

    GUI_FILE_SAVE_DIALOG    CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE         EXPORTING ...

  10. Swagger UI 与SpringMVC的整合

    关于 Swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. S ...