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 ...
随机推荐
- Diag:Diagonal matrices and diagonals of a matrix
Diag:Diagonal matrices and diagonals of a matrix Syntax X = diag(v,k) X = diag(v) v = diag(X,k) v = ...
- 剑指offer 38_统计数组中k出现的个数
思路: 二分法,分别找出第一个和最后一个k出现的位置.相减 加一 #include <stdio.h> //获取第一个K的位置 int getFirstK (int k,int *numb ...
- .each循环的两种使用方法
- bootstrap媒体查询常用写法
@media (max-width: 768px) { /*超小屏幕设备 手机*/ } @media (min-width: 768px) and (max-width: 992px) { /*小屏幕 ...
- HTML5 学习指导
HTML 语义 HTML5为我们提供了很多旨在精确描述内容的语义元素.确保你可以从它丰富的词汇中获益. <!-- bad --> <div id="main"&g ...
- Android getWidth和getMeasuredWidth的区别
getWidth 得到的事某个View的实际尺寸. getMeasuredWidth 得到的是某个View想要在parent view里面占的大小 相比你也见过这样的解释,听起来这样的解释也是云里雾里 ...
- strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置
在一个较长的字符串这查找匹配的字符串或字符,其中strstr()和strchr()是完全一样的. 例: echo strstr('why always you','you'); 输出: you 如果为 ...
- SpringBoot06 统一响应格式
1 要求 每个请求成功后,后台返回的响应格式都是一致的,例如: 2 创建一个视图模型 该模型用于格式化响应数据 package cn.xiangxu.springboottest.model.data ...
- Linux,du、df统计的硬盘使用情况不一致问题
[转]http://blog.linezing.com/?p=2136 Linux,du.df统计的硬盘使用情况不一致问题 在运维Linux服务器时,会碰到需要查看硬盘空间的情况,这时候,通常会使 ...
- Matlab 摄像机标定+畸变校正
博客转载自:http://blog.csdn.net/Loser__Wang/article/details/51811347 本文目的在于记录如何使用MATLAB做摄像机标定,并通过opencv进行 ...