D. Mahmoud and Ehab and another array construction task
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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数组,使得b满足b 的字典序恰能大于a,b的最小值大于1
且b数组的任何元素都两两互质。
首先贪心 b肯定要取小的 且互质的
用筛法把每个取过的数的所有因数的所有倍数标记了
怎样快速找所有因数是关键 千万不要一个一个预处理
用下面这个getfactor 保存了每个数的最小因数 然后不停的递归就可以得到所有的因数了 很简单
有一些细节 要注意 不错的题目
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7+5;
//const int maxtot = 1e5+5;
#define rep(i, j, k) for (int i=j; i<k; i++)
int del[maxn], visit[maxn], factor[maxn];
int n, x, tot;
void getfactor(){
/*一定要是sqrt(maxn) 最后rep(i, 2, maxn)不要漏了
这两个循环加起来比一个rep(i, 2, maxn) 快 别搞错了
*/
rep(i, 2, sqrt(maxn)){
if (!factor[i]){
for (int j = i+i; j<maxn; j+=i){
if (!factor[j]) factor[j] = i;
}
factor[i] = i;
}
}
rep(i, 2, maxn){
if (!factor[i]) factor[i] = i;
}
}
void process(int x){
int tmp = x;
while (tmp != 1){
int p = factor[tmp];
//printf("tmp = %d p = %d\n", tmp, p);
if (!visit[p]) {
visit[p] = 1;
for (int j = p; j<maxn; j+=p) {
del[j] = 1;
}
}
while (tmp%p == 0){
tmp/=p;
}
}
}
int main(){
getfactor();
cin>>n;
/*
rep(i, 0, maxn){
printf("factor[%d] = %d ", i, factor[i]);
}
*/
int greater = 0, minn = 2;
rep(i, 0, n){
scanf("%d", &x);
int now = greater? minn : x;
while(del[now]){
now++;
}
if (greater) minn = now+1;
if (now > x) greater = 1;
printf("%d ", now);
del[now] = 1;
process(now);
}
}