• 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)
    #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("");
    }
    View Code
     
  • 相关阅读:
    分享一个文件的工具类
    关于itext生成pdf的新的demo(包含简单的提取txt文件的内容 和xml内容转化为pdf)
    全文检索的Demo
    dom4j操作xml的demo
    利用Java获取ip地址
    利用htmlparser读取html文档的内容
    关于pdfbox操作pdf的分享链接手长
    poi读取word的内容
    基于NPOI对Excel进行简单的操作
    “尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。”
  • 原文地址:https://www.cnblogs.com/sunus/p/4854240.html
Copyright © 2020-2023  润新知