• Codeforces 937.B Vile Grasshoppers


    B. Vile Grasshoppers
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    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
    5
    input`
    3 4
    output
    -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.

    题目大意:找出一个≤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
    ");
        else
            cout << anss << endl;
    
        return 0;
    }
  • 相关阅读:
    使用边缘计算来改变5G世界中的网络
    解开关于人工智能的六个迷思
    哪些数据将成为区块链系统的关键数据?
    如何通过7个步骤构建机器学习模型
    人工智能的发展体现了人类社会由实向虚的趋势
    5G技术与人工智能的智能结合
    量子计算总是混合的,这需要不断协调
    7.5省队集训 tree
    bzoj2989&4170: 数列
    bzoj1010: [HNOI2008]玩具装箱toy
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8473732.html
Copyright © 2020-2023  润新知