CodeForce 923A PromalSport

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

题意:

两人玩游戏,每人选一个比当前值小的素数a,要求写出一个比当前值大的最小的数,这个数是a的倍数。现在游戏已经进行了两轮,给出第二轮的值,求最小的开始值。

找到X2的最大质因数k,X1的范围就在X2-k+1到X2之间,在这些数用同样方法找X0,取最小值。

#include<bits/stdc++.h>
#define maxn 1000010
#define INF 0x3f3f3f3f
using namespace std;
bool p[maxn];
int ser[maxn];
void prime()
{
    memset(p,1,sizeof(p));
    p[1]=p[0]=0;
    //memset(ser,1,sizeof(ser));
    for(int i=2;i<maxn;i++)
    {
        if(p[i])
        {
            ser[i]=1;
            for(int j=i+i;j<maxn;j+=i)
            {
                p[j]=0;
                ser[j]=i;
            }
        }
    }
}
int main()
{
    int x2;
    prime();
    while(scanf("%d",&x2)!=EOF)
    {
        int ans=INF;
        for(int i=x2-ser[x2]+1;i<x2;i++)
        {
                ans=min(ans,i-ser[i]+1);
        }
        if(ans==INF)
            ans=x2-1;
        printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值