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.
输入
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.
样例
inputCopy
3 6
output
5
inputCopy
3 4
output
-1
题意
给两个数 p, y;
求 p到y的区间中 不是2到p之间的倍数的最大数
明显可以知道如果存在这个数
最大的数一定大于等于比y小的那个素数 因为素数与素数之间最大不超过1e3(在1e9的范围内)
那么最大的所用时间大概就是
1e3∗sqrt(1e9)
1
e
3
∗
s
q
r
t
(
1
e
9
)
AC代码
#include <bits/stdc++.h>
using namespace std;
#define CLR(a,b) memset(a,(b),sizeof(a))
#define LL long long
const int MAXN = 1e3+10;
const double eps = 1e-15;
int p, y;
bool prime(int x)
{
for(int i = 2; i*i<=x&&i<=p; i++) {
if(x%i == 0)
return false;
}
return true;
}
int main()
{
cin >> p >> y;
for(int i = y; i > p; i--) {
if(prime(i)) {
cout << i << endl; return 0;
}
}
cout << -1 << endl;
return 0;
}