Codeforces 959D. Mahmoud and Ehab and another array construction task

题意

构造一个任意两个数都互质的序列,使其字典序大等于a序列且最小。

思路

其实这题乱搞就行了。用到了之前HDdalao教我的素因子分解方法,可以快速地对枚举的数进行检测。

我们维护一个当前已填的数的素因子集合以及一个大于1的自然数集合(考虑最坏情况,我们总可以都用素数来构造这个序列。由素数的密度可知,n/ln(n)要大于1e5,所以该自然数集合上限达到2e6即可)

从自然数集合中从小到大枚举当前要填的数,对其质因子分解,检测是否与前面的数互质,然后该数从自然数集合中删去。互质的话,其素因子加入素因子集合。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<cmath>
#include<climits>
#include<functional>
#include<set>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<endl
#define fi firfac
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> V;
typedef set<int> S;
typedef queue<int> Q;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=2e6,INF=0x3f3f3f3f;
int minp[maxn],ans[maxn],cnt;
S fac,num;
void init()
{
for (int i=2;i<maxn;++i)
{
num.insert(i);
if (minp[i])
continue;
for (int j=i;j<maxn;j+=i)
{
if (!minp[j])
minp[j]=i;
}
}
}
bool check(int x)
{
int _x=x;
while (x!=1)
{
int p=minp[x];
if (fac.find(p)!=fac.end())
return 0;
while (x%p==0)
x/=p;
}
while (_x!=1)
{
int p=minp[_x];
fac.insert(p);
while (_x%p==0)
_x/=p;
}
return 1;
}
int main()
{
init();//de(minp[2]);
int n;
scanf("%d",&n);
S::iterator it;
bool f=0;
for (int i=0;i<n;++i)
{
int x;
scanf("%d",&x);
it = f? num.begin():num.lower_bound(x);
while (it!=num.end())
{
int t=*it;
if (check(t))
{
if (t>x)
f=1;
ans[cnt++]=t;
break;
}
it=num.erase(it);
}
}
for (int i=0;i<n;++i)
printf("%d ",ans[i]);
return 0;
}

Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)的更多相关文章

  1. codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)

    题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质  2  字典序大于等于原数组  3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...

  2. D. Mahmoud and Ehab and another array construction task 因子分界模板+贪心+数学

    D. Mahmoud and Ehab and another array construction task 因子分解模板 题意 给出一个原序列a 找出一个字典序大于a的序列b,使得任意 \(i!= ...

  3. Codeforces 959 D Mahmoud and Ehab and another array construction task

    Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of ...

  4. CF959D Mahmoud and Ehab and another array construction task 数学

    Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same l ...

  5. [CF959D]Mahmoud and Ehab and another array construction task题解

    解法 非常暴力的模拟. 一开始吧\(1 -> 2 \times 10^6\)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则 ...

  6. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  7. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  8. 959F - Mahmoud and Ehab and yet another xor task xor+dp(递推形)+离线

    959F - Mahmoud and Ehab and yet another xor task xor+dp+离线 题意 给出 n个值和q个询问,询问l,x,表示前l个数字子序列的异或和为x的子序列 ...

  9. Codeforces 862A Mahmoud and Ehab and the MEX

    传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...

随机推荐

  1. hdu 1869 枚举+Dijstra

    一点小变形就是了..] #include<iostream> #include<cstdio> #define maxn 201 #define inf 999999 usin ...

  2. windows 安装K8s 简易教程

    1. 先安装 chocolatey https://chocolatey.org/install administrator 运行 命令: @"%SystemRoot%\System32\W ...

  3. 初学java2 认识面向对象 以及运算符 输入输出

    面向对象 面向对象是一种程序设计思路,在设计一个程序时不需要考虑内部如何实现,只需要想他要实现什么功能 就像在餐馆点菜一样,你不需要知道他应该怎么做,你只需要决定你要吃什么 面向对象三大特征 继承 封 ...

  4. 数据分析基础之pandas & numpy

    一.jupyter的常用快捷键 - 插入cell: a, b   a是after从后插入  a是before 从前插入 - 删除cell: dd, x 都可以 - 修改cell的模式:m, y - t ...

  5. 1 bootstrapValidator使用

    1 如何使用 引入 <link href="bootstrapValidator.min.css" rel="stylesheet"> css文件 ...

  6. case when语法

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数: CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  7. echarts —— tooltip 鼠标悬浮显示提示框属性

    最近一直在使用echarts,当然也被其中的各种属性整的头大,记录一下其中遇到的问题. tooltip:鼠标悬浮时显示的提示框. 今天想要记录的是[自定义提示框的内容],如下图,鼠标悬浮时提示框内显示 ...

  8. 使用nodejs对Marketing Cloud的contact主数据进行修改操作

    假设在Marketing Cloud有这样一个contact主数据: 现在需求是使用编程语言比如nodejs修改这个contact实例的高亮属性. 代码如下: var config = require ...

  9. mac上配置apidoc环境

    1. 安装node.js 和npm 前往 https://nodejs.org/en/ 下载node.js的最新版本,双击.pkg进行安装 在终端输入 node -v ,如正确输出版本号即安装成功 ( ...

  10. 利用django 实现个人博客 全记录(二)

    上一篇文章已经把基础环境搭建好了 一  创建app D:\学习\python3.7.3\python manage.py startapp blog 修改 博客的 models.py ) ) def ...