zoukankan      html  css  js  c++  java
  • Vile Grasshoppers

    Problem description

    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 -1instead.

    Examples

    Input

    3 6
    3 4

    Output

    5
    -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.

    解题思路:给两个数 p,y,求区间(p,y]中不是[2,p]之间的倍数的最大数,明显可知如果存在这个数,那么它一定是不大于y的最大素数。因为在2~1e9的范围内相邻素数之差最大不超过1e3,所以最坏的时间为1e3∗sqrt(1e9)≈1e7,可以A过。

    AC代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int p,y;bool flag=false;
     4 bool isprime(int x){
     5     for(int i=2;i*i<=x&&i<=p;++i)//注意最大因子至多为p
     6         if(x%i==0)return false;
     7     return true;
     8 }
     9 int main(){
    10     cin>>p>>y;
    11     for(int i=y;i>p;--i)
    12         if(isprime(i)){flag=true;cout<<i<<endl;break;}
    13     if(!flag)cout<<"-1"<<endl;
    14     return 0;
    15 }
  • 相关阅读:
    背景样式、列表样式、变形样式、过渡动画
    边框样式、段落样式、背景样式
    属性选择符、字体样式和元素样式
    Targets选项下Other linker flags的设置
    OC金额转大写
    输入手机号码 和 金额有效性的判断
    iOS手势冲突问题
    解决iOS手势冲突问题
    iOS开发 字符串的转化 小技巧
    iOS开发添加pch文件
  • 原文地址:https://www.cnblogs.com/acgoto/p/9156911.html
Copyright © 2011-2022 走看看