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

转载于:https://www.cnblogs.com/HazelNut/p/8612940.html

Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport相关推荐

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

    点击打开A题链接 #include<bits/stdc++.h> using namespace std; const int MAX = 105; int n,s;int h[MAX], ...

  2. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)C. Voltage Keepsake

    题目链接:C. Voltage Keepsake 题解:二分时间,然后判断看需要充电的时间总和是否在允许的范围内 #include<bits/stdc++.h> #define pb pu ...

  3. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)切题报告(A-B题)

    这是我第二次参加CF比赛,也是第一次写博客,写得不好,望各位大佬海涵. A. Single Wildcard Pattern Matching 原题链接 这道题,这是难到让人无法直视,太水了. 题目大 ...

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

    A. Doggo Recoloring ps:注意 n == 1 B. Weakened Common Divisor 题解:WCD出现中的数必然是 < a, b >中某个数的公约数.而 ...

  5. Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))

    Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) 题号 题目 知识点 A Simply Strange Sor ...

  6. Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) A-F全题解

    Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) 文章目录 A. Simply Strange Sort B. ...

  7. CF#504,#505(based on VK Cup 2018 Final)上红记

    去年的我这时还是蓝名小哥,然后勉强上了紫. 当时写过一篇上紫记. 我 打cf频率在friends里还是偏高的,大概就是经常打出饱和状态. 1900- 徘徊过几场,2100±徘徊了好几个月,从noip2 ...

  8. Codeforces Round #470 (rated, Div. 2 C. Producing Snow(思维)

    C. Producing Snow time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) C. Restoring

    C. Restoring Permutation time limit per test1 second memory limit per test256 megabytes inputstandar ...

最新文章

  1. php 判断类存在,PHP怎么判断类是否存在
  2. 高性能交易系统设计原理
  3. 单片机小白学步系列(十三) 点亮第一个LED——好的開始,成功的一半
  4. Python库安装相关问题
  5. 栈的基础概念与经典题目(Leetcode题解-Python语言)
  6. 函数式编程让你忘记设计模式
  7. 用LuaBridge为Lua绑定C/C++对象
  8. 每日源码分析 - Lodash(remove.js)
  9. 2018-04-22jenkins+maven+svn环境搭建学习心得
  10. 在信号处理函数中调用longjmp
  11. matlab数字图像处理-找不同
  12. heic图片格式转换jpg_如何在Mac上通过简单方法将HEIC图像转换为JPG
  13. 端口已被占用1080
  14. 什么是等级保护, 等保2.0详解
  15. 微信上传临时素材|微信公众号发送图片
  16. Microsoft Office 2013 word MathType 报错 (MathPage.wll)
  17. 黑名单将公开 我国建立行贿犯罪档案查询系统
  18. 阿里云购买云服务器流程及注意事项(新用户必看图文教程)
  19. 笔记本怎么编html,笔记本win7深度技术旗舰版使用记事本编辑和运行html代码的方法...
  20. 高中数学40分怎么办_高二了数学40多分还有救吗?

热门文章

  1. 无监督学习和监督学习的区别
  2. Facebook有1万名员工在研发AR/VR设备 占员工总数近1/5
  3. Node.js 极简笔记
  4. mysql插入timeStamp类型数据时间相差8小时的解决办法
  5. 十分钟入门RocketMQ
  6. Linux内核链表的移植与使用
  7. pstack命令学习
  8. String中intern的方法
  9. 流行学习简单入门与理解
  10. Intellij Idea 多模块Maven工程中模块之间无法相互引用问题