VK Cup 2018 Round 1: A. 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


题意:初始有一个x0,你可以选择任意一个p满足p<x0且p是质数,之后计算x1为≥x0最小的p的倍数

然后再通过x1按照一样的算法得出一个x2

现给你一个x2,问x0最小可能是多少,保证有解(x2绝对不是质数)


稍加思考会发现,对于当前x2,x1的可能取值范围为[x2-p+1, x2],其中p为x2的最大质因子

暴力[x2-p+1, x2]中的每一个数作为x1,如果x1是质数就无视(因为这样就不可能存在x0),否则计算x0 = x1-p'+1

其中p'是x1的最大质因子,很显然最后答案就是min(x0)


#include<stdio.h>
#include<algorithm>
using namespace std;
int cnt, flag[1005] = {1,1}, pri[1005];
int Cha(int x)
{
	int i, bet;
	bet = 0;
	for(i=1;pri[i]*pri[i]<=x&&pri[i]!=0;i++)
	{
		while(x%pri[i]==0)
		{
			bet = max(bet, pri[i]);
			x /= pri[i];
		}
	}
	if(x!=1)
		bet = max(bet, x);
	return bet;
}
int main(void)
{
	int i, j, x2, now, ans;
	for(i=2;i<=1000;i++)
	{
		if(flag[i])
			continue;
		pri[++cnt] = i;
		for(j=i*i;j<=1000;j+=i)
			flag[j] = 1;
	}
	scanf("%d", &x2);
	now = Cha(x2);
	ans = 10000000;
	for(i=x2-now+1;i<=x2;i++)
	{
		now = Cha(i);
		if(now==i)
			continue;
		ans = min(ans, i-now+1);
	}
	printf("%d\n", ans);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值