码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 582A. GCD Table 解题报告

时间:2015-10-04 19:33:28      阅读:249      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!