zoukankan      html  css  js  c++  java
  • Codeforces Round #467 (Div. 2) B. Vile Grasshoppers[求去掉2-y中所有2-p的数的倍数后剩下的最大值]

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

    [分析]: 给你两个数p,y,求去掉2-y中所有2-p的数的倍数后剩下的最大值,如果没有剩下数输出-1.

    [代码]:和筛法一个意思,注意最大值可以逆向枚举.只要算到sqrt(10^9)(i<=p这个条件的加入是为了防止x可能不是素数,但是x不含有2~p之间的任何一个因数)

    [代码]:

    #include<bits/stdc++.h>
    using namespace std;
    int p, y;
    
    bool ck(int n) {
        for(int i = 2; i * i <= n && i <= p; i++) {
            if(n % i == 0) {
                return false;
            }
        }
        return true;
    }
    
    int main() {
        cin >> p >> y;
        for(int i = y; i > p; i--) {
            if(ck(i)) {
                printf("%d
    ", i);
                return 0;
            }
        }
        puts("-1");
        return 0;
    }
    筛法
  • 相关阅读:
    Promise 对象
    [转] LVM分区在线扩容
    [转] 打开 CMD 时自动执行命令
    [转] FFmpeg常用基本命令
    systemd 之 journalctl
    systemd 之 systemctl
    关于用户权限的加强与理解(上)
    [转] 测试环境下将centos6.8升级到centos7的操作记录
    [搞机] 双网卡做数据均衡负载
    [转] 网络基础知识1:集线器,网桥,交换机
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8491150.html
Copyright © 2011-2022 走看看