CodeForces - 583C GCD Table map的auto遍历 ,有点贪心的想法
题意:给你n*n gcd表中的所有数(以任意顺序) ,求对角线上的n个数分别是什么。gcd表定义如下,先将n个数填在对角线的上,然后将各个格子填上对应对角线上的数的gcd值,也就是V[i][j]=gcd(V[i][i],V[j][j])
题解:观察发现有很多重复的数,而且最大的那个数必然是对角线上的数。所以用map存数据,map.first 存数,map.second存次数。
一开始发现了如果最大的数N重复x*x次,那么对角线上就有x个N,于是每次输出根号次最大的数,用这个规律wa23了(233)
然后又发现了个规律,一次取一个最大值,然后将它与已经取出来的数做gcd得到x,易得x一定不在对角线上,且出现了2次,所以将它从map中erase掉(删去)。
用这个规律可以得到以下算法,
//一开始写得ifelse逻辑有点混乱。。。
ac代码:
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<string.h>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = + ; map<int, int> mp, ans;
int gcd(int a, int b) {
return b == ? a : gcd(b, a%b);
}
int main() {
int n;
cin >> n;
for (int i = ; i <= n*n; i++)
{
int x;
scanf("%d", &x);
mp[x]++;
}
int total = n;
while (total) {
int now = ;
if (mp.rbegin()->second > ) {
now = mp.rbegin()->first, cout << now << ' ', total--, mp.rbegin()->second--;
for (auto j : ans)
mp[gcd(now, j.first)] -= * j.second;
ans[mp.rbegin()->first]++;
}
else { mp.erase(mp.rbegin()->first); }
if (mp.empty() || total == )break;
}
}
CodeForces - 583C GCD Table map的auto遍历 ,有点贪心的想法的更多相关文章
- Codeforces Round #323 (Div. 2) C. GCD Table map
题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- Codeforces 338D GCD Table 中国剩余定理
主题链接:点击打开链接 特定n*m矩阵,[i,j]分值为gcd(i,j) 给定一个k长的序列,问能否匹配上 矩阵的某一行的连续k个元素 思路: 我们要求出一个解(i,j) 使得 i<=n &am ...
- codeforces 338D GCD Table
什么都不会只能学数论QAQ 英文原题不贴了 题意: 有一张N*M的表格,i行j列的元素是gcd(i,j)读入一个长度为k,元素大小不超过10^12的序列a[1..k],问这个序列是否在表格的某一行中出 ...
- codeforces 582A GCD Table
题意简述: 给定一个长度为$n$的序列 将这个序列里的数两两求$gcd$得到$n^2$个数 将这$n^2$个数打乱顺序给出 求原序列的一种可能的情况 ------------------------- ...
- Codeforces Round #323 (Div. 2) C. GCD Table 暴力
C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...
- 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 Round #323 (Div. 1) A. GCD Table
A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)
4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the results of ...
随机推荐
- SpringBoot(五)-- 整合Spring的拦截器
一.步骤 1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口. 2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptor ...
- GitLab 使用
命令行界面的基本操作如下,Web界面的操作参考:https://www.cnblogs.com/pzk7788/p/10291378.html [root@localhost ~]$ gitlab-c ...
- SimpleDateFormat 取当前周的周一和周日的日期,当前月第一个和最后一天的日期
/** * 类说明 :以及获取当前周的周一和周日的日期,当前月第一个和最后一天的日期 * 日期格式化:格式参数 G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) ...
- Google TensorFlow 机器学习框架介绍和使用
TensorFlow是什么? TensorFlow是Google开源的第二代用于数字计算(numerical computation)的软件库.它是基于数据流图的处理框架,图中的节点表示数学运算(ma ...
- 关于GDI+的一些使用基础设置
一.新建一个MFC的单文档工程,例如工程名字叫GDIPLUSTEST1. 二.在工程的stdafx.h头文件中添加入 #include "gdiplus.h" using name ...
- 《C++ Primer Plus》第16章 string类和标准模板库 学习笔记
C++提供了一组功能强大的库,这些库提供了很多常见编程问题的解决方案以及简化其他问题的工具string类为将字符串作为对象来处理提供了一种方便的方法.string类提供了自动内存管理动能以及众多处理字 ...
- ubuntu下升级特定软件与查看软件版本信息
ubuntu 升级软件: sudo apt-get update 更新源 sudo apt-get upgrade 更新已安装的包 sudo apt-get dist-upgrade 升级系统 ubu ...
- 【truffle】Error: `truffle init` no longer accepts a project template name as an argument.
下载范例工程时候.使用命令报错 truffle init webpack 错误如下: Error: `truffle init` no longer accepts a project templat ...
- 【Spring源码分析系列】bean的加载
前言 以 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beans.xml"));为例查看bean的加载过 ...
- JS-缓冲运动基础结构
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...