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. MySQL SQL Training

    源于知乎:50道SQL练习题 一.表数据 1.学生表——Student ),Sname ),Sage )); ' , '赵雷' , '1990-01-01' , '男'); ' , '钱电' , '1 ...

  2. windows下将jar包打入maven仓库

    mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=1.27 -Dpac ...

  3. 使用Docker搭建MySQL服务

    一.安装docker windows 和 mac 版可以直接到官网下载 docker desktop linux 的安装方法可以参考 https://www.cnblogs.com/myzony/p/ ...

  4. Maven错误:警告Classpath entry org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER will not be exported or published

    该错误是在我将一个普通的由maven管理的java项目变为javaweb项目后出现的,由警告可以看出是说maven的类路径容器不会被导出或发布(即通过maven管理的依赖不会被导出或发布),那么我们用 ...

  5. Oracle 如何查看当前的实例及切换实例

    一.Oracle查看当前实例 1.打开终端,输入命令sqlpuls / as sysdba连接到数据库 2.输入命令show parameter name便可以查看当前登录数据库的参数配置,如下可以看 ...

  6. RPC、RMI、REST的区别

    初入职场,接触了不少企业常用的技术,与学校实训使用的技术有很大差异,在这里记录一下RPC.RMI与REST的区别. 概念 RPC(Remote Procedure Call,远程过程调用) 一种通过从 ...

  7. 解决WinForm屏幕缩放适配只需修改两个Form的两个属性

    最近要做一个windows下截屏识别文字的程序,调试发现截取的图像显示不完整. 输出了Screen.PrimaryScreen.Bounds.Width获取的值,结果与实际分辨率不同,所以确定了与我的 ...

  8. C#读取某一文件夹下的所有文件夹和文件

    static List<string> list = new List<string>();//定义list变量,存放获取到的路径 /// <summary> // ...

  9. STM32TIM定时器的影子寄存器

    1.简介 在STM32基本定时器的PSC预分频寄存器和ARR自动装载寄存器都有影子寄存器. 我们可以看到基本定时器功能框图上对应的寄存器有影子~ 2.功能 影子寄存器的存在起到一个缓冲的作用. 设置影 ...

  10. Hadoop_16_MapRduce_MapTask并行度(切片)的决定机制

    MapTask的并行度决定map阶段的任务处理并发度,进而影响到整个job的处理速度那么,mapTask并行实例是否越多 越好呢?其并行度又是如何决定呢?Mapper数量由输入文件的数目.大小及配置参 ...