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);
} }

最新文章

  1. 个人对B/S项目的一些理解(三)--Servlet与Strust
  2. 转:纠结的Shim
  3. Oracle 监听器无法启动(TNS-12537,TNS-12560,TNS-00507)
  4. MVC-Model数据注解(三)-Remote验证的一个注意事项
  5. Linux主机安全配置的几个脚本【转载】
  6. 关于字符编码精简介绍 ANSI GB2312 UTF8 UNICODE
  7. SQL日期格式转换(经常用又经常忘记的东西)转载自http://www.cnblogs.com/wangyuelang0526/archive/2012/06/06/2538224.html
  8. Excel VLOOKUP等使用记录
  9. 快速构建Windows 8风格应用5-ListView数据控件
  10. cpanel导入大数据库(mysql)的方法
  11. html页面顶部出现一段空白,检查控制台发现body 下出现&amp;#65279字符,原因及解决办法
  12. 一种解决eclipse中安装maven出错的方法
  13. 阿里云HBase携X-Pack再进化,重新赋能轻量级大数据平台
  14. javascript---lat const var 的区别
  15. TF版本的Word2Vec和余弦相似度的计算
  16. [UVA-11100] The Trip
  17. UVa 536 Tree Recovery(二叉树后序遍历)
  18. mybatis拦截器实现分页功能的示例讲解
  19. CMake Error: Curses library not found. Please install appropriate package
  20. UVA10212 【The Last Non-zero Digit.】

热门文章

  1. 何为DOM对象
  2. javascript事件列表解说
  3. POJ 2377 Bad Cowtractors( 最小生成树转化 )
  4. 编译htop
  5. Linux设备驱动--块设备(三)之程序设计(转)
  6. Python 从入门到实践 - Web应用程序
  7. c#设置button透明
  8. 2015 Multi-University Training Contest 8 hdu 5385 The path
  9. HDU 3567 Eight II
  10. HDU 4133