zoukankan      html  css  js  c++  java
  • Primal sport

    time limit per test
    1.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try to reach one million by the process described below.

    Alice goes first and then they take alternating turns. In the i-th turn, the player whose turn it is selects a prime number smaller than the current number, and announces the smallest multiple of this prime number that is not smaller than the current number.

    Formally, he or she selects a prime p < Xi - 1 and then finds the minimum Xi ≥ Xi - 1 such that p divides Xi. Note that if the selected prime palready divides Xi - 1, then the number does not change.

    Eve has witnessed the state of the game after two turns. Given X2, help her determine what is the smallest possible starting number X0. Note that the players don't necessarily play optimally. You should consider all possible game evolutions.

    Input

    The input contains a single integer X2 (4 ≤ X2 ≤ 106). It is guaranteed that the integer X2 is composite, that is, is not prime.

    Output

    Output a single integer — the minimum possible X0.

    Examples
    input
    Copy
    14
    output
    Copy
    6
    input
    Copy
    20
    output
    Copy
    15
    input
    Copy
    8192
    output
    Copy
    8191
    Note

    In the first test, the smallest possible starting number is X0 = 6. One possible course of the game is as follows:

    • Alice picks prime 5 and announces X1 = 10
    • Bob picks prime 7 and announces X2 = 14.

    In the second case, let X0 = 15.

    • Alice picks prime 2 and announces X1 = 16
    • Bob picks prime 5 and announces X2 = 20.

    每次找到xi最大的质数因数,然后从xi-pi+1枚举,维护最小值。其中用到了埃式筛法变形。

    #include <iostream>
    #include <bits/stdc++.h>
    #define maxn 1000005
    using namespace std;
    
    int n;
    int prime[maxn];
    void getprime()
    {
        int n = 2;
        while (n < maxn)
        {
            int num = n * 2;
            while (num < maxn)
            {
                prime[num] = n;
                num += n;
            }
            num = n + 1;
            while (prime[num] != 0 && num < maxn)
            {
                num++;
            }
            n = num;
        }
    }
    int main()
    {
        getprime();
        int x2,p2,x1,p1,x0;
        int i,j;
        cin>>x2;
         p2=prime[x2];
        int minum=1e9;
        for(i=x2-p2+1;i<=x2;i++)
        {
             p1=prime[i];
             minum=min(minum,i-p1+1);
        }
        cout<<minum<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    201141 live the lie until the lie becomes your life
    my php & mysql FAQ
    suger日料财务
    python 应用thrift thrift的监控fb303
    cherryPy学习
    my linux FAQ
    Javascript无阻塞加载方法
    设计模式学习笔记之组合模式模式
    【转】cookie
    C# 多线程
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9026775.html
Copyright © 2011-2022 走看看