zoukankan      html  css  js  c++  java
  • Vile Grasshoppers

    output
    standard output

    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.

    Examples
    input
    Copy
    3 6
    output
    Copy
    5
    input
    Copy
    3 4
    output
    Copy
    -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*p>=y,则直接往下筛素数。 因为能被p+1又不被小于p的数整除的一定大于y。

    否则暴力找。

        #include <iostream>
        #include <bits/stdc++.h>
        using namespace std;
        bool primejudge(int n)
        {
            if(n==2) return true;
            for(int i=2;i<=sqrt(n)+1;i++)
            {
                if(n%i==0) return false;
            }
            return true;
        }
        int main()
        {
            long long p,y;
            int i;
            cin>>p>>y;
            for(i=y;i>p;i--)
            {
                if(p*p>=y)
                {
                  if(primejudge(i))
                  {
                      printf("%d
    ",i);
                      return 0;
                  }
                }
                else
                {   int flag=0;
                    for(int j=2;j<=p;j++)
                    {
                        if(i%j==0)
                        {
                            flag=1;
                        }
                    }
                    if(!flag){printf("%d
    ",i);
                    return 0;}
                }
            }
            printf("-1
    ");
            return 0;
        }
    

      

  • 相关阅读:
    CentOS7 安装 RabbitMQ
    测试工程师 - 要了解的技能总结
    STF 连接其它操作系统上的安卓设备实操介绍【转】
    adb -a server nodaemon,设备一直显示 offline,而 adb devices 一直显示 device【已解决】
    Mac 之 STF 搭建(淘宝源安装)
    无损压缩图片
    jenkins 之 Android 打包及上传至蒲公英
    JoinPoint
    元数据库 information_schema.tables
    @RestControllerAdvice全局异常统一处理
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/8946911.html
Copyright © 2011-2022 走看看