D. Mahmoud and Ehab and another array construction task 因子分解模板

题意

给出一个原序列a 找出一个字典序大于a的序列b,使得任意 \(i!=j\),\(gcd(a[i],a[j])==1\),现在要你找出这样的序列b,并且满足所有合法序列中输出字典序最小的那个

思路

维护一个set,set里面装所有当前可以取的合法元素,先把所有的数字放进set里面,因为要求字典序最小的序列b,并且b的字典序要大于a,当构造的b到当前位置截止时和a相同时,每次在set里面二分找第一个大于等于当前位置a的值,当不相同时,直接每次取set的最小的元素也就是首元素以满足构造的b字典序最小

因子分界模板 (听说是nlog(n),有空证一下)

void init(){
for(int i=2;i<maxn;i++){
if(!vis[i]){
vis[i]=1;
for(int j=i;j<maxn;j+=i){
vis[j]=1;
v[j].pb(i);
}
}
}
}

AC代码

#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
using namespace std;
const int maxn=2e6+200;
bool vis[maxn+5];
set<int>s;
vector<int>v[maxn];
void init(){
for(int i=2;i<maxn;i++){
if(!vis[i]){
vis[i]=1;
for(int j=i;j<maxn;j+=i){
vis[j]=1;
v[j].pb(i);
}
}
s.insert(i);
}
}
bool vis2[maxn];
int main(){
int n;
init();
scanf("%d",&n);
int tmp;
int flag=1;
for(int i=1;i<=n;i++){
scanf("%d",&tmp);
int now=*s.begin();
if(flag){
now=*s.lower_bound(tmp);
if(now!=tmp)flag=0;
}
//s.erase(now);
printf("%d ",now);
for(int &x:v[now]){
for(int j=x;j<maxn;j+=x){
if(!vis2[j]){
vis2[j]=1;
s.erase(j);
}
}
}
}
// for(int i=1;i<=n;i++)printf("%d ",ans[i]); return 0;
}

D. 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. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

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

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

  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. [CF959F]Mahmoud and Ehab and yet another xor task题解

    搞n个线性基,然后每次在上一次的基础上插入读入的数,前缀和线性基,或者说珂持久化线性基. 然后一个num数组记录当时线性基里有多少数 然后每次前缀操作一下就珂以了 代码 #include <cs ...

随机推荐

  1. C语言 typedef struct _STUDENT {}STUDENT,*PSTUDENT;

    #include <stdio.h> #include <stdlib.h> #include <string.h> //给stuct _STUDENT 起一个别名 ...

  2. Pandas 中对列 groupby 后进行 sum() 与 count() 区别及 agg() 的使用方法

    groupby[根据哪一列][ 对于那一列].进行计算 代码演示: direction:房子朝向 view_num:看房人数 floor:楼层 计算: A 看房人数最多的朝向 df.groupby([ ...

  3. 【Vue2.x笔记3】从源码看watch对象

    初始化 function initWatch (vm: Component, watch: Object) { for (const key in watch) { const handler = w ...

  4. JS绑定事件处理函数及处理流程

    一.事件绑定的几种方式: 1.1 ele.on+“事件名“:如div.onclick = function(event){ }; 1.1.1这种方式兼容性非常好,但一个元素的同一个事件上只能绑定一个处 ...

  5. springboot~集成DataSource 与 Druid监控配置

    介绍 Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部 ...

  6. SpringBoot学习- 2、使用IDEA创建项目

    SpringBoot学习足迹 上一节使用sts创建项目,感觉只是基于eclipse做了一些界面定制,还是改使用IDEA开发,为了跟上时代,将使用IDEA最新版本,安装各种最新插件. 1.下载IDEA ...

  7. laravel如何向视图传递值

    1.定义路由 Route::get('demo','DemoController@demo'); 2.定义控制器(内with();方法就是定义传递的值 key=>value)=>" ...

  8. vue学习指南:第十三篇(详细) - Vue的 路由 第三篇 ( 路由的缓存 )

    路由的缓存 路由缓存是 Vue组件优化的一个重要方法 为什么实现路由缓存? 为了 组件间 相互切换不会重复加载数据,影响用户体验,我们通常需要将组件的数组实现缓存,当我们点过来,在点的时候会再次发送 ...

  9. php设计模式之适配器模式实例代码

    <?php header("Content-type:text/html;charset=utf-8"); // 适配器模式 /** * 查看天气接口 */ class Ti ...

  10. 1047 Student List for Course

    1039 Course List for Student 依靠unordered_map<string,set<int>> ans 解决问题. 这次依靠unordered_ma ...