zoukankan      html  css  js  c++  java
  • codeforces#323(div2) C. GCD Table 贪心

    codeforces#323(div2) C. GCD Table  贪心

    C. GCD Table
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 test(s)
    input
    4
    2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2
    output
    4 3 6 2
    input
    1
    42
    output
    42 
    input
    2
    1 1 1 1
    output
    1 1 

     先统计次数再从大到小删除就行了,每个数会和添加进的数贡献出两个gcd。

    #include<bits/stdc++.h>
    #define REP(i,a,b) for(int i=a;i<=b;i++)
    #define MS0(a) memset(a,0,sizeof(a))
    
    using namespace std;
    
    typedef long long ll;
    const int maxn=1000100;
    const int INF=(1<<29);
    
    int n;
    ll a[maxn];
    map<ll,int> cnt;
    ll s[maxn],sz;
    ll b[maxn],m;
    
    int main()
    {
        while(cin>>n){
            cnt.clear();sz=0;
            REP(i,1,n*n) scanf("%I64d",&a[i]),cnt[a[i]]++,s[++sz]=a[i];
            sort(s+1,s+n*n+1);
            sz=unique(s+1,s+n*n+1)-(s+1);
            m=0;
            for(int i=sz;i>=1;i--){
                ll t=s[i];
                while(cnt[t]){
                    REP(i,1,m) cnt[__gcd(t,b[i])]-=2;
                    cnt[t]--;
                    b[++m]=t;
                }
            }
            REP(i,1,m){
                if(i==m) printf("%I64d
    ",b[i]);
                else printf("%I64d ",b[i]);
            }
        }
        return 0;
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    图书助手冲刺第四天
    图书助手冲刺第三天
    图书助手冲刺第二天
    图书助手冲刺第一天
    《编写有效用例》读书笔记一
    《需求工程--软件建模与分析》读书笔记三
    《需求工程--软件建模与分析》读书笔记二
    《需求工程--软件建模与分析》读书笔记一
    《探索需求--设计前的质量》阅读笔记三
    “利益相关者”课堂讨论电子版
  • 原文地址:https://www.cnblogs.com/--560/p/4857292.html
Copyright © 2011-2022 走看看