技术好文:VileGrasshoppers

简介: 技术好文:VileGrasshoppers

"

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 //代码效果参考:https://v.youku.com/v_show/id_XNjQwMDE4NDEzNg==.html

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 //代码效果参考:https://v.youku.com/v_show/id_XNjQwMDE4NDE1Mg==.html

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代码:

1 #include [/span>bits/stdc++.h

2 using namespace std;

3 int p,y;bool flag=false;

4 bool isprime(int x){

5 for(int i=2;i*i<=x&&i<=p;++i)//注意最大因子至多为p

6 if(x%i==0)return false;

7 return true;

8 }

9 int main(){

10 cinpy;

11 for(int i=y;i

12 if(isprime(i)){flag=true;cout[i[endl;break;}

13 if(!flag)cout[""-1""[endl;

14 return 0;

15 }

作者:霜雪千年

出处:

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。


"
image.png
相关文章
|
1月前
|
安全 架构师 Java
理论实战源码齐飞!架构师社区疯传的SpringSecurity进阶小册真香
安全管理是Java应用开发中无法避免的问题,随着Spring Boot和微服务的流行,Spring Security受到越来越多Java开发者的重视,究其原因,还是沾了微服务的光。作为Spring家族中的一员,其在和Spring家族中的其他产品如SpringBoot、Spring Cloud等进行整合时,是拥有众多同类型框架无可比拟的优势的。
64 0
|
1天前
|
Linux API Apache
技术好文:saltstackpillar
技术好文:saltstackpillar
|
1天前
|
存储 编解码 索引
技术好文:StudingDay3
技术好文:StudingDay3
|
2天前
|
前端开发
技术好文:wobble
技术好文:wobble
|
2天前
|
API C#
技术好文:xluatips
技术好文:xluatips
|
1天前
|
JSON 程序员 Swift
技术好文:Swit项目
技术好文:Swit项目
|
2天前
|
前端开发 关系型数据库 MySQL
技术好文:R基础学习(三)
技术好文:R基础学习(三)
|
1月前
好文推荐
好文推荐
56 2
|
1月前
|
算法 搜索推荐
太厉害了!腾讯T4大牛把《数据结构与算法》讲透了,带源码笔记
经历过校招的人都知道,算法和数据结构都是不可避免的。 在笔试的时候,最主要的就是靠算法题。像拼多多、头条这种大公司,上来就来几道算法题,如果你没AC出来,面试机会都没有。
|
6月前
|
算法 程序员
GitHub刷屏热议,原来是字节跳动大牛分享数据结构与算法实战笔记
大家都知道,对于程序员来说很大程度上算法就是为了应付面试的。大佬们都说过,学算法之前起码得了解数据结构呀!你是否当程序员这么多年,还只是能手写出个冒泡排序的代码?别怕!我最近正好找到一份字节跳动大牛分享的数据结构与算法实战笔记,在这里分享给大家!