Codeforces 959 D Mahmoud and Ehab and another array construction task

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 959 D Mahmoud and Ehab and another array construction task相关的知识,希望对你有一定的参考价值。

?????????date   UI   integer   ica   ons   define   memset   exists   highlight   

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
5
2 3 5 4 13
Output
2 3 5 7 11 
Input
3
10 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;
}

??????

以上是关于Codeforces 959 D Mahmoud and Ehab and another array construction task的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

[CF959E] Mahmoud and Ehab and the xor-MST - 贪心,最小生成树

Codeforces 862D. Mahmoud and Ehab and the binary string 二分(交互题)

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

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

Mahmoud and a Dictionary