Codeforces Round #323 C GCD Table
来源:互联网 发布:android 91 桌面 源码 编辑:程序博客网 时间:2024/06/14 19:56
题意:
由n个数,可以构成n*n的gcd矩阵
给你n*n个数,表示这个n*n矩阵的元素(是任意顺序的)
然后让你找到这n个数,满足这个gcd矩阵
思路:
既然满足矩阵,那么首先我将这n*n个数排序,按照降序找一定是可以的,因为首先要满足矩阵,最大的数一定是n个数中的一个。
既然如此,每在确定一个数之前我将我已经确定的数他们的gcd都找出来,并且-2,为什么要-2? 因为既然我满足矩阵的话,
那么一定会有两个gcd,可以观察图,很容易发现、所以最后直接输出容器里面的数 就可以了、
AC代码:
#include<cstdio>#include<vector>#include<map>#include<algorithm>using namespace std;const int maxn =510;int a[maxn*maxn];int n;int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}int main(){ while(scanf("%d",&n)!=EOF){ vector<int> M; map<int,int> H; M.clear(); H.clear(); for(int i=1;i<=n*n;i++){ scanf("%d",&a[i]); H[a[i]]++; } sort(a+1,a+n*n+1); for(int i=n*n;i;i--){ if(!H[a[i]]) continue; H[a[i]]--; for(int j=0;j<M.size();j++) H[gcd(M[j],a[i])]-=2; M.push_back(a[i]); } printf("%d",M[0]); for(int i=1;i<n;i++) printf(" %d",M[i]); printf("\n"); } return 0;}
0 0
- Codeforces Round #323 C GCD Table
- Codeforces Round #323 (Div. 2) C.GCD Table
- Codeforces Round #323 (Div. 2)C. GCD Table
- Codeforces Round #323 (Div. 2) C. GCD Table
- codeforces #323 c GCD Table(map)
- Codeforces Round #323 (Div. 2) C gcd
- Codeforces Round #323 (Div. 1) A. GCD Table stl应用
- Codeforces 583c GCD Table
- codeforces 583C GCD Table
- Codeforces 583C GCD Table 【gcd + 构造】
- 【Codeforces Round 323 (Div 2)C】【观察找规律 STL map】GCD Table 从GCD矩阵中找出所有原始元素
- Codeforces #323 C. GCD Table (脑洞题、Induction)
- Codeforces #323 div2 C. GCD Table 数论 构造
- 【Codeforces Round #196 (Div. 1)】Codeforces 338D GCD Table
- Codeforces 583C GCD Table 模拟 map
- CodeForces 581C - GCD Table(思维)
- Codeforces 583C GCD Table【贪心】
- Educational Codeforces Round 20-C. Maximal GCD
- 数据密集、计算密集、IO密集
- 将DataGridView转换为DataTable
- R语言-数字、字符与向量
- java反射机制及简单工厂模式
- HDU-5119-Happy Matt Friends
- Codeforces Round #323 C GCD Table
- UITableView
- Cocos2d-x 开发环境搭建
- 学生选课系统中关于窗口关闭又打开不刷新
- IOS 存储和读取用户首项数据
- 1213:How Many Tables
- C#窗体操作SQL Server存储大文件方法分享
- 数据结构应用案例——栈结构用于8皇后问题的回溯求解
- LintCode -- 最小路径和