A - Vile Grasshoppers
Problem description
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.
Input
The only line contains two integers p and y (2 ≤ p ≤ y ≤ 109).
Output
Output the number of the highest suitable branch. If there are none, print -1instead.
Examples
Input
3 6
3 4
Output
5
-1
Note
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,求区间(p,y]中不是[2,p]之间的倍数的最大数,明显可知如果存在这个数,那么它一定是不大于y的最大素数。因为在2~1e9的范围内相邻素数之差最大不超过1e3,所以最坏的时间为1e3∗sqrt(1e9)≈1e7,可以A过。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int p,y;bool flag=false;
bool isprime(int x){
for(int i=;i*i<=x&&i<=p;++i)//注意最大因子至多为p
if(x%i==)return false;
return true;
}
int main(){
cin>>p>>y;
for(int i=y;i>p;--i)
if(isprime(i)){flag=true;cout<<i<<endl;break;}
if(!flag)cout<<"-1"<<endl;
return ;
}
A - 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 ...
- 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 ...
- 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 ...
- CodeForces937B:Vile Grasshoppers(素数性质)
The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape. ...
- CF937B Vile Grasshoppers
Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #467 (div.2)
Codeforces Round #467 (div.2) 我才不会打这种比赛呢 (其实本来打算打的) 谁叫它推迟到了\(00:05\) 我爱睡觉 题解 A. Olympiad 翻译 给你若干人的成绩 ...
- codeforce round #467(div.2)
A. Olympiad 给出n个数,让你找出有几个非零并且不重复的数 所以用stl的set //#define debug #include<stdio.h> #include<ma ...
- 【codeforces】【比赛题解】#937 CF Round #467 (Div. 2)
没有参加,但是之后几天打了哦,第三场AK的CF比赛. CF大扫荡计划正在稳步进行. [A]Olympiad 题意: 给\(n\)个人颁奖,要满足: 至少有一个人拿奖. 如果得分为\(x\)的有奖,那么 ...
随机推荐
- hibernate中的懒加载和急加载
懒加载(FatchType.LAZY)是Hibernate为提高程序执行效率而提供的一种机制,简单说就是只有正真使用其属性的时候,数据库才会进行查询. 具体的执行过程就是:Hibernate从数据库获 ...
- 【sqli-labs】 less41 GET -Blind based -Intiger -Stacked(GET型基于盲注的堆叠查询整型注入)
整型的不用闭合引号 http://192.168.136.128/sqli-labs-master/Less-41/?id=1;insert into users(id,username,passwo ...
- mac 上执行 rm -rf /
# 很可怕的指令,清空磁盘所有资料,千万不要用 sudo 尝试,吓的小心肝差掉跳出来 rm -rf / 无聊,想执行rm -rf /会怎样,想起没加sudo时对~/download执行提示权限不足,被 ...
- X509 颁发者和使用者 详解
CN=公用名称C=国家ST=省份L =城市或者区域O=组织名称OU=组织单位名称
- TensorFlow实战笔记(17)---TFlearn
目录: 分布式Estimator 自定义模型 建立自己的机器学习Estimator 调节RunConfig运行时的参数 Experiment和LearnRunner 深度学习Estimator 深度神 ...
- Innodb 中的事务隔离级别和锁的关系
转自:https://tech.meituan.com/innodb-lock.html 前言: 我们都知道事务的几种性质,数据库为了维护这些性质,尤其是一致性和隔离性,一般使用加锁这种方式.同时数据 ...
- Windows10系统如何清除记录和关掉xbox录制
在Windows的Xbox平台玩游戏时,总会发现以前玩过的许多游戏进度都保留着,麻烦的是白白的成就条让人感到相当的不爽,并且想删除还删不掉.某些当年一边玩一边录制的游戏,每当重新打开的时候都会弹出录制 ...
- Python 实现 Excel 里单元格的读写与清空操作
#coding=utf-8 # coding=utf-8 作用是声明python代码的文本格式是utf-8,python按照utf-8的方式来读取程序. # 如果不加这个声明,无论代码中还是注释中有中 ...
- /tmp目录下执行脚本失败提示Permission denied
Linux上执行Shell脚本运行失败提示Permission denied一个问题,挺好的问题,切中了知识盲点. 问题现象 Shell脚本在/tmp目录下,执行./test.sh运行失败,提示Per ...
- Sigma Function 数学 因子求和
Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma ...