C. GCD Table
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/583/problem/CDescription
The GCD table G of size n × n for an array of positive integers a of length n is defined by formula
Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both xand y, it is denoted as . For example, for array a = {4, 3, 6, 2} of length 4 the GCD table will look as follows:
Given all the numbers of the GCD table G, restore array a.
Input
The first line contains number n (1 ≤ n ≤ 500) — the length of array a. The second line contains n2 space-separated numbers — the elements of the GCD table of G for array a.
All the numbers in the table are positive integers, not exceeding 109. Note that the elements are given in an arbitrary order. It is guaranteed that the set of the input data corresponds to some array a.
Output
In the single line print n positive integers — the elements of array a. If there are multiple possible solutions, you are allowed to print any of them.
Sample Input
4
2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2
1
42
2
1 1 1 1
Sample Output
4 3 6 2
42
1 1
这道题当时猜了好久的规律,然而却与真相擦肩而过。。。
#include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; #define ll long long #define _cle(m, a) memset(m, a, sizeof(m)) #define repu(i, a, b) for(int i = a; i < b; i++) #define repd(i, a, b) for(int i = b; i >= a; i--) #define sfi(n) scanf("%d", &n) #define pfi(n) printf("%d ", n) #define pfi3(a, b, c) printf("%d %d : %d ", a, b, c) #define MAXN 505 #include<iostream> #include<stdio.h> #include<queue> #include<map> #include<algorithm> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int a[MAXN * MAXN]; map<int, int> H; vector<int> ans; int main() { int n; sfi(n); int t = n * n + 1; repu(i, 1, t) { sfi(a[i]); H[a[i]]++; } sort(a + 1, a + n * n + 1); for(int i = t - 1; i > 0; i--) { if(!H[a[i]]) continue; H[a[i]]--; int siz = ans.size(); repu(j, 0, siz) H[gcd(ans[j], a[i])] -= 2; ans.push_back(a[i]); } repu(i, 0, n) if(!i) printf("%d", ans[i]); else printf(" %d", ans[i]); puts(""); }