zoukankan      html  css  js  c++  java
  • Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心

    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 x and 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.

    Examples
    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 

                    

    题意: n个数 形成了n*n的 gcd表格 

              现在乱序给你这n*n的 gcd表格 要求你输出这n个数

    题解:因为两个数字的最大公因数一定小于这两个数,所以n*n中最大的两个数字一定在主对角线上,然后排除掉他们的最大公因数后找剩余数字最大的数字,继续排除已知数字的最大公因数,直到找出n个数字为止。这种构造方法的有效性可以证明贪心策略的正确性,注意去掉已知的数字的最大公因数每次-2

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 #include<stack>
     6 #include<vector>
     7 #include<map>
     8 #include<algorithm>
     9 #define ll __int64
    10 #define mod 1e9+7
    11 #define PI acos(-1.0)
    12 using namespace std;
    13 int n;
    14 int a[400005];
    15 map<int,int>mp;
    16 vector<int> ve;
    17 int gcd(int aa,int bb)
    18 {
    19     if(bb==0)
    20         return aa;
    21     else
    22     return gcd(bb,aa%bb);
    23 }
    24 int main()
    25 {
    26     scanf("%d",&n);
    27     for(int i=1;i<=n*n;i++)
    28     {
    29         scanf("%d",&a[i]);
    30         mp[a[i]]++;
    31     }
    32     sort(a+1,a+n*n+1);
    33     for(int i=n*n;i;i--)
    34     {
    35         if(!mp[a[i]])
    36             continue;
    37         mp[a[i]]--;
    38         for(int j=0;j<ve.size();j++)
    39             mp[gcd(ve[j],a[i])]-=2;
    40         ve.push_back(a[i]);
    41     }
    42     for(int i=0;i<=n-1;i++)
    43         cout<<ve[i]<<" ";
    44     cout<<endl;
    45 }
  • 相关阅读:
    Android学习之三:使用DDMS调试程序
    Android学习之二:使用Android文档帮助
    Android学习之四:创建一个简单程序
    Android学习之五:android一些基本控件
    创建Android开发环境
    Android学习之七:使用Container
    Android学习之六:使用Container
    IOSresign keyboard 新法儿
    IOStxt文件UTF8、UTF16格式
    IOSXMPP arc用方法fobjcarc
  • 原文地址:https://www.cnblogs.com/hsd-/p/5683045.html
Copyright © 2011-2022 走看看