您的位置:首页 > 运维架构

Vile Grasshoppers CodeForces - 937B

2018-03-14 13:41 375 查看

                                 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.
InputThe only line contains two integers p and y (2 ≤ p ≤ y ≤ 109).
OutputOutput the number of the highest suitable branch. If there are none, print -1instead.
ExampleInput
3 6
Output
5
Input
3 4
Output
-1
NoteIn 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.

大概题意:蚂蚱占领了2~P的树枝 他们可以跳到自己倍数的树枝上。然后你在P~Y上选择你可以没被占领的地方。看例题一 2~3被占领 相应的 2 4 6 3都被占领 此时P~Y就只有5可以取(最高)

理解题意还是很简单代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
using namespace std;
int main()
{
long long p,y,i,j;
bool fg=0;
bool flag=0;
scanf("%lld %lld",&p,&y);
if(y%2==0) y--;//偶数直接自减
for(i=y; i>p; i-=2)//偶数不用判断了 能被2位置的蚂蚱跳到 从上往下枚举
{
fg=0;
for(j=3; j*j<=i&&j<=p; j++)//直接判断就行了
{
if(i%j==0)
{
fg=1;
break;
}
}
if(!fg)
{
cout<<i<<endl;
flag=1;
break;
}
}
if(!flag) cout<<-1<<endl;
}
思维能力还是较弱。继续潜水刷题(逃
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: