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 ...
随机推荐
- 获取Linux权限后安装rootkit
1.首先获得远程服务器的root权限,当然这是基本的也是最难的. 2.然后下载rootkit程序,本文用到的是mafix. 3.开始安装 wget http://godpock.googlecode. ...
- javascript——对象的概念——创建对象与销毁对象
一.创建对象 1.创建空对象 方式一: var o ={};o; //Object {} typeof(o); //"object" 方式二: var o=new Object() ...
- 如何设置select下拉禁止选择
转自:https://blog.csdn.net/you23hai45/article/details/52233207
- IOS AppStore上线前测试
IOS AppStore上线前测试 TestFlight Beta ios 8才开始支持. https://developer.apple.com/library/ios/documentation ...
- DAY19-上传头像并预览
一个简单的注册页面: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- python爬虫实战(2)--爬取百度贴吧
本篇目标 1.对百度贴吧的任意帖子进行抓取 2.指定是否只抓取楼主发帖内容 3.将抓取到的内容分析并保存到文件 1.URL格式的确定 先观察百度贴吧url格式,以中南财经政法大学迎新帖为例,URL我们 ...
- day35-hibernate映射 03-Hibernate持久态对象自动更新数据库
持久态对象一个非常重要的能力:自动更新数据库. package cn.itcast.hibernate3.demo1; import static org.junit.Assert.*; import ...
- KNN算法实现
import csvimport randomimport mathimport operatorimport numpy as np def loadDataset(filename,split,t ...
- Spring 已看 没用
注解 @Autwired 依赖注入 作用: 自动按照类型注入.当使用注解注入属性时,set方法可以省略.它只能注入其他bean类型.当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在 ...
- WOJ 43 电话邀请
并查集缩点这个trick感觉明明用得很广泛,为什么以前都不知道…… 先把$m$条线路从小到大排个序,这样可以保证之前合并出来的一定是最小的,大的代价不会把小的覆盖掉. 维护两个并查集,一个用来缩点,另 ...