【CodeForces - 948B】【Primal Sport 】

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/81979916

题目:

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 - 1such 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.

题意:小花和她的好朋友嘉瑜君玩游戏 第一个数字Xo确定, 后面的数字X1由前面一个数字确定 ,确定的方式是, 找到小于等于Xo的一个质数, 在质数的倍数中 ,不小于Xo的最小的数字就是下一个数字  NODE:由上文可知 当Xo为质数的时候 Xo与X1可能相等  给出一个X2  要求求出最小的Xo

解题思路:素数筛的plus ,我们找到当前值的最小的最大素因子,然后从  [x0-prime[x0]+1, x0]区间内寻找满足条件的最小值,即  x1的范围是[X2-prime(X2)+1,X2],X0的范围是[X1-prime(X1)+1,X1)],。

ac代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define maxn 1000006
using namespace std;

int prime[maxn];
void db()
{
	prime[0]=prime[1]=1;
	for(int i=2;i<=maxn;i++)
		if(!prime[i])
			for(int j=i*2;j<maxn;j+=i)
				prime[j]=i;	
} 

int main()
{
	db();
	int x0,x1,x2;
	while(scanf("%d",&x2)!=EOF)
	{
		int a=0,b=0;
		b=prime[x2];
		x0=99999999;
		for(int i=x2-b+1;i<=x2;i++)
		{
			a=prime[i];
			x0=min(i-a+1,x0);
		}
		cout<<x0<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/81979916