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 the same length such that:
- b is lexicographically greater than or equal to a.
- bi ≥ 2.
- b is pairwise coprime: for every 1 ≤ i < j ≤ n, bi and bj are coprime, i. e.GCD(bi, bj) = 1, where GCD(w, z) is the greatest common divisor of w and z.
Ehab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it?
An array x is lexicographically greater than an array y if there exists an index isuch than xi > yi and xj = yj for all 1 ≤ j < i. An array x is equal to an array y if xi = yi for all 1 ≤ i ≤ n.
Input
The first line contains an integer n (1 ≤ n ≤ 105), the number of elements in a andb.
The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 105), the elements of a.
Output
Output n space-separated integers, the i-th of them representing bi.
Examples
5
2 3 5 4 13
2 3 5 7 11
3
10 3 7
10 3 7
Note
Note that in the second sample, the array is already pairwise coprime so we printed it.
字典序显然可以贪心,如果前若干位都和a一样的话,那么我们就找>=a[now] 的可以选的最小的数;否则就直接选现在可以取的最小的数。
然后我们每次选了一个数之和都要把有和这个数至少一个质因子相同的数给删掉,介于一个质因子只会被删一次,所以复杂度还是可靠的。。。
至于数最大处理到多少。。。。我一开始选的10^6然后正好RE了,,换成2*10^6就A 了 。。。。迷
主要是这个选小了会WA,选大了怕T。。。要是考试的时候就取一个最大的不会T的值吧2333.
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int maxn=2000000;
const int inf=1e9;
vector<int> D[maxn+5];
int MIN[maxn*4+5],n;
int le,ri,now,num;
bool v[maxn+5],F=0; inline void maintain(int o,int lc,int rc){
MIN[o]=min(MIN[lc],MIN[rc]);
} void build(int o,int l,int r){
if(l==r){ MIN[o]=l; return;}
int mid=l+r>>1,lc=o<<1,rc=(o<<1)|1;
build(lc,l,mid),build(rc,mid+1,r);
maintain(o,lc,rc);
} inline void init(){
for(int i=2;i<=maxn;i++) if(!v[i])
for(int j=i;j<=maxn;j+=i) v[j]=1,D[j].pb(i);
build(1,1,maxn),memset(v,0,sizeof(v));
} void update(int o,int l,int r){
if(l==r){ MIN[o]=inf; return;}
int mid=l+r>>1,lc=o<<1,rc=(o<<1)|1;
if(le<=mid) update(lc,l,mid);
else update(rc,mid+1,r);
maintain(o,lc,rc);
} void query(int o,int l,int r){
if(l>=le&&r<=ri){ num=min(num,MIN[o]); return;}
int mid=l+r>>1,lc=o<<1,rc=(o<<1)|1;
if(le<=mid) query(lc,l,mid);
if(ri>mid) query(rc,mid+1,r);
} inline void MDF(int x){
for(le=x;le<=maxn;le+=x) if(!v[le]) v[le]=1,update(1,1,maxn);
} inline void solve(){
le=F?2:now,num=inf,ri=maxn,query(1,1,maxn);
printf("%d ",num);
if(num>now) F=1;
for(int i=D[num].size()-1;i>=0;i--) MDF(D[num][i]);
} int main(){
init(),scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&now);
solve();
}
return 0;
}
Codeforces 959 D Mahmoud and Ehab and another array construction task的更多相关文章
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质 2 字典序大于等于原数组 3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...
- D. Mahmoud and Ehab and another array construction task 因子分界模板+贪心+数学
D. Mahmoud and Ehab and another array construction task 因子分解模板 题意 给出一个原序列a 找出一个字典序大于a的序列b,使得任意 \(i!= ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- 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 ...
- [CF959D]Mahmoud and Ehab and another array construction task题解
解法 非常暴力的模拟. 一开始吧\(1 -> 2 \times 10^6\)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则 ...
- Codeforces 959 E Mahmoud and Ehab and the xor-MST
Discription Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- 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的子序列 ...
随机推荐
- PHP必知必会
MQ(消息队列) 消息队列主要用于以下场景: 1. 上传图片,用户需要迅速反馈,把上传图片的后续操作交给consumer 2. A用户对B用户发消息 3. 日志记录,APP发生的任何警告错误日志都要被 ...
- ccf_201712-2
题目 问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向. 游戏开始,从1 ...
- hihoCoder-1109-堆优化的Prim
优先队列是由堆组成的,所以当我们使用优先队列对Prim进行优化时,就把这种优化叫做堆优化. 它的算法核心思想就是每次向后找边,每个pair存的都是下一个点,以及边权.我们对于已经走过的点就避开,这样就 ...
- Re:从零开始的Linux之路(基础篇)
基于 Red Hat Enterprise Linux 7.5 或者 CentOS 7.4 Linux的命令一定遵循以下格式:command指令 [-options]选项 parameter1参数 ...
- 看外设(uart/spis/i2c/i2s)模块设计
1.先看外设接口协议. 2.看具体设计文档. 3.仿真case.
- linux中的硬盘及flash操作
磁盘操作是块设备的必备操作,需要认真掌握. 一.硬盘 1.硬盘文件 默认串口硬盘的设备文件为sda(第一块硬盘).sdb(第二块硬盘).... 默认并口硬盘的设备文件为hda(第一块硬盘).hdb(第 ...
- The North American Invitational Programming Contest 2018 H. Recovery
Consider an n \times mn×m matrix of ones and zeros. For example, this 4 \times 44×4: \displaystyle \ ...
- PAT Basic 1065
1065 单身狗 “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣 ...
- PAT Basic 1038
1038 统计同成绩学生 本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第1行给出不超过10^5^的正整数N,即学生总人数.随后1行给出N名学生的百分制整数成绩,中 ...
- PAT Basic 1022
1022 D进制的A+B 输入两个非负10进制整数A和B(<=2^30^-1),输出A+B的D (1 < D <= 10)进制数. 输入格式: 输入在一行中依次给出3个整数A.B和D ...