CodeForces937B:Vile Grasshoppers(素数性质)
The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape.
The pine's trunk includes several branches, located one above another and numbered from 2 to y. Some of them (more precise, from 2 to p) are occupied by tiny vile grasshoppers which you're at war with. These grasshoppers are known for their awesome jumping skills: the grasshopper at branch x can jump to branches
.
Keeping this in mind, you wisely decided to choose such a branch that none of the grasshoppers could interrupt you. At the same time you wanna settle as high as possible since the view from up there is simply breathtaking.
In other words, your goal is to find the highest branch that cannot be reached by any of the grasshoppers or report that it's impossible.
The only line contains two integers p and y (2 ≤ p ≤ y ≤ 109).
Output the number of the highest suitable branch. If there are none, print -1 instead.
3 6
3 4
-1
In the first sample case grasshopper from branch 2 reaches branches 2, 4 and 6 while branch 3 is initially settled by another grasshopper. Therefore the answer is 5.
It immediately follows that there are no valid branches in second sample case.
题意:给定P和Y,求最大的X,满足<=Y,且不是2到P的倍数。
思路: 没想到是暴力求解。。。这里利用了一个性质,1e9范围内相邻的素数距离不超过300。所以我们从Y向下验证是否是“素数”,这里的素数指的的无[2,P]区间的因子。可以想象,由于可以除的数少了,这些“素数”的距离比300更加接近,而检验素数的复杂度不超过根号。于是复杂度<O(300*sqrt(1e9));
#include<bits/stdc++.h>
using namespace std;
int P,Y;
bool isprime(int x){
for(int i=;i<=P&&i*i<=x;i++){
if(x%i==) return false;
} return true;
}
int main()
{
scanf("%d%d",&P,&Y);
for(int i=Y;i>P;i--){
if(isprime(i)) {
printf("%d\n",i);
return ;
}
}
printf("-1\n");
return ;
}
CodeForces937B:Vile Grasshoppers(素数性质)的更多相关文章
- Codeforces Round #467 (Div. 2) B. Vile Grasshoppers
2018-03-03 http://codeforces.com/problemset/problem/937/B B. Vile Grasshoppers time limit per test 1 ...
- Codeforces Round #467 (Div. 2) B. Vile Grasshoppers[求去掉2-y中所有2-p的数的倍数后剩下的最大值]
B. Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard ...
- A - Vile Grasshoppers
Problem description The weather is fine today and hence it's high time to climb the nearby pine and ...
- CF937B Vile Grasshoppers
Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- B. Vile Grasshoppers
http://codeforces.com/problemset/problem/937/B The weather is fine today and hence it's high time to ...
- Codeforces 937.B Vile Grasshoppers
B. Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard ...
- zoj 1562 反素数 附上个人对反素数性质的证明
反素数的定义:对于不论什么正整数,其约数个数记为.比如,假设某个正整数满足:对随意的正整 数.都有,那么称为反素数. 从反素数的定义中能够看出两个性质: (1)一个反素数的全部质因子必定是从2開始的连 ...
- WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化
WPF中的常用布局 一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...
- zoj2562:搜索+数论(反素数)
题目大意:求n以内因子数量最多的数 n的范围为1e16 其实相当于求n以内最大的反素数... 由素数中的 算数基本原理 设d(a)为a的正因子的个数,则 d(n)=(a1+1)(a2+1)..... ...
随机推荐
- 省赛i题/求1~n内所有数对(x,y),满足最大公约数是质数的对数
求1~n内所有数对(x,y),gcd(x,y)=质数,的对数. 思路:用f[n]求出,含n的对数,最后用sum[n]求和. 对于gcd(x,y)=a(设x<=y,a是质数),则必有gcd(x/a ...
- npm 安装vue-cli
TIP:win10下安装,使用管理员身份进行,否则会有权限限制. 1,安装完成node,node有自带的npm,可以直接在cmd中,找到nodeJs安装的路径下,进行命令行全局安装vue-cli.(n ...
- (46)C#注册表及读写
启动注册表:regedit 结构: 注册表一共有7个配置单元用regedit只能看到5个 HKEY_CLASSES_ROOT 包含系统上文件类型的细节(.txt,.doc)等.以及使用那些应用程序可以 ...
- LibieOJ 6165 一道水题 (线性筛)
题目链接 LOJ6165 题目意思其实就是求LCM(1, 2, 3, ..., n) 直接用线性筛求出1到1e8之间的所有质数 然后对于每个质数p,他对答案的贡献为$p^{i}$ 其中$p^{i}$小 ...
- C51单片机头文件和启动文件
STARTUP.A51//启动文件. 清理RAM.设置堆栈等.即执行完start.a51后跳转到.c文件的main函数 <reg51.h> //特殊寄存器的字节地址和位地址,sfr定义字 ...
- How to Upgrade PIP in Windows
https://datatofish.com/upgrade-pip/ 这个网站写的非常的全
- Java面试题总结之数据结构、算法和计算机基础(刘小牛和丝音的爱情故事1)
Java面试题总结之数据结构.算法和计算机基础(刘小牛和丝音的爱情故事1)mp.weixin.qq.com 全文字数: 1703 阅读时间: 大约6 分钟 刘小牛是一名Java程序员,由于天天9 ...
- 提高google网站访问速度
修改:C:\Windows\System32\drivers\etc\hosts文件 # google websites.203.208.46.180 ssl.gstatic.com203.208.4 ...
- 移动端日历选择控件(支持Zepto和JQuery)
移动端日历选择控件(支持Zepto和JQuery) <!DOCTYPE html> <html> <head> <meta charset="utf ...
- CMS - tabBar
Tips:如果网页图片(文字)看不清,请按CTRL+鼠标滚轮 1.建议使用阿里图库 或者 easyicon 2.建议使用81*81且低于40KB的图片(建议jpg) 3.如需查看脑图结构,请点击:ta ...