codeforces #323 div 2 C. GCD Table (暴力?)
2015-10-04 16:16
519 查看
C. GCD Table
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The GCD table G of size n × n for an array of positive integers a of length n is defined by formula
View Code
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The GCD table G of size n × n for an array of positive integers a of length n is defined by formula
/************************************************************************* > File Name: code/#323/CC.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年10月04日 星期日 14时51分08秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define yn hez111qqz #define j1 cute111qqz #define ms(a,x) memset(a,x,sizeof(a)) using namespace std; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; const int N=5E2+7; int a[N*N]; int n; map<int,int>mp; vector<int> ans; int gcd(int a,int b) { return b==0?a:gcd(b,a%b);} int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif mp.clear(); scanf("%d",&n); for ( int i = 0 ; i < n*n ; i++) { scanf("%d",&a[i]); mp[a[i]]++; } sort(a,a+n*n); for ( int i = n*n-1 ;i>=0 ; i--) { if (!mp[a[i]]) continue; mp[a[i]]--; for ( int j = 0 ; j< ans.size() ; j++) { int tmp = gcd(a[i],ans[j]); mp[tmp]--; mp[tmp]--; } ans.push_back(a[i]); } for ( int i = 0 ; i < n-1 ; i++) printf("%d ",ans[i]); printf("%d",ans[n-1]); #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
偶数次 然后我从大往小加,直到有n个为止(算上之前的奇数次的) 错了是因为我想当然得以为...n个数的gcd table 中...对于任意一对gcd(a,b)(a!=b)一定小于等于所有的原始数列的数... 这显然不对...因为第四点的小于等于关系只是针对某对数的局部结论,而没办法推到整体...想当然了.好傻逼啊... 正确的做法其实很接近了... 由于第四个可以知道...最大的数一定是原始数列中的数... 我们可以从已经确定为原始数列的数来排除掉答案. 由于对称性...将a[i]排序后从大到小枚举...每个新出现的数和已经确定为原始数列的数为产生一对相等的gcd...减掉就可以了. 具体见代码:
相关文章推荐
- Android Studio 配置优化
- 联萌十一大决战之如日中天 C. Cinderella(灰姑娘新编)
- jeesite dbname 问题
- 打开谷歌与谷歌学术的最简单方式
- 黑马程序员—OC语言面向对象三大特性
- 79. Word Search (Graph; DFS)
- #hihocoder #1081 : 最短路径·一
- vector::push_back
- 节省空间
- 现代C++ 基于范围的for和for_each语句
- 现代C++ 基于范围的for和for_each语句
- 使用IntelliJ IDEA开发SpringMVC网站(三)数据库配置
- jquery 对属性和css操作
- 360笔试题——处理字符串
- Android技巧小结之Service调用
- ASP.NET连接ACCESS数据库WEB.CONFIG内路径最优写法
- 证券基础--再融资
- SCPC / ACM2015国庆热身赛
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
- linux 网卡配置 和切换