题目传送门

题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 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 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 ≤ 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 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.

codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)的更多相关文章

  1. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

  2. D. Mahmoud and Ehab and another array construction task 因子分界模板+贪心+数学

    D. Mahmoud and Ehab and another array construction task 因子分解模板 题意 给出一个原序列a 找出一个字典序大于a的序列b,使得任意 \(i!= ...

  3. Codeforces 959 D Mahmoud and Ehab and another array construction task

    Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of ...

  4. 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 l ...

  5. [CF959D]Mahmoud and Ehab and another array construction task题解

    解法 非常暴力的模拟. 一开始吧\(1 -> 2 \times 10^6\)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则 ...

  6. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  7. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  8. 959F - Mahmoud and Ehab and yet another xor task xor+dp(递推形)+离线

    959F - Mahmoud and Ehab and yet another xor task xor+dp+离线 题意 给出 n个值和q个询问,询问l,x,表示前l个数字子序列的异或和为x的子序列 ...

  9. Codeforces 862A Mahmoud and Ehab and the MEX

    传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...

随机推荐

  1. Go并发原理

    Go语言是为并发而生的语言,Go语言是为数不多的在语言层面实现并发的语言:也正是Go语言的并发特性,吸引了全球无数的开发者. 并发(concurrency)和并行(parallellism) 并发(c ...

  2. springboot中单元测试

    测试service: 测试api:

  3. 【摘自张宴的"实战:Nginx"】使用nginx的proxy_cache模块替代squid,缓存静态文件

    #user nobody;worker_processes 1; error_log logs/static_source.error.log;#error_log logs/error.log no ...

  4. Ubuntu,kubuntu与xubuntu的差别 Ubuntu各版本主要差异

    Ubuntu各版本主要差异 Ubuntu官方考虑到使用者的不同需求,提供各种不同的发行版.虽然发布了几种版本的Ubuntu系统,但是它们的核心系统是一模一样的.可以这么说不同发行版的Ubuntu的区别 ...

  5. Java-马士兵设计模式学习笔记-工厂模式-简单工厂

    一.概述 1.目标:要控制任意类型交通工具的生产模式 2.目标有两层意思(1)任意类型 (2)生产模式,所以对应的,要这两个层面上抽象(Movable,VehicleFactory),利用接口,实现多 ...

  6. Mat类

    1.Mat类 早期的opencv中,使用IplImage和CvMat数据结构来表示图像.IplImage和 CvMat 都是c语言的结构.使用这两个结构的问题是内存需要手动管理,开发者必须清楚的知道何 ...

  7. fiddler----APP弱网测试

    转自:http://www.51testing.com/html/01/n-3727001.html APP弱网模拟测试 移动端测试区别于PC端测试的一点就是网络的多变性:不同的网络环境和网络制式的差 ...

  8. hustOJ SPJ(special judge)模板

    #include <stdio.h> #include <math.h> #define PI acos(-1.0) #define AC 0 #define WA 1 int ...

  9. 使用swiper.animate时,给一个对象添加两个动画且动画循环的方法

    swiper官网上给对象加一个动画的方法是 <div class="swiper-slide"> <p class="ani" swiper- ...

  10. Java50道经典习题-程序29 求矩阵对角线之和

    题目:求一个3*3矩阵对角线元素之和分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出. 例如:下面矩阵的对角线之和为24 1 4 6 2 5 3 9 7 8 public cla ...