D. Mahmoud and Ehab and another array construction task 因子分界模板+贪心+数学
D. Mahmoud and Ehab and another array construction task 因子分解模板
题意
给出一个原序列a 找出一个字典序大于a的序列b,使得任意 \(i!=j\),\(gcd(a[i],a[j])==1\),现在要你找出这样的序列b,并且满足所有合法序列中输出字典序最小的那个
思路
维护一个set,set里面装所有当前可以取的合法元素,先把所有的数字放进set里面,因为要求字典序最小的序列b,并且b的字典序要大于a,当构造的b到当前位置截止时和a相同时,每次在set里面二分找第一个大于等于当前位置a的值,当不相同时,直接每次取set的最小的元素也就是首元素以满足构造的b字典序最小
因子分界模板 (听说是nlog(n),有空证一下)
void init(){
for(int i=2;i<maxn;i++){
if(!vis[i]){
vis[i]=1;
for(int j=i;j<maxn;j+=i){
vis[j]=1;
v[j].pb(i);
}
}
}
}
AC代码
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
using namespace std;
const int maxn=2e6+200;
bool vis[maxn+5];
set<int>s;
vector<int>v[maxn];
void init(){
for(int i=2;i<maxn;i++){
if(!vis[i]){
vis[i]=1;
for(int j=i;j<maxn;j+=i){
vis[j]=1;
v[j].pb(i);
}
}
s.insert(i);
}
}
bool vis2[maxn];
int main(){
int n;
init();
scanf("%d",&n);
int tmp;
int flag=1;
for(int i=1;i<=n;i++){
scanf("%d",&tmp);
int now=*s.begin();
if(flag){
now=*s.lower_bound(tmp);
if(now!=tmp)flag=0;
}
//s.erase(now);
printf("%d ",now);
for(int &x:v[now]){
for(int j=x;j<maxn;j+=x){
if(!vis2[j]){
vis2[j]=1;
s.erase(j);
}
}
}
}
// for(int i=1;i<=n;i++)printf("%d ",ans[i]);
return 0;
}
D. Mahmoud and Ehab and another array construction task 因子分界模板+贪心+数学的更多相关文章
- 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序列 ...
- 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 ...
- 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题解
解法 非常暴力的模拟. 一开始吧\(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的子序列 ...
- [CF959F]Mahmoud and Ehab and yet another xor task题解
搞n个线性基,然后每次在上一次的基础上插入读入的数,前缀和线性基,或者说珂持久化线性基. 然后一个num数组记录当时线性基里有多少数 然后每次前缀操作一下就珂以了 代码 #include <cs ...
随机推荐
- SDN-数据控制分离
严格来说,控制面与数据面分离并不是SDN的专利.从一个chassis角度看,传统路由器其实控制面和转发面也是分离的.Route-enginee和line card分别负责控制面板和转发面.但是传统网络 ...
- x86 openwrt编译备忘录
无需自己同步官方openwrt源码,采用Lean的openwrt源码是极好的,感谢Lean长久的坚持. https://github.com/coolsnowwolf/lede 准备工作 注意 不要用 ...
- Web_0003:关于PHP上传文件大小的限制
相关设置如下: 1,file_uploads = on 是否允许通过HTTP上传文件的开关,默认为ON即是开 2,upload_max_filesize = 8m ; 即允许上传文件大小的最大值.默 ...
- ORACLE10G非归档模式下异机迁库(文件迁移)
环境信息: 源库 目标库 操作系统 WIN7 WIN SVR 2012 R2 系统盘符 C,D,E,F C,D IP x.x.x.216 x.x.x.112 数据库版本 10.2.0.4.0 - 64 ...
- C 库函数 - modf()
C 库函数 - modf() C 标准库 - <math.h> 描述 C 库函数 double modf(double x, double *integer) 返回值为小数部分(小数点后的 ...
- xmind修改默认配置
XMIND使用过程中,输入英文字符的时候,第1.2层级的英文字母总是默认大写,手动修改很繁琐.默认字体,想切换成其他类型,也是要手动一个个去修改. 网上找了下相关的问题,找到一些解决办法,整理到文档中 ...
- python之爬虫(爬取.ts文件并将其合并为.MP4文件——以及一些异常的注意事项)
//20200115 最近在看“咱们裸熊——we bears”第一季和第三季都看完了,单单就第二季死活找不到,只有腾讯有资源,但是要vip……而且还是国语版……所以就瞄上了一个视频网站——可以在线观看 ...
- PHPstorm配置xdebug问题小记
安装的是符合自己环境的xdebug,因为是按照xdebug官网的步骤安装的:安装什么版本检测地址:https://xdebug.org/wizard.php,安装操作复制phpinfo()内容之后点击 ...
- (转)java中新生代和老年代
转自:http://blog.csdn.net/lojze_ly/article/details/49456255 聊聊JVM的年轻代 1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不 ...
- 解决jquery.pjax加载后的异常滚动
个人博客 地址:http://www.wenhaofan.com/article/20181106154356 在使用jquery.pjax的时候发现每次加载完成后都会将滚动条滚动至顶部,用户体验极不 ...