【Codeforces 582A】 GCD Table
【题目链接】
【算法】
G中最大的数一定也是a中最大的数。
G中次大的数一定也是a中次大的数。
第三、第四可能是由最大和次大的gcd产生的
那么就不难想到下面的算法:
1. 令p为G中最大的数。在G中删除p,a中加入p。
2 . 对于a中的所有其他数(设为q),在G中删除2个gcd(p, q)。
3. 若G为空则结束;否则回到(1)。
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 500 int i,N,x,k;
map<int,int> M;
int a[MAXN*MAXN];
vector<int> res; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline int gcd(int x,int y) { return !y ? x : gcd(y,x%y); } int main() { read(N);
for (i = ; i <= N * N; i++) {
read(a[i]);
M[a[i]]++;
}
sort(a+,a+N*N+);
for (i = N * N; i >= ; i--) {
if (!M[a[i]]) continue;
--M[a[i]];
for (k = ; k < res.size(); k++) M[gcd(a[i],res[k])] -= ;
res.push_back(a[i]);
}
for (i = ; i < res.size(); i++) {
write(res[i]);
if (i < res.size() - ) putchar(' ');
}
puts(""); return ;
}
【Codeforces 582A】 GCD Table的更多相关文章
- 【Codeforces 582A】GCD Table
[链接] 我是链接,点我呀:) [题意] 给你一个数组A[]经过a[i][j] = gcd(A[i],A[j])的规则生成的二维数组 让你求出原数组A [题解] 我们假设原数组是A 然后让A数组满足A ...
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
- 【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【84.62%】【codeforces 552A】Vanya and Table
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 664A】 Complicated GCD
[题目链接] 点击打开链接 [算法] gcd(a,a+1) = 1 所以当a = b时,答案为a,否则为1 [代码] #include<bits/stdc++.h> using names ...
- 【codeforces 509A】Maximum in Table
[题目链接]:http://codeforces.com/contest/509/problem/A [题意] 给你一个递推式f[i][j] = f[i-1][j]+f[i][j-1]; 让你求f[i ...
- 【codeforces 803C】Maximal GCD
[题目链接]:http://codeforces.com/contest/803/problem/C [题意] 给你一个数字n;一个数字k; 让你找一个长度为k的序列; 要求这个长度为k的序列的所有数 ...
随机推荐
- js dom 的常用属性和方法
1.对象集合: (1).all[]; (2).images[]; (3).anchors[]; (4).forms[]; (5).links[]; ...
- sqlalchemy的merge使用
1.先看下文档 merge(instance, load=True) Copy the state of a given instance into a corresponding instance ...
- 最近遇到的C++数字和字符串的转换问题
1. 用itoa 和atoi 在头文件#include<cstidlib> itoa用法: char * itoa ( int value, char * str, int base ) ...
- Android实现SQLite数据库的增、删、改、查的操作
核心代码DAO类 package com.examp.use_SQLite.dao; import java.util.ArrayList; import java.util.List; import ...
- 删除DataGridView选中行并更新数据库
前面写过一篇文章是DataGridView控件显示数据的,DataGridView在与数据库打交道时会常常出现,也非常有用.通过DataGridView对数据库进行更改和查询都比較方便. 这里我们须要 ...
- 小老虎CSDN博客流量分析
小老虎CSDN博客流量分析 一.分析的博客对象 http://blog.csdn.net/littletigerat 二.分析的时间节点 2014年7月10日星期四 三.PV.UV以及IP值 wa ...
- weexpack build android 和 weexpack run android 报错 及 解决方案
1. weexpack build android (1)Configuring > 0/3 projects > root project > Resolving dependen ...
- HTML5与Javascript 实现网页弹球游戏
终于效果图: 1. 使用html 5 的canvas 技术和javascript实现弹球游戏 总体流程图: 1.1 html5 canvas技术的使用 首先在html页面中定义画布. <canv ...
- 线程相关函数(POSIX线程):
创建单个线程 #include <pthread.h> // 若成功返回0,出错返回正的Exxx值 int pthread_create(pthread_t *tid, // 每个线程在进 ...
- SQL Server故障转移集群
在XenServer集群上给客户搭建一个应用服务,要求有负载均衡,Web服务器用Windows Server 2008 R2 + IIS,数据库Sql Server 2008 R2,并且使用SAN存储 ...