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("\n")
using namespace std;
typedef long long ll; const int maxn = 1e6+;
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=;i<maxn;i++)
{
n[i].k = i;
n[i].p = i;
n[i].vis = ;
n[i].maxp = ;
}
for(int i=;i<maxn;i++)
if(!isp[i])
{
n[i].maxp = i;
for(int j=i+i;j<maxn;j+=i)
{
isp[j] = ;
n[j].maxp = i;
n[j].p = min(n[j].p, (n[j].k/i - )*i + );
}
}
} int main()
{
init();
int x;
scanf("%d",&x);
int p2 = n[x].maxp;
if(x / p2 < ) printf("%d\n", x);
else
{
int s = (x / p2 - ) * p2 + ;
sort(n+s, n+x+); printf("%d\n", n[s].p);
} }

Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport的更多相关文章

  1. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)A. Protect Sheep

    http://codeforces.com/contest/948/problem/A   A. Protect Sheep Bob is a farmer. He has a large pastu ...

  2. Codeforces Round #470 (rated, Div. 1, based on VK Cup 2018 Round 1) 923D 947D 948E D. Picking Strings

    题: OvO http://codeforces.com/contest/947/problem/D 923D 947D 948E 解: 记要改变的串为 P1 ,记目标串为 P2  由变化规则可得: ...

  3. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1) C.Producing Snow

    题目链接  题意  每天有体积为Vi的一堆雪,所有存在的雪每天都会融化Ti体积,求出每天具体融化的雪的体积数. 分析 对于第i天的雪堆,不妨假设其从一开始就存在,那么它的初始体积就为V[i]+T[1. ...

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

    A. Protect Sheep time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) F 构造

    http://codeforces.com/contest/967/problem/F 题目大意: 有n个点,n*(n-1)/2条边的无向图,其中有m条路目前开启(即能走),剩下的都是关闭状态 定义: ...

  6. Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) E 贪心

    http://codeforces.com/contest/967/problem/E 题目大意: 给你一个数组a,a的长度为n 定义:b(i) = a(1)^a(2)^......^a(i), 问, ...

  7. Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D 贪心

    http://codeforces.com/contest/967/problem/D 题目大意: 有n个服务器,标号为1~n,每个服务器有C[i]个资源.现在,有两个任务需要同时进行,令他为x1,x ...

  8. 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

    题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...

  9. 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio

    题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...

随机推荐

  1. 关于配置websocket,nginx转发https至wss问题

    在本地测试通过的socket,再放到现在的有nginx代理之后发现会报:failed: Error in connection establishment: net::ERR_NAME_NOT_RES ...

  2. Mysql插入语句.txt

    INSERT INTO 目标表 SELECT * FROM 来源表;比如要将 articles 表插入到 newArticles 表中,则是:INSERT INTO newArticles SELEC ...

  3. jenkins 新增节点的3种方式

    1.通过ssh建立节点(在节点机子上要安装好jdk) (1)通过用户+密码建立ssh连接 (2)通过用户+密钥建立连接 2.通过jnlp,javaweb的方式连接 (1)创建好节点 (2)在节点的机子 ...

  4. OOP 面向对象 七大原则 (一)

    OOP 面向对象   七大原则 (一) 大家众所周知,面向对象有三大特征继承封装多态的同时,还具有这七大原则,三大特征上一篇已经详细说明,这一篇就为大家详解一下七大原则: 单一职责原则,开闭原则,里氏 ...

  5. 2019年北航OO第四单元(UML任务)及学期总结

    第四单元两次作业总结 第十三次作业 需求分析 本次作业需要完成一个UML类图解析器,所需要解析的只有符合UML标准和能够在Java 8中复现的UML类图.查询指令存在两种:仅与所查对象有关的指令,以及 ...

  6. socket 客户端的认证

    一:使用 hashlib 进行加密验证: # server.py 服务端 import os import socket import hashlib ​ def check_conn(conn): ...

  7. sso 登录业务逻辑

  8. String类常见的方法

    类String public final class String extends Object implements Serializable, comparable<String>, ...

  9. nyoj 120 建边构强连通

    #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...

  10. Android慎用layout嵌套, 尽量控制在5层下面java.lang.StackOverflowError

    一.探寻原因 在一个复杂的layout嵌套较多layout的android界面.在Android 2.3.内存较低 的机型上,出现 java.lang.StackOverflowError 这个Exc ...