VK Cup 2018 - Round 1 A. Primal Sport
1.5 seconds
256 megabytes
standard input
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.
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 a single integer — the minimum possible X0.
14
6
20
15
8192
8191
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.
可能英语不行啊,“announces the smallest multiple of this prime number that is not smaller than the current number.”这句话看成素数的最小倍数不小于该数。
但经过大佬的提点=》tzuhsiao
题意:我们只看x0,x1,x2这三个变量,题目给你x2的值,然后你找一个素数p1,要求p1<x1,而且x2是p1的倍数,这个x2是接近x1但大于x1的最小数(上面的英文也是这个意思)
简单来讲就是,对于当前的x,选取一个质数p,p<当前值,然后让这个质数的最小倍数大于等于当前值。(等于的时候说明是这个质数本身是当前数的质因数的情况)
思路:
对于每个合数而言,都可表示成若干个质数的乘积。所以当知道了x2的值以后,x1的取值范围为[x2-P(x2)=1,x2],(其中P(x2)表示x2的最大质因数)。
同理,x0的取值范围是[x1-P(x1)+1,x1]。从这个取值范围的表达式可以看出,取得素数的值越大,上一个数字的最小值就会越小。所以这里取得是最大质因数。
这样的话就可以先使用质数筛预处理,然后按这个思路找最小值。
#include <iostream>
using namespace std;
const int maxn=1000006;
int prime[maxn];
void getprime()
{
int n=2;
while(n<maxn)
{
int num=2*n;
while(num<maxn)
{
prime[num]=n;
num+=n;//该数的倍数也更新一次
}
num=n+1;
while(prime[num]!=0&&num<maxn)
{
num++;//略过那些没有质因数的情况
}
n=num;
}
}
int main()
{
getprime();
//int n;
//cin>>n;
int x0,x1,x2;
int a,b;
cin>>x2;//因为取得质因数越大,则他的下一个数的最小值越小
b=prime[x2];
x0=999999;
int i;
for(i=x2-b+1;i<=x2;i++)
{
a=prime[i];
x0=min(x0,i-a+1);
}
cout<<x0<<endl;
return 0;
}
VK Cup 2018 - Round 1 A. Primal Sport的更多相关文章
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport
Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try ...
- VK Cup 2018 - Round 1+Codeforces Round #470
A. Primal Sport 题意:有两个人轮流玩游戏.给出数X(i-1),轮到的人需要找到一个小于X(i-1)的素数x,然后得到Xi,Xi是x的倍数中大于等于X(i-1)的最小的数.现在已知X2, ...
- 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 ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1) C.Producing Snow
题目链接 题意 每天有体积为Vi的一堆雪,所有存在的雪每天都会融化Ti体积,求出每天具体融化的雪的体积数. 分析 对于第i天的雪堆,不妨假设其从一开始就存在,那么它的初始体积就为V[i]+T[1. ...
- 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条路目前开启(即能走),剩下的都是关闭状态 定义: ...
- 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), 问, ...
- 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 ...
- 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution
题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...
- 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio
题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...
随机推荐
- lemon oa前端页面——由user-base-list谈项目组织
content user-base-list.jsp中指定<%pageContext.setAttribute("currentHeader", "user&quo ...
- linux文件描述符fd(windows下的句柄)
在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件.链接文件和设备文件 fd:file descriptor 文件描述符0,1,2分别给了标准输入.标准输出和错误输出. ls - ...
- .NET 之 ORM 性能评测
.NET 之 ORM 性能评测 Why 你应该总能听到某ORM性能比Dapper高 你应该有如下疑问: 基准测试是否权威 基准测试的方式是否合理 基准测试的标准是否能够统一 统一基准测试标准/规范 如 ...
- 相机标定(Camera calibration)
简单介绍 摄像机标定(Camera calibration)简单来说是从世界坐标系换到图像坐标系的过程.也就是求终于的投影矩阵 P 的过程,以下相关的部分主要參考UIUC的计算机视觉的课件(网址Spr ...
- 杭电1232畅通project
畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则
ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...
- Android——SlidingMenu学习总结
来源 SlidingMenu是github上比較火开源库.很强大,不但但是简单的设置实现两側滑动菜单,还能够设置菜单的阴影.渐变色.划动模式等. 下载地址:https://github.com/jfe ...
- 实战c++中的string系列--指定浮点数有效数字并转为string
上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...
- 如何离线分析Kafka海量业务消息?1分钟快速为您支招
场景介绍 说起Kafka,许多使用者对它是又爱又恨.Kafka是一种分布式的.基于发布/订阅的消息系统,其极致体验让人欲罢不能,但操心的运维.复杂的安全策略.可靠性易用性的缺失等,仍需要使用者付出诸多 ...
- linux-shell脚本命令之awk
[ awk简单介绍: ] awk能够从一个文本中获取部分内容, 或者对这个文本进行排版, 使它按某种格式输出. [ awk工作流程: ] awk会把文件一行内容去到内存里, 然后对这行内容进行分段 ( ...