codeforces 948B Primal Sport

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

难得起个大早,写一篇结题报告吧,这个题我智障想了好久好久,开始我以为是个找规律的题目,没想到在1000000那里翻船了,然后又左思右想,终于想到了

题目大意

有两个人在玩游戏,轮流来的,首先给出一个 X0 X 0 ,每次选取一个质数,这个质数比当前数值小,然后让这个质数的最小倍数大于等于当前数值(等于的时候是说这个质数本身就是当前数值的因数),这个数字便是下一个数字。题目给 X2 X 2 (这个数字是个非质数)的结果,希望你能求出满足要求的最小的 X0 X 0

思路

首先明确一点,每个非质数都可以变成几个质数的乘积,因此 x1 x 1 的范围是[ X2 X 2 -P( X2 X 2 )+1, X2 X 2 ], X0 X 0 的范围是[ X1 X 1 -P( X1 X 1 )+1,X1)],可以看出来选取的质数越大,上一个数字的最小值越小。首先运用素数筛,灵活改变一点,不仅可以筛出非质数,而且标注非质数的最大质数因子,这是最重要的一步,可以求出P( X1 X 1 ),即可求出最小的 X0 X 0

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 1000006;
int prime[maxn];
void getprime()
{
    int n = 2;
    while (n < maxn)
    {
        int num = n * 2;
        while (num < maxn)
        {
            prime[num] = n;
            num += n;
        }
        num = n + 1;
        while (prime[num] != 0 && num < maxn)
        {
            num++;
        }
        n = num;
    }
}
int main()
{
    getprime();
    int x0, x1, x2;
    while (~scanf("%d", &x2))
    {
        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);
        }
        printf("%d\n", x0);
    }
    return 0;
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值