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 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...
随机推荐
- JSON的parse()和stringfy()方法
1.JSON.parse; 作用:将JavaScript对象表示法的JSON字符串转换为对象(字符串转对象). 语法:JSON.parse(text [, reviver]) text 必选. 一个有 ...
- IIS7.5 在已有的WEB网站上配置FTP发布
IIS7.5 有了很多新特性,例如FashCGI,Rewrite 模块的内置,简易的FTP发布等等,但是即使是微软,也没有详细的文档,本文详细的介绍了如何在现有的WEB网站上建立FTP发布. IIS ...
- [Storm] 内部消息缓存
这篇文件翻译自 http://www.michael-noll.com/blog/2013/06/21/understanding-storm-internal-message-buffers/ 当进 ...
- PHP 数组的遍历的几种方式(以及foreach与for/while+each效率的比较)
* 使用foreach遍历数组时要注意的问题: * 1.foreach在遍历之前会自动重置指针使用其指向第一个元素,所以foreach可以多次遍历 * 2.foreach遍历完成之后,指针是没有指向数 ...
- hadoop之 mr输出到hbase
1.注意问题: 1.在开发过程中一定要导入hbase源码中的lib库否则出现如下错误 TableMapReducUtil 找不到什么-- 2.编码: import java.io.IOExceptio ...
- Aspose.Cells设置自动列宽(最佳列宽)及一些方法总结
/// <summary> /// 设置表页的列宽度自适应 /// </summary> /// <param name="sheet">wor ...
- ubuntu安装php常见错误集锦
一.configure 报错 1.错误类型: Configure: error: Please reinstall the libcurl distribution-easy.h should be ...
- Codeforces 271 Div 2 C. Captain Marmot
题目链接:http://codeforces.com/contest/474/problem/C 解题报告:给一个n,然后输入4*n个平面坐标系上的点,每四个点是一组,每个点有一个中心,这四个点可以分 ...
- Oracle 管道化表函数
在PL/SQL中,如果要返回数据的多个行,必须通过返回一个REF CURSOR的游标,或者一个数据集合(如临时表或物理表)来完成,而REF CURSOR的局限于可以从查询中选择的数据,而数据集合的局限 ...
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
Here is this topic’s background: I defined a custom View which extends FrameLayout and contains a Te ...