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 ...
随机推荐
- [POI2005] SKA-Piggy Banks - DFS
有N个小猪存钱罐.每一个存钱罐能够用相应的钥匙打开或者被砸开.Byteazar已经将钥匙放入到一些存钱罐中.现在已知每个钥匙所在的存钱罐,Byteazar想要买一辆小汽车,而且需要打开所有的存钱罐.然 ...
- Arcgis runtime sdk .net 二次开发
前段时间研究了下 arcgis runtime sdk .net 二次开发··这里做个笔记 runtime版本为100.6 基于WPF 开发 命名空间引入 xmlns:esri="http: ...
- data_analysis 第一课
1.anaconda的安装与使用 在官网下载anaconda的客户端,因为python有2和3之分,所以有两个版本可以供选择,由于该课程使用2作为开发工具,选择anaconda2下载安装. 安装好之后 ...
- Windows里面的虚拟机,部署的项目在同一网段也可以访问到
一直想对自己Linux里面部署的项目进行再同一网段进行共享让其他和我同网段的人都看到 今天对于这个问题我终于解决了 首先编辑自己的虚拟机 进行虚拟机网络编辑 选择VMnet8 然后进行下面的NET设置 ...
- sqlalchemy_mptt一次调优
问题背景: 我用sqlalchemy_mptt构建了一个多级分类项目,数据库用了sqlite.随着数据条数越来越多,写入速度逐渐变慢,一棵树的插入甚至需要1分钟,远远不能满足需求 分析思路: 1. 批 ...
- 机器学习作业(一)线性回归——Python(numpy)实现
题目太长啦!文档下载[传送门] 第1题 简述:设计一个5*5的单位矩阵. import numpy as np A = np.eye(5) print(A) 运行结果: 第2题 简述:实现单变量线性回 ...
- Python-selenium,使用SenKey模块时所碰到的坑
一.SenKey模块(模拟鼠标键盘操作) :python3中没有该模块,使用PyUserInput模块代替 二.PyUserInput模块安装前需要安装:pywin32和pyHook模块,pywin3 ...
- 短路运算符&&、或运算符||的理解
一.短路运算符&& 1.当只有两个表达式的时候(如a&&b),先看第一个表达式转换成布尔值的结果若为真,那么再看第二个表达式转换成布尔值的结果,就可以返回该表达式的值了 ...
- python面试的100题(15)
41.super函数的具体用法和场景 为了调用父类(超类)的一个方法,可以使用 super() 函数,比如: class A: def spam(self): print('A.spam') clas ...
- c# 让接口实现方法
interface IMy { } static class MyFunc { public static void Func<T>(this T obj) where T : IMy { ...