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?≤?nbi 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 a1a2, ..., an (2?≤?ai?≤?105), the elements of a.

Output

Output n space-separated integers, the i-th of them representing bi.

Examples

Input

52 3 5 4 13

Output

2 3 5 7 11 

Input

310 3 7

Output

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;
}

  

原文地址:https://www.cnblogs.com/JYYHH/p/8835700.html

时间: 2024-10-13 23:40:04

Codeforces 959 D Mahmoud and Ehab and another array construction task的相关文章

codeforces 862B B. Mahmoud and Ehab and the bipartiteness

http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且此图还是一个二分图. 思路: 想得比较复杂了....其实既然已经给出了二分图并且有n-1条边,那么我们就一定可以用染色法对每一个点进行染色,从而将点划分为两个集合,然后从两个集合中分别任意选择一个点连边就行了. 一开始考虑二分图的基本属性是不存在奇数条边的环...并不需要这样,因为两个集合是分开的,

Codeforces 862C - Mahmoud and Ehab and the xor

862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了,0异或x等于x.所以只要把剩下的异或起来变成x就可以了.如果剩下来有3个,那么,这3个数可以是x^i^j,i,j. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back

Codeforces 862B - Mahmoud and Ehab and the bipartiteness

862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+5; bool color[N]; vector<i

E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees. A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in su

CF 862C Mahmoud and Ehab and the xor(异或)

题目链接:http://codeforces.com/problemset/problem/862/C 题目: Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a n

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]

题目:http://codeforces.com/problemset/problem/862/D 题意:交互题,询问15次以内Hamming distance,输出一个二进制串里任意一个0或1的位置 题解:极简单的二分,从最后一位先判断一个,然后二分 根据上次和本次的距离差是否等于二分长度判断在左端还是右端有需要寻找的值寻找另一个. 1 #define _CRT_SECURE_NO_DEPRECATE 2 #pragma comment(linker, "/STACK:102400000,10

D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/D 询问题 fflush(stdout) 调试: 先行给出结果,函数代替输入 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <se

E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #includ