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. [转载]java中import作用详解

    [转载]java中import作用详解 来源: https://blog.csdn.net/qq_25665807/article/details/74747868 这篇博客讲的真的很清楚,这个作者很 ...

  2. spring cloud EurekaClient 多网卡 ip 配置 和 源码分析(转)

    https://blog.csdn.net/qq_30062125/article/details/83856655 1.前言对于spring cloud,各个服务实例需要注册到Eureka注册中心. ...

  3. sql server 查看表中某一字段的排序规则

    SELECT o.name,o.object_id,c.name,c.column_id,c.collation_name   FROM sys.columns c      JOIN sys.obj ...

  4. c# 将datatable中的数据保存到excel文件中

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  5. 安卓进阶之自定义View

    目录 安卓进阶之自定义View 自定义View的工作流程和内容 工作流程 测量阶段和布局阶段的工作内容 View 和 ViewGroup 在测量阶段和布局阶段的区别 绘制阶段的工作内容 上手:实现继承 ...

  6. mysql启动失败“MySQL Daemon failed to start”

    CentOS上,用命令:service mysqld restart 启动mysql报错: # service mysqld restart Stopping mysqld: [ OK ] MySQL ...

  7. 多进程编程——理论讲解与 multiprocessing 模块

    多进程编程 import os pid = os .fork() 功能 :创建新的进程 参数: 无 返回值 :失败返回一个负数 成功:在原有进程中返回新的进程的PID号 在新进程中返回为0* 子进程会 ...

  8. C#验证控件使用方法及常用正则表达式例析(转)

    ASP.NET为开发人员提供了一整套完整的服务器控件来验证用户输入的信息是否有效.这些控件如下: 1.RequiredFieldValidator:验证一个必填字段,如果这个字段没填,那么,将不能提交 ...

  9. 【Day3】5.Python中的lxml模块

    import lxml.etree as le with open('edu.html','r',encoding='utf-8') as f: html = f.read() html_x = le ...

  10. java lambda 所有列求和

    今天做东西的时候遇到一个需求,求list集合所有列的求和.折腾半天也没有搞出来,网上大部分都是单列求和就像下面这样的,其他都差多,什么 min,max avg count 只得到了number这个属性 ...