Codeforces 937.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.
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.
题目大意:找出一个≤y的最大的不是2,3,......,p的倍数的数.
分析:想起了被noip d1t1支配的恐惧......
没什么好的的方法,靠打表观察,可以发现答案离y非常近,从大到小枚举判断是否符合要求即可. 如何判断是否符合要求?根号复杂度枚举质因子,找到一个最小的质因子,看是否>p. 如果找到了或者是一个质数,就是答案了.
打表找规律,发现答案所在的区间比较小就可以直接暴力了,而且cf的B题也不会很难.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll p,y,anss; bool flag = false; int main() { cin >> p >> y; for (ll i = y; i >= p; i--) { ll ans = i; for (ll j = 2; j * j <= i; j++) { if (i % j == 0) { ans = j; break; } } if (ans > p) { flag = true; anss = i; break; } } if (!flag) printf("-1\n"); else cout << anss << endl; return 0; }