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 6Output
5Input
3 4Output
-1NoteIn 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; }思维能力还是较弱。继续潜水刷题(逃
相关文章推荐
- CodeForces - 937B(素数打表)
- codeforces 685B Kay and Snowflake 树的重心
- codeforces-Interview with Oleg<水>
- codeforces 817C Really Big Numbers 二分
- codeforces 818G - Four Melodies 费用流,压缩建图,好题
- codeforces 490D Chocolate 数论
- Codeforces 148E Porcelain (dp)
- CodeForces 160D Edges in MST 题解
- CodeForces 738A - Interview with Oleg(水题)
- codeforces 2016-2017 NTUWFTSC I Tree Game
- Codeforces 626F Group Projects 动态规划
- CodeForces 487A Fight the Monster
- Codeforces 24A
- Codeforces 163A Substring and Subsequence
- codeforces - 707A - Brain's Photos(水)
- [Codeforces 817E] Choosing The Commander Trie树
- CodeForces 808A Lucky Year(思维)
- CodeForces 25D Roads not only in Berland 并查集
- CodeForces 621B Wet Shark and Bishops
- codeforces 688B B. Lovely Palindromes(水题)