zoukankan      html  css  js  c++  java
  • 质因数的思维题

    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 p already 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
    6
    Input
    Copy
    20
    Output
    15
    Input
    Copy
    8192
    Output
    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.

    题目分析 : 给你一个X2, 让你去寻找一个X0, 并且要求 x0 是最小的,选取的规则是每次选取一个质因数,在变换到X的时候是让其扩大一定的倍数,使其刚好大于等于当前的x

    思路分析 :对于我输入的一个X2,我们发现当它的质因数越大时,上一个 x1 可以选取的值就可以越小,因此预处理下每个数最大的质因数就可以了

    代码示例 :

    #define ll long long
    const int maxn = 1e6+5;
    const double pi = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    
    int f[maxn];
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        int x;
        cin >> x;
        for(int i = 2; i <= 1000000; i++){
            if (!f[i]){
                for(int j = i+i; j <= 1000000; j += i){
                    f[j] = i;
                }
            }
            f[i] = i - f[i] + 1;
        }
        //for(int i = 1; i <= x; i++) printf("%d ", f[i]);
        int ans = inf;
        //cout << f[x] << endl;
        for(int i = f[x]; i <= x; i++){
            ans = min(ans, f[i]);
        }
        cout << ans << endl;
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    工厂设计模式
    Java 注解 (Annotation)你可以这样学
    java的多线程和并发库
    NIO高并发基础
    Java编写画图板程序细节-保存已画图形
    程序员必须知道的知识
    慢慢读懂经济【持续更新】-经济浅谈
    Spring IOC与AOP的简单实现
    Python网络爬虫【持续更新】-BeautifulSoup库的使用
    Python网络爬虫【持续更新】-requests库的使用
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8550514.html
Copyright © 2011-2022 走看看