Codeforces Round #323 (Div. 2): C. GCD Table(思维题)
题意:
给你一个长度为n的序列a[1]~a[n], 之后用这个序列生成一个n*n的矩阵,其中矩阵第i行第i列的值为a[i],第i行第j列(j!=i)的值为Gcd(a[i], a[j]),现在给你一个矩阵(里面元素被打乱),求复原这个序列(答案可能不唯一输出任意一种)
思路:
可以证明序列中元素顺序不影响答案,将所有矩阵元素从大到小塞入一个可重集合,之后按照以下操作即可:
- 取出当前最大元素x,x一定是原序列中的一个元素
- 暴力x与前面所有已经确定的序列元素的Gcd,将它们从可重集合中删除(注意矩阵因为对称,删除2次)
搞定
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<set>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
multiset<int> st;
int ans[250005];
int Gcd(int x, int y)
{if(y==0)return x;return Gcd(y, x%y);
}
int main(void)
{int n, i, x, cnt;scanf("%d", &n);for(i=1;i<=n*n;i++){scanf("%d", &x);st.insert(x);}cnt = 0;while(st.empty()==0){ans[++cnt] = *(--st.end());st.erase(--st.end());for(i=cnt-1;i>=1;i--){st.erase(st.lower_bound(Gcd(ans[i], ans[cnt])));st.erase(st.lower_bound(Gcd(ans[i], ans[cnt])));}}for(i=1;i<=cnt;i++)printf("%d ", ans[i]);puts("");return 0;
}
Codeforces Round #323 (Div. 2): C. GCD Table(思维题)相关推荐
- Codeforces Round #323 (Div. 2) C. GCD Table
C. GCD Table 题目链接: http://codeforces.com/contest/583/problem/C 题意是给你n个数,可以构成n*n的GCD矩阵,矩阵里的元素顺序可以是任意的 ...
- Codeforces Round #323 (Div. 2) C.GCD Table
C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is define ...
- Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题
A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...
- Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...
- Codeforces Round #587 (Div. 3) C. White Sheet 思维
传送门 文章目录 题意: 思路: 题意: 给你一个白色的矩形和俩个黑色的矩形,问白色被黑色覆盖后还能不能看到. 思路: 经典被简单题卡. 一开始写了个自我感觉很对的做法,结果wa41wa41wa41, ...
- Codeforces Round #636 (Div. 3) E. Weights Distributing 思维 + bfs
传送门 文章目录 题意: 思路: 题意: n≤2e5,m≤2e5n\le2e5,m\le2e5n≤2e5,m≤2e5 思路: 怎么感觉每场div3div3div3都有一个巧妙的图论题. 首先如果只有两 ...
- Codeforces Round #617 (Div. 3) F. Berland Beauty 思维
传送门 文章目录 题意: 思路: 题意: 给定一棵树,再给定若干两点最短路之间边权的最小值,让你给树的边权赋值,使得满足给定的条件,如果不存在输出−1-1−1. 思路: 观察一个性质,加入经过这条边的 ...
- Codeforces Round #323 (Div. 2)
被进爷坑了,第二天的比赛改到了12点 水 A - Asphalting Roads /************************************************ * Author ...
- Codeforces Round #256 (Div. 2) D Multiplication Table
Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the mu ...
最新文章
- python描述器 触发事件_Python面向对象 - 描述器
- zookeeper系列(二)实战master选举
- 推荐 9 个 爱不释手的 JSON 工具!
- 坚果云feodra(linux)启动失败
- Scala教程之:Either
- 系统带你学习 WebAPIs 第五讲
- Dubbo(八)之API 配置
- [剑指offer][JAVA]面试题第[27]题[二叉树的镜像][递归][栈]
- 评一本书:C#.net 手机动漫游戏设计教程 (一)
- java deque.pop_Java - dequeue的介紹及用法
- 深度学习:循环神经网络RNN的变体
- windows、ubuntu下eclipse搭建java、Python环境问题总结
- 背景知识:从熵(Entropy)到互信息(MI,Mutual Information)
- 时间类计算:双代号网络图、单代号网络图、时标网络图
- P1551 亲戚 (并查集)
- AutoResetEvent与ManualResetEvent区别
- 思维导图超级学习力提升宝典
- MES系统到底是什么?
- Error: Could not create the Java Virtual Machine.Error: A fatal exception has occurred. Progra
- 修改电脑中C盘下的文件名
热门文章
- python是干什么的-python学来主要是干什么的
- 用python画皮卡丘-实现童年宝可梦,教你用Python画一只属于自己的皮卡丘
- python是什么类型的编程语言-python和scratch有什么区别
- 中文语音识别系统搭建流程笔记
- 语音识别如何操作?这种语音转文字方法也太好用了吧,简单高效
- 科大讯飞独家Founding赞助国际语音顶会,14篇论文被收录
- c语言马拉松试题,C语言马拉松_04.2_函数与指针
- linux区别sata和sas,SAS与SATA之间的异同分析
- Node-express写接口的详细步骤
- vue中使用vuex结合sessionStorage做的登录功能