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 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...
随机推荐
- Python画图笔记
matplotlib的官方网址:http://matplotlib.org/ 问题 Python Matplotlib画图,在坐标轴.标题显示这五个字符 ⊥ + - ⊺ ⨁,并且保存后也能显示 h ...
- 关于Mysql错误:./bin/mysqld_safe --user=mysql& [1] 32710 121003 16:40:22 mysqld_safe Logging to '/var/log/mysqld.log'. 121003 16:40:22 mysqld_s
[root@www]# ./bin/mysqld_safe --user=mysql&[1] 32710[root@www]# 121003 16:40:22 mysqld_safe Logg ...
- MySQL的Order By Rand()的效率问题
MySQL很多时候需要获取随机数据,举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是: 但是,后来我查了一下MYSQL的官方手册,里面针对RAND()的提示大概意思就是,在OR ...
- jquery Ajax跨域调用WebServices方法
由于公司需要开发一个手机页面,想提供给同事直接在手机上可以查询SAP资料.数据需要使用js调用webserver来获取. 因为初次使用Jquery调用Webserver,所以期间并不顺利.测试调用We ...
- Hadoop 面试题redis
Hadoop 面试题之十 548.redis有什么特别之处,为什么用redis,用hbase 不行么? 答:redis 是基于内存的数据库,速度快 551.redis用什么版本? 3.0以上才支持集群 ...
- IDEA之创建不了.java文件解决
1.问题:在IDEA中新建的maven项目,无法创建.java文件 从上图看出,在new对应的栏目中没有java class选项 2.解决 这是因为maven的配置问题 应该如下: 注:如果这样还不行 ...
- APPCAN MAS接口之SOAP
APPCAN MAS接口中使用webservice接口形式,示例代码如下: 1 var MEAP=require("meap"); 2 3 function run(Par ...
- error C2504 类的多层继承 头文件包含
error C2504:头文件包含不全 今天碰到了很烦的问题,继承一个类之后,感觉头文件都包含了,可还是出现父类未定义的问题,最后发现,子类的子类在实现时,需要在cpp文件中包含所有他的父类的定义.因 ...
- 淘宝(阿里百川)手机客户端开发日记第十四篇 jsp提交含有上传控件表单乱码问题
今天我来总结昨天开发的一个简单的jsp web 应用程序时,在做一个调教表单,从servlet端获取数据,这个表单里含有上传文件控件.如果我们在测试的时候,获取数据的是乱码,这时,大家可以先去掉上传控 ...
- 正则表达式30分钟入门:http://deerchao.net/tutorials/regex/regex.htm#mission
http://deerchao.net/tutorials/regex/regex.htm#mission