【题目链接】

点击打开链接

【算法】

G中最大的数一定也是a中最大的数。
          G中次大的数一定也是a中次大的数。

第三、第四可能是由最大和次大的gcd产生的

那么就不难想到下面的算法:

1. 令p为G中最大的数。在G中删除p,a中加入p。
         2 . 对于a中的所有其他数(设为q),在G中删除2个gcd(p, q)。

3. 若G为空则结束;否则回到(1)。

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 500 int i,N,x,k;
map<int,int> M;
int a[MAXN*MAXN];
vector<int> res; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline int gcd(int x,int y) { return !y ? x : gcd(y,x%y); } int main() { read(N);
for (i = ; i <= N * N; i++) {
read(a[i]);
M[a[i]]++;
}
sort(a+,a+N*N+);
for (i = N * N; i >= ; i--) {
if (!M[a[i]]) continue;
--M[a[i]];
for (k = ; k < res.size(); k++) M[gcd(a[i],res[k])] -= ;
res.push_back(a[i]);
}
for (i = ; i < res.size(); i++) {
write(res[i]);
if (i < res.size() - ) putchar(' ');
}
puts(""); return ;
}

【Codeforces 582A】 GCD Table的更多相关文章

  1. 【Codeforces 582A】GCD Table

    [链接] 我是链接,点我呀:) [题意] 给你一个数组A[]经过a[i][j] = gcd(A[i],A[j])的规则生成的二维数组 让你求出原数组A [题解] 我们假设原数组是A 然后让A数组满足A ...

  2. 【CF#338D】GCD Table

    [题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...

  3. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  4. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

  5. 【35.29%】【codeforces 557C】Arthur and Table

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【84.62%】【codeforces 552A】Vanya and Table

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【Codeforces 664A】 Complicated GCD

    [题目链接] 点击打开链接 [算法] gcd(a,a+1) = 1 所以当a = b时,答案为a,否则为1 [代码] #include<bits/stdc++.h> using names ...

  8. 【codeforces 509A】Maximum in Table

    [题目链接]:http://codeforces.com/contest/509/problem/A [题意] 给你一个递推式f[i][j] = f[i-1][j]+f[i][j-1]; 让你求f[i ...

  9. 【codeforces 803C】Maximal GCD

    [题目链接]:http://codeforces.com/contest/803/problem/C [题意] 给你一个数字n;一个数字k; 让你找一个长度为k的序列; 要求这个长度为k的序列的所有数 ...

随机推荐

  1. linux下查看隐藏文件

    linux下查看隐藏文件的快捷键:Ctrl+H 命令:ls -a

  2. Codeforces 959 E Mahmoud and Ehab and the xor-MST

    Discription Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him ...

  3. keras函数式编程(多任务学习,共享网络层)

    https://keras.io/zh/ https://keras.io/zh/getting-started/functional-api-guide/ https://github.com/ke ...

  4. redis hash 类型的操作命令

    redis 文档: https://redis.readthedocs.io/en/2.4/index.html keys * type key --------------------------- ...

  5. scrollReveal 使用

    传统的layzload只能适用于图片懒加载,而我们现在需要的是全部元素的懒加载! 官网:https://scrollrevealjs.org/ gitHub:https://github.com/jl ...

  6. android_checkbox_dialog 设计 是不是要开起 默认不提示对话框

    android_checkbox_dialog设计是不是开起默认不提示 package com.example.android_checkbox_dialog; import android.app. ...

  7. android的armeabi和armeabi-v7a

    在ANE中如果SDK调用了so库,则需要把so库放到ANE下Android-ARM/lib/armeabi (调试模式)或者 armeabi-v7a(发行模式)下. 可以贴个ADT代码说明问题: // ...

  8. java开始到熟悉66-69

    本次内容:DateFormat类 1.DateFormat类 package array; /** * 时间和字符串之间的转化 */ import java.text.DateFormat; impo ...

  9. 浏览器开发者工具Chrome Developer Tool

    开发者工具Chrome Developer Tool https://developers.google.com/chrome-developer-tools/docs/profiles   一直被墙 ...

  10. Json的简单介绍和解析

    Json:JavaScript对象表示法(JavaScript Object Noatation) Json是存储和交换文本信息的语法,类似XML.它采用键值对的方式来组织,易于人们阅读和编写,同时也 ...