codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 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]);
}
}
3 seconds
256 megabytes
standard input
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.
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.
codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)的更多相关文章
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- D. Mahmoud and Ehab and another array construction task 因子分界模板+贪心+数学
D. Mahmoud and Ehab and another array construction task 因子分解模板 题意 给出一个原序列a 找出一个字典序大于a的序列b,使得任意 \(i!= ...
- 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 ...
- 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 ...
- [CF959D]Mahmoud and Ehab and another array construction task题解
解法 非常暴力的模拟. 一开始吧\(1 -> 2 \times 10^6\)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则 ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- 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的子序列 ...
- 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 ...
随机推荐
- office 2010打开doc文档报错:Word 在尝试打开文件时遇到错误
今天在百度文库中下载了几个文档,下载后发现无法打开.出现以下的提示框. 那么,使用多年office的我,这点小问题当然难不倒我啦. 这个问题是由于系统安全设置所导致的 ,所有我们只需要处理这个安全设置 ...
- JMS编程模型
(1) ConnectionFactory创建Connection对象的工厂,针对两种不同的jms消息模型,分别有QueueConnectionFactory和TopicConnectionFacto ...
- css垂直居中方法(一)
第一种方法:首先用margin:0 auto实现水平居中,然后设置position:relative,设置top为50%(父元素高度的50%),然后设置margin-top:-150px(设置负值是因 ...
- 【总结整理】WebGIS基础
1.万维网:www是world wide web的简称是在超文本基础上形成的信息网 2.互联网:即广域局域网及单机按照一定的通讯协议组成的国际计算机网络 3.WebGIS:网络地理信息系统,指基于In ...
- ROS探索总结(六)——使用smartcar进行仿真
转自:https://www.ncnynl.com/archives/201609/843.html 总结: 一.机器人描述文件三个: 机器人主体body文件: gazebo属性文件: 主文件 sma ...
- C++获取电脑上连接的多个摄像头名称与编号
#include<iostream>#include "strmif.h"#include <initguid.h>#include<vector&g ...
- unity3d MonoDevelop引用外部自定义dll文件报错:are you missing an assembly reference?
在unity3d 编辑器 MonoDevelop 中引用外部自定义dll文件报错:are you missing an assembly reference? 因为unity还停留在.NET Fram ...
- java全栈day03--循环高级
今日主要内容介绍1.引用类型变量的创建及使用2.流程控制语句之选择语句3.流程控制语句之循环语句4.循环高级01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scanner ...
- iOS symbolicatecrash工具crash日志分析
若一个App没有加入Crashlytics或者Buggly这些崩溃日志监控,那么我们在App崩溃的时候如何获取崩溃信息呢? 此时我们可以通过symbolicatecrash工具对手机日志来进行分析定位 ...
- 树莓派(Raspberry Pi 3) centos7 扩容内存卡
在使用Win32DiskImager为一张空白的SD卡刷入新的centos7系统后,发现卡上的可用剩余空间并不大,用df -h查看,根目录下的空间使用率居然是25%,而且总空间也就2G左右.网上查到的 ...