• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
井_中窥月
万物美好,我在中央
博客园    首页    新随笔    联系   管理    订阅  订阅
Codeforces Round #323 (Div. 2) C. GCD Table

C. GCD Table

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/583/problem/C

Description

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


    这道题当时猜了好久的规律,然而却与真相擦肩而过。。。
   题意: 由n个数,可以构成n*n的gcd矩阵,给你n*n个数,表示这个n*n矩阵的元素(是任意顺序的),要求找到这n个数,满足这个gcd矩阵(解不唯一)。
   思路:抓住一点,最大数一定为解。
#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", n)
#define pfi3(a, b, c) printf("%d %d : %d\n", 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("");
}
View Code

 

 
posted on 2015-10-04 10:49  井_中窥月  阅读(163)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3