题目传送门

题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质  2  字典序大于等于原数组  3 每一个元素都大于2

思路:

1.两个数互质的意思就是没有公因子。所以每确定一个数字之后,就把这个数字的所有公因子全部用vis数组标记一下。

2.每一次找数字都是从a[i]开始找,如果a[i]符合条件则下一个,如果不符合条件就a[i]+1,暴力枚举(贪心),如果有一个地方是大于原数组的,之后的所有数字则从最小的开始找就可以了。

3.找公因子的方法是素数筛法的改编。

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<string>
  6. #include<math.h>
  7. #include<cmath>
  8. #include<time.h>
  9. #include<map>
  10. #include<set>
  11. #include<vector>
  12. #include<queue>
  13. #include<algorithm>
  14. #include<numeric>
  15. #include<stack>
  16. using namespace std;
  17. typedef long long ll;
  18. int n;
  19. const int maxn=3000010;
  20. int a[maxn],prim[maxn],vis[maxn],ans[maxn];
  21. vector<int >fac[maxn];
  22. int main() {
  23. int k=2;
  24. //预处理 找公因子
  25. for(int i=2; i<maxn/2; i++) {
  26. if(!prim[i])
  27. for(int j=2*i; j<maxn; j+=i) {
  28. prim[j]=1;
  29. fac[j].push_back(i);
  30. }
  31. }
  32. for(int i=2; i<maxn; i++)
  33. fac[i].push_back(i);//自己本身也是因子
  34. cin>>n;
  35. for(int i=1; i<=n; i++) {
  36. scanf("%d",&a[i]);
  37. }
  38. bool flag=true;//标记是否出现过>a[i]的情况
  39. int res;
  40. for(int i=1; i<=n; i++) {
  41. if(flag) {
  42. for(res=a[i];; res++) {
  43. bool v=false;
  44. for(int j=0; j<fac[res].size(); j++) {
  45. if(vis[fac[res][j]]) {//如果自己的因子出现过 则这个数字不能用
  46. v=true;
  47. break;
  48. }
  49. }
  50. if(!v) {//如果可以用 就把自己的因子全部标记
  51. for(int j=0; j<fac[res].size(); j++) {
  52. vis[fac[res][j]]=1;
  53. }
  54. ans[i]=res;
  55. vis[res]=1;
  56. break;
  57. }
  58. }
  59. if(ans[i]>a[i]) {
  60. flag=false;
  61. }
  62. } else {
  63. while(k) {//flag改变之后 后面的数字就可以从最小值开始找了 k从2开始
  64. bool v=false;
  65. for(int j=0; j<fac[k].size(); j++) {
  66. if(vis[fac[k][j]]) {
  67. v=true;
  68. break;
  69. }
  70. }
  71. if(!v) {
  72. for(int j=0; j<fac[k].size(); j++) {
  73. vis[fac[k][j]]=1;
  74. }
  75. ans[i]=k;
  76. vis[k]=1;
  77. k++;
  78. break;
  79. }
  80. k++;
  81. }
  82. }
  83. vis[ans[i]]=1;
  84. }
  85. for(int i=1; i<=n; i++) {
  86. printf("%d ",ans[i]);
  87. }
  88. }
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
  1. 5
  2. 2 3 5 4 13
output
Copy
  1. 2 3 5 7 11
input
Copy
  1. 3
  2. 10 3 7
output
Copy
  1. 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. java获取多个汉字的拼音首字母

    本文属于http://java.chinaitlab.com/base/803353.html原创!!! public class PinYin2Abbreviation { // 简体中文的编码范围 ...

  2. Windows平台上通过git下载github的开源代码

    常见指令整理: (1)检查ssh密钥是否已经存在.GitBash. 查看是否已经有了ssh密钥:cd ~/.ssh.示例中说明已经存在密钥 (2)生成公钥和私钥 $ ssh-keygen -t rsa ...

  3. ListView里面嵌套CheckBox

    布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro ...

  4. Nginx 正向代理和反向代理

    正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器这个代理服务器呢,他能访问那个我不能访问的网站于是我先连上代 ...

  5. DB2--sql计算时间差和格式化时间

    格式化时间 db2 格式化时间使用的 TO_CHAR(TIMESTAMP('2017-10-24 21:18:12'),'YYYY-MM-DD'): 测试sql:SELECT TO_CHAR(TIME ...

  6. 基于IFC的施工过程模拟程序(4D BIM)

  7. Boost 线程学习笔记

    Bolg转载自:http://www.cnblogs.com/lvdongjie/p/4447193.html 一: 创建线程 #include <iostream> #include & ...

  8. JavaWeb_泛型(Generic)

    JDK5以前,对象保存到集合中就会失去其特性,取出时通常要程序员手工进行类型的强制转换,这样不可避免的就会引发程序的一些安全性问题.例如: ArrayList list = new ArrayList ...

  9. 关于解决cmd中执行java提示"找不到或无法加载主类"的问题

    昨天学生遇到一个问题:在cmd命令行中,用javac编译java文件可以成功,但是用java执行却提示“找不到或无法加载主类”.现将该问题的原因以及解决办法记录一下. 先理解一下系统变量path和cl ...

  10. C++面试笔记--字符串

    基本上求职者进行笔试没有不考字符串的.字符串也是一种相对简单的数据结构,容易被考.事实上,字符创也是一个考验程序猿编程规范和编程习惯的重要考点. 1.替换空格:实现一个函数,把字符串中的每个空格替换成 ...