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 ...
随机推荐
- Go并发原理
Go语言是为并发而生的语言,Go语言是为数不多的在语言层面实现并发的语言:也正是Go语言的并发特性,吸引了全球无数的开发者. 并发(concurrency)和并行(parallellism) 并发(c ...
- springboot中单元测试
测试service: 测试api:
- 【摘自张宴的"实战:Nginx"】使用nginx的proxy_cache模块替代squid,缓存静态文件
#user nobody;worker_processes 1; error_log logs/static_source.error.log;#error_log logs/error.log no ...
- Ubuntu,kubuntu与xubuntu的差别 Ubuntu各版本主要差异
Ubuntu各版本主要差异 Ubuntu官方考虑到使用者的不同需求,提供各种不同的发行版.虽然发布了几种版本的Ubuntu系统,但是它们的核心系统是一模一样的.可以这么说不同发行版的Ubuntu的区别 ...
- Java-马士兵设计模式学习笔记-工厂模式-简单工厂
一.概述 1.目标:要控制任意类型交通工具的生产模式 2.目标有两层意思(1)任意类型 (2)生产模式,所以对应的,要这两个层面上抽象(Movable,VehicleFactory),利用接口,实现多 ...
- Mat类
1.Mat类 早期的opencv中,使用IplImage和CvMat数据结构来表示图像.IplImage和 CvMat 都是c语言的结构.使用这两个结构的问题是内存需要手动管理,开发者必须清楚的知道何 ...
- fiddler----APP弱网测试
转自:http://www.51testing.com/html/01/n-3727001.html APP弱网模拟测试 移动端测试区别于PC端测试的一点就是网络的多变性:不同的网络环境和网络制式的差 ...
- hustOJ SPJ(special judge)模板
#include <stdio.h> #include <math.h> #define PI acos(-1.0) #define AC 0 #define WA 1 int ...
- 使用swiper.animate时,给一个对象添加两个动画且动画循环的方法
swiper官网上给对象加一个动画的方法是 <div class="swiper-slide"> <p class="ani" swiper- ...
- Java50道经典习题-程序29 求矩阵对角线之和
题目:求一个3*3矩阵对角线元素之和分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出. 例如:下面矩阵的对角线之和为24 1 4 6 2 5 3 9 7 8 public cla ...