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 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 i such 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 and b.

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

Note that in the second sample, the array is already pairwise coprime so we printed it.

求一个互素的序列且字典序比 A 序列大的最小序列;

显然,当某一位置的 b[ i ]> a[ i ] 时,我们只需要安排剩下的数使得 b 数列互素即可;

那么这个我们可以用 fg 来标记一下;

如何保证我们的序列互素呢?考虑质因子,

我们用 use 来看最小质因子是否有使用,如果已经使用,那么显然不能选用;

#include<bits/stdc++.h>
using namespace std;

#define maxn 200005




int prime[2000004],pre[2000004];
int n;
int a[maxn];
int b[maxn];
bool fg,vis[2000004],use[2000005];

bool judge(int x)
{
    // 检验 x 是否有已经用过的质因子
    int num[60],cnt=0;
    while(vis[x]){
        num[++cnt]=pre[x];x/=pre[x];
    }
    num[++cnt]=x;
    for(int i=1;i<=cnt;i++){
        if(use[num[i]])return false;
    }
    return true;
}


int main()
{
    ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    int cnt=0;
    for(int i=2;i<=2000000;i++){
        if(!vis[i]){
            prime[++cnt]=i;
        }
        for(int j=1;j<=cnt;j++){
            int tmp=i*prime[j];
            if(tmp>2000000)break;
            vis[tmp]=1;
            pre[tmp]=prime[j];// 最小质因子
            if(i%prime[j]==0)break;
        }
    }
    int j=1;
    for(int i=1;i<=n;i++){
        if(fg){
            while(use[prime[j]])j++;
            b[i]=prime[j];
            use[prime[j]]=1;
        }
        else{
            int tmp=a[i];
            while(!judge(tmp))tmp++;
            if(tmp>a[i])fg=1;
            b[i]=tmp;
            while(vis[tmp]){
                use[pre[tmp]]=1;tmp/=pre[tmp];
            }
            use[tmp]=1;
        }
    }
    for(int i=1;i<=n;i++)cout<<b[i]<<' ';
    cout<<endl;
}
EPFL - Fighting
原文地址:https://www.cnblogs.com/zxyqzy/p/10016852.html