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 ...
随机推荐
- Mesos 入门教程
Mesos提供了高效.跨分布式应用程序和框架的资源隔离和共享,支持Hadoop. MPI.Hypertable.Spark等. Mesos是Apache孵化器中的一个开源项目,使用ZooKeeper实 ...
- 在python中重新导入模块
重新加载模块 倘若,更改了已经在 Python shell 中导入的模块,然后重新导入该模块,Python 会认为“我已经导入了该模块,不需要再次读取该文件”,所以更改将无效. 要解决这个问题,有以下 ...
- IIS 7安装ModSecurity实现WAF功能
ModSecurity 是一款开源Web应用防火墙,支持Apache/Nginx/IIS,可作为服务器基础安全设施,还是不错的选择. 系统环境:window 2008 R2+IIS 7 0X01 Mo ...
- X-WAF简单测试体验
X-WAF 最近才关注到的一款云WAF,花了一些时间搭建了一个环境,并做了一些测试,感觉比较适合新手来练习WAF Bypass. X-WAF是一款适用中.小企业的云WAF系统,让中.小企业也可以非常方 ...
- 【重要】攻击动作时间段判断~使用动画time比较动画length和使用一个变量数组做延迟
using UnityEngine; using System.Linq; using System.Collections.Generic; [RequireComponent(typeof(Cha ...
- mybatis 之 resultType="Integer"
public class EcPromoteRuleAdditionalNew extends BaseBO { private String[] promoteRuleIds; public Str ...
- MPAndroidChart market右边显示不全问题
//lineChart.setMarkerView(new CustomChartMarkerView(activity, R.layout.line_chart_maker));//add mark ...
- Linux配置示例:配置java环境变量
1.修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种方法,因为所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题. (1)用文本编辑器打开/etc ...
- iOS使用TFHpple解析html
iOS 开发中解析html 网上有很多写好的解析框架 今天就来讲一下如何用框架TFHpple来解析html 使用TFHpple解析html github地址:https://github.com/to ...
- 保存对象时碰到的问题-列名 'Discriminator' 无效
今天保存对象时碰到问题: {"列名 'Discriminator' 无效.\r\n列名 'Discriminator' 无效."} 百度了一下,百度找到的一个解决: http:/ ...