# Codeforces Round #467 (Div. 2)T2题解

c++经典 专栏收录该内容
11 篇文章 0 订阅

http://codeforces.com/contest/937/problem/B

2，3，一开始被占领
2的倍数4，6，被占领
3的倍数6，被占领

B. Vile Grasshoppers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
3 6
output
5
inputCopy
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.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll p,y;//定义p，y
bool prime(ll n) {//质数判断函数
if(n<2)return 0;//这句在本程序中无实际作用，不过判断质数确实是这样的
ll k=sqrt(n);//先将n的平方根存储下来，减少计算量
for(ll i=2; i<=k; i++)
if(n%i==0)return 0;//不是质数
return 1;//是质数
}
int main() {
ios::sync_with_stdio(false);//关闭同步，据说可以增加效率
cin>>p>>y;//输入p,y
for(ll i=y; i>p; i--) {//外层y-p循环
bool flag=0;//是否符合要求
if(prime(i)) {//剪枝2，如果是质数直接输出
cout<<i<<endl;//输出
return 0;//直接结束程序
}
for(ll j=2; j<=p; j++) {//内层2-p循环
if(!prime(j))continue;//剪枝1，如果是合数就舍弃
if(i%j==0) {//如果是2-p中某个数的倍数
flag=1;//即不符合要求
break;//退出内层循环
}
}
if(!flag) {//要是符合要求
cout<<i<<endl;//输出
return 0;//直接结束程序
}
}
cout<<-1<<endl;//运行到这里，说明无解
return 0;//程序要休息了
}

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-03 61

02-26 125
09-05 122
04-08 243
03-04 362
03-06 170
05-07 32
12-12 2224
07-17 212