题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质 2 字典序大于等于原数组 3 每一个元素都大于2
思路:
1.两个数互质的意思就是没有公因子。所以每确定一个数字之后,就把这个数字的所有公因子全部用vis数组标记一下。
2.每一次找数字都是从a[i]开始找,如果a[i]符合条件则下一个,如果不符合条件就a[i]+1,暴力枚举(贪心),如果有一个地方是大于原数组的,之后的所有数字则从最小的开始找就可以了。
3.找公因子的方法是素数筛法的改编。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<cmath>
#include<time.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<numeric>
#include<stack>
using namespace std;
typedef long long ll;
int n;
const int maxn=3000010;
int a[maxn],prim[maxn],vis[maxn],ans[maxn];
vector<int >fac[maxn];
int main() {
int k=2;
//预处理 找公因子
for(int i=2; i<maxn/2; i++) {
if(!prim[i])
for(int j=2*i; j<maxn; j+=i) {
prim[j]=1;
fac[j].push_back(i);
}
}
for(int i=2; i<maxn; i++)
fac[i].push_back(i);//自己本身也是因子
cin>>n;
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
bool flag=true;//标记是否出现过>a[i]的情况
int res;
for(int i=1; i<=n; i++) {
if(flag) {
for(res=a[i];; res++) {
bool v=false;
for(int j=0; j<fac[res].size(); j++) {
if(vis[fac[res][j]]) {//如果自己的因子出现过 则这个数字不能用
v=true;
break;
}
}
if(!v) {//如果可以用 就把自己的因子全部标记
for(int j=0; j<fac[res].size(); j++) {
vis[fac[res][j]]=1;
}
ans[i]=res;
vis[res]=1;
break;
}
}
if(ans[i]>a[i]) {
flag=false;
}
} else {
while(k) {//flag改变之后 后面的数字就可以从最小值开始找了 k从2开始
bool v=false;
for(int j=0; j<fac[k].size(); j++) {
if(vis[fac[k][j]]) {
v=true;
break;
}
}
if(!v) {
for(int j=0; j<fac[k].size(); j++) {
vis[fac[k][j]]=1;
}
ans[i]=k;
vis[k]=1;
k++;
break;
}
k++;
}
}
vis[ans[i]]=1;
}
for(int i=1; i<=n; i++) {
printf("%d ",ans[i]);
}
}
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.
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 n space-separated integers, the i-th of them representing bi.
5 2 3 5 4 13
2 3 5 7 11
3 10 3 7
10 3 7
Note that in the second sample, the array is already pairwise coprime so we printed it.