codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A
网上很多题解,就不说了,直接贴代码= =
官方题解:
http://codeforces.com/blog/entry/20692
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std; const int maxn = + ;
map<int, int> cnt;
vector<int> ans;
int a[maxn*maxn]; int GCD(int a, int b)
{
return b == ? a : GCD(b, a%b);
} int main()
{
int n;
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%d", &n) != EOF) {
ans.clear();
for (int i = ; i < n*n; i++) {
scanf("%d", &a[i]);
cnt[a[i]]++;
} sort(a, a+n*n);
for (int i = n*n-; i >= ; i--) {
if (cnt[a[i]] <= ) {
continue;
}
cnt[a[i]]--; for (int j = ; j < ans.size(); j++) {
cnt[GCD(ans[j], a[i])] -= ;
}
ans.push_back(a[i]);
}
for (int i = ; i < ans.size(); i++) {
cout << ans[i] << (i == ans.size()- ? '\n' : ' ');
}
}
return ;
}
官方解法代码(个人比较喜欢这个)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
using namespace std; const int maxn = + ; map<int, int> cnt;
int ans[maxn]; int gcd(int a, int b)
{
return b == ? a : gcd(b, a % b);
} int main()
{
int a, n; #ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%d", &n) != EOF) {
for (int i = ; i < n*n; i++) {
scanf("%d", &a);
cnt[-a]++; // 为了将数组从大到小排序
} int pos = n-;
for (map<int, int>::iterator mp = cnt.begin(); mp != cnt.end(); ++mp) {
int x = -mp->first; while (mp->second) { // 当次数还有的时候
ans[pos] = x;
--mp->second;
for (int i = pos+; i < n; i++) {
cnt[-gcd(ans[i], x)] -= ;
}
pos--;
} }
for (int i = ; i < n; i++) {
printf("%d%c", ans[i], (i == n- ? '\n' : ' '));
}
}
return ;
}
codeforces 582A. GCD Table 解题报告的更多相关文章
- codeforces 582A GCD Table
题意简述: 给定一个长度为$n$的序列 将这个序列里的数两两求$gcd$得到$n^2$个数 将这$n^2$个数打乱顺序给出 求原序列的一种可能的情况 ------------------------- ...
- Codeforces Round 665 赛后解题报告(暂A-D)
Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- Codeforces Round #277.5 解题报告
又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...
- codeforces A. Table 解题报告
题目链接:http://codeforces.com/problemset/problem/359/A 题目意思:给出一个n行m列的table,你需要选择一个good cell(假设为(x, y), ...
- codeforces 798C.Mike and gcd problem 解题报告
题目意思:给出一个n个数的序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] ) 现在需要对序列a进行若干变换,来构造一个beautiful的序列: b1,b2, ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
- codeforces A. The Wall 解题报告
题目链接:http://codeforces.com/problemset/problem/340/A 这道题目理解不难,就是在[a, b]区间内,找出同时能够被x和y整除的个数.第一次想当然的开了两 ...
- codeforces B. Routine Problem 解题报告
题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...
随机推荐
- Windows10的快捷键和新功能你利用了多少?
win10快捷键大全大家可以来了解一下,今天小编带来了win10常用快捷键,很多朋友喜欢使用快捷键来操作电脑,那么Windows10系统有哪些新的快捷键呢• 贴靠窗口:Win +左/右> Win ...
- objective-c与c++的差异
oc的编译指令为 clang -fobjc-arc -framework Foundation test.m -o test oc中,1表示YES,0表示NO.并不是非0值都是YES,这是因为BOOL ...
- gulp进阶构建项目由浅入深
gulp进阶构建项目由浅入深 阅读目录 gulp基本安装和使用 gulp API介绍 Gulp.src(globs[,options]) gulp.dest(path[,options]) gulp. ...
- WCF自定义扩展,以实现aop!
引用地址:https://msdn.microsoft.com/zh-cn/magazine/cc163302.aspx 使用自定义行为扩展 WCF Aaron Skonnard 代码下载位置: S ...
- Ali相关面试题
接到的电话面试,人比较随和,当时IOS有一段时间没怎么碰了,因为近期一直在用C++,QT做IM.很多回答我都扯到了C++上,所以可能没戏- -! 回想一下,大概有如下几个问题:(都是很常见的问题) 1 ...
- import()函数
- tc 146 2 RectangularGrid(数学推导)
SRM 146 2 500RectangularGrid Problem Statement Given the width and height of a rectangular grid, ret ...
- 原生态js获取节点的方法
<input value="我是用id来获取值的" type="button" onclick="GetById()"/> &l ...
- python 编码 UnicodeDecodeError
将一个py脚本从Centos转到win运行,出错如下: UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: il ...
- 实现百度IFE2015Spring的任务Task3的几个问题和解决办法
1.如何实现页面的宽高自适应浏览器的变化,且有一个最小宽和高 <head> <meta charset = "utf-8"> <title>个人 ...