Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task
题意
构造一个任意两个数都互质的序列,使其字典序大等于a序列且最小。
思路
其实这题乱搞就行了。用到了之前HDdalao教我的素因子分解方法,可以快速地对枚举的数进行检测。
我们维护一个当前已填的数的素因子集合以及一个大于1的自然数集合(考虑最坏情况,我们总可以都用素数来构造这个序列。由素数的密度可知,n/ln(n)要大于1e5,所以该自然数集合上限达到2e6即可)
从自然数集合中从小到大枚举当前要填的数,对其质因子分解,检测是否与前面的数互质,然后该数从自然数集合中删去。互质的话,其素因子加入素因子集合。
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<cmath>
#include<climits>
#include<functional>
#include<set>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<endl
#define fi firfac
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> V;
typedef set<int> S;
typedef queue<int> Q;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=2e6,INF=0x3f3f3f3f;
int minp[maxn],ans[maxn],cnt;
S fac,num;
void init()
{
for (int i=2;i<maxn;++i)
{
num.insert(i);
if (minp[i])
continue;
for (int j=i;j<maxn;j+=i)
{
if (!minp[j])
minp[j]=i;
}
}
}
bool check(int x)
{
int _x=x;
while (x!=1)
{
int p=minp[x];
if (fac.find(p)!=fac.end())
return 0;
while (x%p==0)
x/=p;
}
while (_x!=1)
{
int p=minp[_x];
fac.insert(p);
while (_x%p==0)
_x/=p;
}
return 1;
}
int main()
{
init();//de(minp[2]);
int n;
scanf("%d",&n);
S::iterator it;
bool f=0;
for (int i=0;i<n;++i)
{
int x;
scanf("%d",&x);
it = f? num.begin():num.lower_bound(x);
while (it!=num.end())
{
int t=*it;
if (check(t))
{
if (t>x)
f=1;
ans[cnt++]=t;
break;
}
it=num.erase(it);
}
}
for (int i=0;i<n;++i)
printf("%d ",ans[i]);
return 0;
}
Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)的更多相关文章
- codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质 2 字典序大于等于原数组 3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...
- 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 ...
- 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 ...
- [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的子序列 ...
- Codeforces 862A Mahmoud and Ehab and the MEX
传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...
随机推荐
- poj 1753高斯
和前面的开关问题差不多,就是要理解一下我们方程等号的右端代表的含义是什么.我们建立的方程是想让对位的位置变或者不变,然后生成增广矩阵的时候要多注意一点. ac代码: #include #include ...
- python 画图像训练结果的loss图
得到每个epoch的loss和predict精度后,就可以愉快地画图直观地看出训练结果和收敛性了. # coding:utf-8 import matplotlib.pyplot as plt dat ...
- caffe prototxt分析
测试用prototxt name: "CIFAR10_quick"layer { name: "data" type: "MemoryData&quo ...
- luogu P3773 [CTSC2017]吉夫特
luogu 这里的组合数显然要用\(\text{lucas}\)定理来求,所以考虑\(\text{lucas}\)定理的本质,即把\(n,m\)分别拆分成\(p\)进制串\(\{a\}\{b\}\), ...
- 如何使用jMeter发送两个逻辑上相关的HTTP请求
在前一篇文章使用jMeter构造大量并发的随机HTTP请求里我通过jMeter构造了大量的HTTP GET并发请求,对服务器产生了大量读操作. 现在我有另一个需求场景:假设我开发了一个创建Servic ...
- jQuery EasyUI简介
jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面插件. jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助we ...
- JavaScript 的基本使用
JavaScript 基本语法要求: 1.JS的写法是严格区分大小写的. 2.标识符的起名要求跟java的是一样的,第一个位置可以说字母.下划线.美元符号.其他位置可以字母.下划线.美元符号.数字. ...
- C和指针--链表
1.链表的基本概念 链表(linked list)是一些包含数据的节点的集合.链表中的每个节点通过链或指针连接在一起.程序通过指针访问链表中的节点.通常节点是动态分配的. 2.链表的分类 链表可分为: ...
- Django单表查询及其方法
单表查询 前期准备 首先新建一个test的python文件,然后再manage.py中导入main语句及其下面的复制到新文件中 并导入django 写上django.setup() 就可以导入对应的m ...
- 利用commands模块执行shell命令
利用commands模块执行shell命令 用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态 ...