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.
这个题读了半个多小时才搞懂...很难受...
看懂过后想明白了为什么也不是很难。
题目讲的是两个人玩♂游戏,先给个x0,第一个人从小于x0的数字里面选一个素数,让这个素数乘以一个整数,使其刚好大于x0(意思就是选的要乘的这个数最小,并且乘出来要大于x0)。乘得的结果为x1,然后继续上面的步骤,从小于x1的数里面选个素数,乘出来刚好大于x1,乘积为x2.
现在题目就是告诉了你x2,叫你求x0的最小值。
解题思想是要倒推。从x2入手。令第二个人选的素数为p2,如果p2要乘以一个数刚好大于x1,那么就说明x1刚好在p2的某两个相邻倍数乘积之间。化成区间那么就是x1在[x2-p2+1,x2]之间。同理可求得x0在[x1-p1+1,x1]之间(p1为第一个人选的素数)。为了从范围可以看出,自然是选的素数越大范围越大,结果就更精确。(选的素数要是x的因子)
素数的选择就用素数筛选法,不过要注意点是在边筛选的时候要边标记出每个数的最大素因子,具体见代码。
最后在范围里遍历x1的值,取最小x0就行了
代码:
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<stack>
using namespace std;
typedef long long int LL;
int num[1000010];
int main()
{
int x2;
scanf("%d", &x2);
for (int i = 2; i < x2; i++)
{
if (num[i] == 0)
{
for (int j = i + i; j <= x2; j += i)
num[j] = i;
}
}
int minn = 1e7;
for (int i = x2 - num[x2] + 1; i <= x2; i++)
minn = min(minn, i - num[i] + 1);
printf("%d\n", minn);
//system("pause");
return 0;
}
x2 = int(input())
num = []
for val in range(1000010):
num.append(0)
for i in range(2,x2):
if(num[i]==0):
for j in range(i+i,x2+1,i):
num[j]=i
prim = x2 - num[x2] + 1
minn = 10000000
for i in range(prim,x2+1):
minn = min(minn,i - num[i] + 1)
print(minn)
还是python写着舒服啊。。。不过时间可能会。。。