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 ...
随机推荐
- [Python Study Notes]pynput实现对键盘控制与监控
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- contentvalue的探究(结构,用途)
contentvalue类似HASHMAP,但是KEY只能为STRING 该类用于数据库操作时对数据的封装,可以避免使用SQL语句,为后期创建CONTENTPROVIDER提供便利. 如果没有上述需求 ...
- Solr各组件之间的关系图
原文地址:http://blog.csdn.net/clj198606061111/article/details/20854419
- MyBatis配置Setting详细说明
该表格转载自http://blog.csdn.net/summer_yuxia/article/details/53169227 setting是指定MyBatis的一些全局配置属性,这是MyBati ...
- WebSocket详解(一):初步认识WebSocket技术
1.什么是Socket?什么是WebSocket? 对于第1次听说WebSocket技术的人来说,两者有什么区别?websocket是仅仅将socket的概念移植到浏览器中的实现吗? 我们知道,在网络 ...
- ENCODE:DNA 分子元件的百科全书
ENCODE(DNA分子元件的百科全书)是由国家人类基因研究所(NHGRI)资助的一个国际研究联盟, 该联盟的目标是:建立一份综合的人类基因组功能元件的清单,这些基本元件包括那些直接作用蛋白质和RNA ...
- 51NOD1052 最大M字段和
传送门 分析 一眼看去我们自然会想到dp[i][j][k]表示区间[i,j]中选k个子段的最大值.然后我们考虑降去一维.我们设dp[i][j]表示考虑了前i个数,在选了a[i]的情况下共有j个子段的最 ...
- js的学习
对于 ff的 relatedTarget 及IE的toElement fromElement DOM通过event对象的relatedTarget属性提供了相关元素的信息.这个属性只对于mou ...
- Entity Framework Tutorial Basics(4):Setup Entity Framework Environment
Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...
- PartyLocation.get请求
1.PartyLocationDto:partyDto 2.PartyLocationConverter: 3.PartyDto:Public PartyDto 4.PartyLocationToPa ...