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 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
3 6
Output
5
Input
3 4
Output
-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.

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

想法:判断素数的变形,暴力肯定TLE。

Code

 1 #include<string.h>
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<vector> 
 7 #include<queue>
 8 using namespace std;
 9 #define MAX 0x3f3f3f3f
10 #define fi first
11 #define se second
12 #define LL long long
13 int main()
14 {
15    LL p,y,ans;
16    cin>>p>>y;
17    int cflag=0;
18    for(int i=y;i>p;i--)
19    {
20         int flag=0;
21         for(int j=2;j*j<=i&&j<=p;j++)   //j<=p
22      {
23         if(i%j==0)
24         {
25             flag=1;
26             break;
27         }     
28         }     
29         if(flag==0)
30      {
31       //本来还企图在这里搞一个循环去遍历最大的素数之后的数,好傻...    
32        ans=i;
33        cflag=1; 
34        break;                          
35      } 
36    } 
37       if(cflag) cout<<ans;
38       else      cout<<-1;
39 } 
原文地址:https://www.cnblogs.com/LLbinGG/p/8496771.html