Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport


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
14
output
6
input
20
output
15
input
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.

题意:给定一个当前数xn,选择一个比xn小的素数,然后取这个素数的倍数中比xn大的最小的数作为xn+1;输入一个x2,输出x0最小的可能解。

时间复杂度O(n*sqrt(n))的解法:

容易得到,对于每一个xn,假设它的一个质因数为p,那么xn-1的取值范围是((xn/p - 1) * p, xn];那么显然对于这个数所有的质因数,小的质因数对应的域是属于大的质因数对应的域的。所以对于输入x2,求解其最大的质因数可以直接对应得到x1的取值范围。

由先前证明已经得到,xn所对应的最小的xn-1的值是确定的,即(xn/p - 1) * p + 1,因此预处理素数和将该式的值,对所有得到的取值范围内的x1按该值从小到大排序,输出最小的那个值即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("
")
using namespace std;
typedef long long ll;

const int maxn = 1e6+1;
int isp[maxn];
struct num {
    int k, p, maxp;
    int vis;
    bool operator <(const num &c) const {
        return p < c.p;
    }
}n[maxn];

void init()
{
    for(int i=0;i<maxn;i++)
    {
        n[i].k = i;
        n[i].p = i;
        n[i].vis = 0;
        n[i].maxp = 1;
    }
    for(int i=2;i<maxn;i++)
        if(!isp[i])
        {
            n[i].maxp = i;
            for(int j=i+i;j<maxn;j+=i)
            {
                isp[j] = 1;
                n[j].maxp = i;
                n[j].p = min(n[j].p, (n[j].k/i - 1)*i + 1);
            }
        }
}


int main()
{
    init();
    int x;
    scanf("%d",&x);
    int p2 = n[x].maxp;
    if(x / p2 < 2) printf("%d
", x);
    else
    {
        int s = (x / p2 - 1) * p2 + 1;
        sort(n+s, n+x+1);
        
        printf("%d
", n[s].p);
    }
   
}
原文地址:https://www.cnblogs.com/HazelNut/p/8612940.html