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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值