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 ...
随机推荐
- polarssl rsa & aes 加密与解密
上周折腾加密与解密,用了openssl, crypto++, polarssl, cyassl, 说起真的让人很沮丧,只有openssl & polarssl两个库的RSA & AES ...
- win10禁止更新的方法
Windows10强制更新苦恼了很多人,下面提供三种禁止Windows10更新的方法. 禁止Windows update服务 启动任务管理器->选择服务->打开服务. 找到Windows ...
- vmware centos7系统虚拟机复制到其他电脑后不能联网问题解决
虚拟机复制到别的电脑后,无法联网 使用ifconfig命令,没有显示出IP4的网址信息(显示ens33文件配置信息) 在网上查看相关资料,大部分说是mac地址不一致导致 如果配置了mac地址,那么在/ ...
- Java使用dom4j读取xml时报错:org.dom4j.DocumentException: Error on line 2 of document : Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence
1.Java使用dom4j读取xml时报错: org.dom4j.DocumentException: Error on line 2 of document : Invalid byte 2 of ...
- gearman 简介
附件 Gearman.doc 1:介绍gearman 1.1 简介 Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在 ...
- react中的hoc和修饰器@connect结合使用
在学习react-redux的时候,看到了修饰器这个新的属性,这个是es7的提案属性,很方便.于是我用@connect代替了connect(使用的时候需要配置,这里不赘述),省去了很多不必要的代码,但 ...
- 简单的Excel导入(上传、解析、持久化)
/** * excel导入 * @param req * @param resp * @return */ public void excelImport(){ //先将要上传的Excel文件上传到项 ...
- Swift - 类型转换(as as! as?)
swift 类型转换 一,as 1,as使用场合 (1)从派生类转换为基类,向上转型(upcasts) class Animal {} class Cat: Animal {} let cat = C ...
- JS动态设置确认弹窗
多次使用确认弹窗 <!--START 确认收货--> <div class="popout-boxbg out" id="delivery_goods& ...
- could not bind to address 0.0.0.0:80 no listening sockets available, shutting down
在启动apache服务的时候(service httpd start 启动)出现这个问题. 出现这个问题,是因为APACHE的默认端口被占用的缘故.解决方法就是把这个端口占用的程序占用的端口去掉. 使 ...