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.
The only line contains two integers p and y (2 ≤ p ≤ y ≤ 109).
Output the number of the highest suitable branch. If there are none, print -1 instead.
3 6
5
3 4
-1
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.
这题的实质就是素数打表了。不过不能开1e9的数组存,内存会超限,我们每次都素数打表判断一下输出最大值即可,这样不会超时。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3fffffff
using namespace std;
typedef long long LL;
const int N=1e9+1;
int n,p;
bool prime(int x)
{
for(int i=2;i<=p&&i*i<=n;i++)
if(x%i==0)
return false;
return true;
}
int main()
{
scanf("%d%d",&p,&n);
for(int i=n;i>p;i--)
{
if(prime(i))
{
printf("%d\n",i);
return 0;
}
}
printf("-1\n");
}
334

被折叠的 条评论
为什么被折叠?



