题意:给你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遍历 ,有点贪心的想法的更多相关文章

  1. 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 ...

  2. codeforces 582A. GCD Table 解题报告

    题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...

  3. Codeforces 338D GCD Table 中国剩余定理

    主题链接:点击打开链接 特定n*m矩阵,[i,j]分值为gcd(i,j) 给定一个k长的序列,问能否匹配上 矩阵的某一行的连续k个元素 思路: 我们要求出一个解(i,j) 使得 i<=n &am ...

  4. codeforces 338D GCD Table

    什么都不会只能学数论QAQ 英文原题不贴了 题意: 有一张N*M的表格,i行j列的元素是gcd(i,j)读入一个长度为k,元素大小不超过10^12的序列a[1..k],问这个序列是否在表格的某一行中出 ...

  5. codeforces 582A GCD Table

    题意简述: 给定一个长度为$n$的序列 将这个序列里的数两两求$gcd$得到$n^2$个数 将这$n^2$个数打乱顺序给出 求原序列的一种可能的情况 ------------------------- ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 第二篇:Hadoop 在Ubuntu Kylin系统上的搭建[图解]

    前言 本文介绍如何在Ubuntu Kylin操作系统上搭建Hadoop平台. 配置 1. 操作系统: Ubuntu Kylin 14.04 2. 编程语言: JDK 1.8 3. 通信协议: SSH ...

  2. phpQuery的用法

    一.phpQuery的hello word! 下面简单举例: include 'phpQuery.php'; phpQuery::newDocumentFile('http://www.phper.o ...

  3. UISegmentedControl的基本用法

    本文转载至 http://www.tuicool.com/articles/yUfURj 原文  http://blog.csdn.net/hmt20130412/article/details/38 ...

  4. Android学习之蓝牙操作

    BluetoothAdapter 用法 蓝牙运行原理:通过BluetoothAdapter 蓝牙适配器处理任务,如果蓝牙被启动之后,系统会自动去搜索其它设备,如果匹配到附近的设备就发送一个广播,Bro ...

  5. RabbitMQ备份交换器

    备份交换器,AlternateExchange(AE): 备份交换器是为了实现没有路由到队列的消息,与上篇介绍到的mandatory都是为了处理没有路由到的消息. AE相对于mandatory逻辑更简 ...

  6. <转>Python: and、or以及and-or

    转自 http://www.cnblogs.com/BeginMan/p/3197123.html 一.and: 在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样,但是它们并不 ...

  7. 《转》Python学习(17)-python函数基础部分

    http://www.cnblogs.com/BeginMan/p/3171977.html 一.什么是函数.方法.过程 推荐阅读:http://www.cnblogs.com/snandy/arch ...

  8. SharpGL学习笔记(三) 投影变换和视点变换

    从本节开始,我们使用SharpGL带的VS2010扩展,来直接生成SharpGL工程. 如果你新建项目时,没有看到下面的SharpGL项目,那么请事先在SharpGL源代码中找到一个叫 ”SharpG ...

  9. 重建索引:ALTER INDEX..REBUILD ONLINE vs ALTER INDEX..REBUILD

    什么时候需要重建索引 1. 删除的空间没有重用,导致 索引出现碎片 2. 删除大量的表数据后,空间没有重用,导致 索引"虚高" 3.索引的 clustering_facto 和表不 ...

  10. 题目1442:A sequence of numbers(数列计算以及二分求幂运用)

    题目链接:http://ac.jobdu.com/problem.php?pid=1442 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...