gcd最大生成树模板
出处:
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最大生成树模板的更多相关文章
- D - GCD HDU - 1695 -模板-莫比乌斯容斥
D - GCD HDU - 1695 思路: 都 除以 k 后转化为 1-b/k 1-d/k中找互质的对数,但是需要去重一下 (x,y) (y,x) 这种情况. 这种情况出现 x ,y ...
- gcd和lcm模板
long long gcd(long long b,long long c)//计算最大公约数{ return c==0?b:gcd(c,b%c);} long long lcm(long long ...
- 最小生成树&&最大生成树模板
#include<bits/stdc++.h> using namespace std; int n,m; struct edge { int x; int y; int len; }ed ...
- Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)
链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS Memory Limit: 131072K To ...
- gcd 与 扩gcd 总结
gcd 定理的证明: 模板: ll gcd(ll a,ll b) { ) return a; else return gcd(b,a%b); } 扩gcd证明: 模板: ll extgcd(ll a, ...
- 25个增强iOS应用程序性能的提示和技巧(初级篇)
25个增强iOS应用程序性能的提示和技巧(初级篇) 标签: ios内存管理性能优化 2013-12-13 10:53 916人阅读 评论(0) 收藏 举报 分类: IPhone开发高级系列(34) ...
- 25 个增强iOS应用程序性能的提示和技巧 应用程序性能的提示和技巧
初级 在开发过程中,下面这些初级技巧需要时刻注意: 1.使用ARC进行内存管理2.在适当的情况下使用reuseIdentifier3.尽可能将View设置为不透明(Opaque)4.避免臃肿的XIBs ...
- 增强iOS应用程序性能的提示和技巧(25个)
转自 http://www.cocoachina.com/newbie/basic/2013/0522/6259.html 在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的 ...
- (转)25个增强iOS应用程序性能的提示和技巧--初级篇
在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的,如果你的程序运行迟钝或缓慢,会招致用户的差评.然而由于iOS设备的局限性,有时候要想获得良好的性能,是很困难的.在开发过程 ...
随机推荐
- 对 IIC 总线的理解、调用函数以及常见面试问题
一.IIC 总线概述: IIC 即Inter-Integrated Circuit(集成电路总线) I2C总线是PHLIPS公司推出的一种串行总线, I2C总线只有两根双向信号线.一根是数据线SDA, ...
- 【转载】VMware虚拟机NAT模式网络配置图文教程
原文:https://blog.csdn.net/dingguanyi/article/details/77829085 一.引言 在Windows上搭建集群实验环境时,为能够让集群结点之间相互通信, ...
- HADOOP security
https://www.microsoft.com/en-us/trustcenter/security/azure-security https://docs.microsoft.com/en-us ...
- 深入分析 ThreadLocal
ThreadLoacal是什么? ThreadLocal是啥?以前面试别人时就喜欢问这个,有些伙伴喜欢把它和线程同步机制混为一谈,事实上ThreadLocal与线程同步无关.ThreadLocal虽然 ...
- Software Engineer Title Ladder
http://changelog.ca/log/2013/08/09/software_engineer_title_ladder Within the software engineering pr ...
- vim简单的移动光标
vim学习简单的光标移动 ==> 移动到行首 $ ==> 移动到行尾 ^ ==> 移动到本行第一个不是blank的位置 g_ ==> 移动到本行最后一个不是blank的位置^Z ...
- C语言 结构体中的零长度数组
/* C语言零长度数组大小和取值问题 */ #include <stdio.h> #include <stdlib.h> #include <string.h> s ...
- 联想项目结束了,聊聊华为SAP HANA项目八卦
联想项目结束了,聊聊华为SAP HANA项目八卦 [转] 本文目录 [隐藏] 1.故事线 2.华为的文化我们不懂 3.分分钟的文化冲突 4. 项目到底要做什么(待更新) 5.项目咋样了(待更新) 1. ...
- SAP Parallel Accounting(平行分类账)业务配置及操作手册
目录 SAP Parallel Accounting(平行分类账业务)配置及操作手册 SAP Parallel Accounting(平行分类账业务)配置及操作手册 Overview 业务说明 为了适 ...
- GoLang之反射
反射 反射(reflect) 所谓反射(reflect)就是能检查程序在运行时的状态. 使用反射的三条定律: 反射可以将“接口类型变量”转换为“反射类型对象”: 反射可以将“反射类型对象”转换为“接口 ...