Codeforces Round #467 (Div. 2) B. Vile Grasshoppers

B. 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.

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 -1 instead.

Examples
input
Copy
3 6
output
5
input
Copy
3 4
output
-1

题意:你在1的位置,从2到p都是蚱蜢们的位置,每个蚱蜢的跳法:对于位置x的蚱蜢,可以跳到2*x,3*x...不超过y。问你找一个不超过y的最大位置,让所有蚱蜢都跳不到。

思路:想了半天只猜了一个暴力判定素数的办法,只是感觉素数一般都是每隔几个数字出现一次,所以就从y到p递减进行素数判定,如果是了就break,因为素数出现率很高,所以没超时,如果没有素数,说明y到p这个区间很小,同样不会超时。

每次判定O(√n),不过要注意判定素数时,是从2到p就可以了。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll p,y,flag,f,ans;
int main()
{
    while(~scanf("%lld%lld",&p,&y))
    {
        flag=0;
        for(ll i=y;i>p;i--)
        {
            f=0;
            for(ll j=2;j*j<=i&&j<=p;j++)  //这里要j<=p,具体自己想想就可以明白
            {
                if(i%j==0)
                {
                    f=1;
                    break;
                }
            }
            if(f==0)
            {
                flag=1;
                ans=i;
                break;
            }
        }
        if(flag==1)printf("%lld\n",ans);
        else printf("-1\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值