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!
  • 相关阅读:
    Git上传文件、文件夹到github
    Git管理修改、撤销修改、删除文件
    Git使用教程之从远程库克隆项目(四)
    Git使用教程之在github上创建项目(三)
    Git使用教程之SSH连接方式配置(二)
    Git使用教程之新手也能看懂(一)
    JS字符串截取 “指定字符” 前面和后面的内容!
    vue.js 实现点击展开收起动画
    最简单的手机预览WEB移动端网页的方法
    vue-cli中浏览器图标的配置
  • 原文地址:https://www.cnblogs.com/--560/p/4857292.html
Copyright © 2011-2022 走看看