首页 > 其他 > 详细

codeforces 582A. GCD Table 解题报告

时间:2015-10-04 19:33:12      阅读:256      评论:0      收藏:0      [点我收藏+]

题目链接:http://codeforces.com/problemset/problem/582/A

  网上很多题解,就不说了,直接贴代码= =

  官方题解:

  http://codeforces.com/blog/entry/20692

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <map>
 7 #include <vector>
 8 using namespace std;
 9 
10 const int maxn = 500 + 5;
11 map<int, int> cnt;
12 vector<int> ans;
13 int a[maxn*maxn];
14 
15 int GCD(int a, int b)
16 {
17     return b == 0 ? a : GCD(b, a%b);
18 }
19 
20 int main()
21 {
22     int n;
23     #ifndef ONLINE_JUDGE
24         freopen("in.txt", "r", stdin);
25     #endif // ONLINE_JUDGE
26 
27     while (scanf("%d", &n) != EOF) {
28         ans.clear();
29         for (int i = 0; i < n*n; i++) {
30             scanf("%d", &a[i]);
31             cnt[a[i]]++;
32         }
33 
34         sort(a, a+n*n);
35         for (int i = n*n-1; i >= 0; i--) {
36             if (cnt[a[i]] <= 0) {
37                 continue;
38             }
39             cnt[a[i]]--;
40 
41             for (int j = 0; j < ans.size(); j++) {
42                 cnt[GCD(ans[j], a[i])] -= 2;
43             }
44             ans.push_back(a[i]);
45         }
46         for (int i = 0; i < ans.size(); i++) {
47             cout << ans[i] << (i == ans.size()-1 ? \n :  );
48         }
49     }
50     return 0;
51 }

 

codeforces 582A. GCD Table 解题报告

原文:http://www.cnblogs.com/windysai/p/4854825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!