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\)的有奖,那么 ...
随机推荐
- Yoga710笔记本Win10和Ubuntu系统共存
联想yoga710默认安装了win10系统,且使用EFI分区格式,安装Ubuntu不是一般的困难,经公司小哥的帮助下,几次终于完成了Ubuntu和Win10 共存. 经过多次安装测试,暂时能运行成功的 ...
- AI:AI是什么?
古老的哲学对科学有永远的借鉴意义,科学上的咬文嚼字往往会让其丧失完备性. 一.AI是什么 你看起来它有多好,它就有多好.本质只能通过表象来描述,在色即是空的逻辑里,图灵测试也许是最精准的AI测试方式. ...
- 用一条mysql语句插入多条数据
这篇文章主要介绍了在mysql中使用一条sql语句插入多条数据,效率非常高,但是原理其实很简单,希望对大家有所帮助 假如有一个数据表A: id name title addtime 如果需要插入n条数 ...
- MVC 返回404,返回图片,流到数组,apk信息
return HttpNotFound(); byte[] buffer0 = QRCode(); return File(buffer0, @"image/jpeg"); // ...
- vue.js的ajax和jsonp请求
首先要声明使用ajax 在 router下边的 Index.js中 import VueResource from 'vue-resource'; Vue.use(VueResource); ajax ...
- python基础1 格式化输出
转载自:https://www.cnblogs.com/fat39/p/7159881.html %用法 1.整数输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 ...
- [Ynoi2015]我回来了
题目大意: 给定一张无向无权图,每次给定若干个二元组\((x_i,y_i)\),定义点\(u\)满足条件,当且仅当存在\(i\),并满足\(dist(u,x_i)\leqslant y_i\)(\(d ...
- str使用注意点(未完待续)
1.(切片)str[num1,num2],是指截取 str[num1] 到 str[num2-1] 直接的元素
- CodeForces - 274A - k-Multiple Free Set
先上题目 k-Multiple Free Set time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- [MySQLCPU]线上飙升800%,load达到12的解决过程
接到报警通知,负载过高,达到800%,load也过高,有11了. MySQL版本为5.6.12-log 1 top 之后,确实是mysqld进程占据了所有资源. 2 查看error日志,无任何异常 3 ...