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 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.
求一个互素的序列且字典序比 A 序列大的最小序列;
显然,当某一位置的 b[ i ]> a[ i ] 时,我们只需要安排剩下的数使得 b 数列互素即可;
那么这个我们可以用 fg 来标记一下;
如何保证我们的序列互素呢?考虑质因子,
我们用 use 来看最小质因子是否有使用,如果已经使用,那么显然不能选用;
#include<bits/stdc++.h>
using namespace std; #define maxn 200005 int prime[2000004],pre[2000004];
int n;
int a[maxn];
int b[maxn];
bool fg,vis[2000004],use[2000005]; bool judge(int x)
{
// 检验 x 是否有已经用过的质因子
int num[60],cnt=0;
while(vis[x]){
num[++cnt]=pre[x];x/=pre[x];
}
num[++cnt]=x;
for(int i=1;i<=cnt;i++){
if(use[num[i]])return false;
}
return true;
} int main()
{
ios::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
int cnt=0;
for(int i=2;i<=2000000;i++){
if(!vis[i]){
prime[++cnt]=i;
}
for(int j=1;j<=cnt;j++){
int tmp=i*prime[j];
if(tmp>2000000)break;
vis[tmp]=1;
pre[tmp]=prime[j];// 最小质因子
if(i%prime[j]==0)break;
}
}
int j=1;
for(int i=1;i<=n;i++){
if(fg){
while(use[prime[j]])j++;
b[i]=prime[j];
use[prime[j]]=1;
}
else{
int tmp=a[i];
while(!judge(tmp))tmp++;
if(tmp>a[i])fg=1;
b[i]=tmp;
while(vis[tmp]){
use[pre[tmp]]=1;tmp/=pre[tmp];
}
use[tmp]=1;
}
}
for(int i=1;i<=n;i++)cout<<b[i]<<' ';
cout<<endl;
}
CF959D Mahmoud and Ehab and another array construction task 数学的更多相关文章
- [CF959D]Mahmoud and Ehab and another array construction task题解
解法 非常暴力的模拟. 一开始吧\(1 -> 2 \times 10^6\)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则 ...
- codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质 2 字典序大于等于原数组 3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...
- 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 ...
- 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的子序列 ...
- [CF959F]Mahmoud and Ehab and yet another xor task题解
搞n个线性基,然后每次在上一次的基础上插入读入的数,前缀和线性基,或者说珂持久化线性基. 然后一个num数组记录当时线性基里有多少数 然后每次前缀操作一下就珂以了 代码 #include <cs ...
随机推荐
- Java中Return和Finally执行顺序的实现
下面这段代码的执行结果是怎样的呢? publc int test(){ int x; try{ x = 1; return x; }catch(Exception e){ x = 2; return ...
- c#.net常用字符串函数 字符串常用方法 string
RegionsStr = RegionsStr.Remove(RegionsStr.LastIndexOf(","), 1); //去掉最后一个逗号 string html = ...
- js在浏览器下的区别小结(部分)
1.初始化数组: document.write([1,2,3,].length); IE:4//把数组中最后一个逗号后面的当做了undefined元素 FF.Opera.Safari:3 2.join ...
- [Python Study Notes]计算器
# ------------------------------------------------------------------------------------- # @文件: 计算器.p ...
- 进程与进程之间通信Manager
#!/usr/bin/env python from multiprocessing import Process,Manager #Manager进程与进程之间通信 def Foo(i,dic): ...
- 巧用cssText属性
给一个HTML元素设置css属性,如 1 2 3 4 var head= document.getElementById("head"); head.style.width = & ...
- 关于FILL_PARENTE和match_parent布局属性
在观看早期的代码的时候,经常会看到FILL_PARENT属性,但是新的代码中却有了MATCH_PARENT 那么,两者有何区别呢? 答案是,没有,只是换了个名字而已,均为-1
- ps和ai的一些认识
ps主要是一个后期软件,它很大程度上不是一个创作型的软件,这是它的定位.我觉得李涛老师那句话说的很好,ps是对已有的素材进行加工的.这个已有的素材来源包括但不限于拍照.扫描.数绘板.下载的.如果说你想 ...
- Python程序设计8——网络编程
Python是一个很强大的网络编程工具,python内有很多针对场景网络协议的库,在库顶部可以获得抽象层,这样就可以集中精力在程序的逻辑处理上,而不是停留在网络实现的细节中. 1 少数几个网络设计模块 ...
- Bootstrap 组件之 Nav
一.简介 Nav 指导航页.这里 是一个线上例子. 使用了 .nav 的标签就是一个 Nav.下面举例. {注意} 记住,下面的几种导航页都依赖 .nav. 二.导航页 添加 .nav-tabs. & ...