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.
input 3 6 output 5
input 3 4 output -1
题目很简单,从2-y,中找出数 除2-p中的所有数都有余数,没有这样的数就输出-1;
一开始暴力试了下 超时卡在第五点;后来也还是没写出来,参考别人的才有了解决超时是做法;
1e9内有质数约5e7个,每个质数隔的不是很长;可以暴力...(第一次写博客,有不足之处希望多多指教)
#include<stdio.h>
#include<math.h>
int prime(int x)//判断质数 是质数返回 1
{
for(int i=2;i<=sqrt(x);i++)
{
if(x%i==0) return 0;
}return 1;
}
int main()
{
int p,y;
scanf("%d%d",&p,&y);
int ans=0;
while(y>p)
{
if(prime(y)==1)// 先判断质数
{
ans=y; break;
}
int flag=0;
for(int i=2;i<=p;i++)//能否在2-p有因数// 判断质数和有没有因数好像可以放一起
{
if(y%i==0) {flag=1;break;}
}
if(flag==0) //没有因数就可以输出
{
ans=y;break;
}
y--;
}
if(ans==0) printf("-1\n"); else printf("%d\n",y);
}