Primal Sport ( 质数+思维 )

A. Primal Sport
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

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
Copy
14
output
6
input
Copy
20
output
15
input
Copy
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.


思路:

设prime[i]为i的最大质数因子,那么x1的范围为[ x2-prime[x2]+1 , x2 ],x0 = x1 - prime[x1] +1;


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 1e6 + 10;
const int INF = 1e9 + 7;

int prime[MAX];

int main()
{
	int x2;
	scanf("%d", &x2);
	for (int i = 2; i <= x2; i++)
	{
		if (!prime[i])
		{
			for (int j = i * 2; j <= x2; j += i)
				prime[j] = i;
		}
	}
	int ans = INF;
	for (int i = x2 - prime[x2] + 1; i <= x2; i++)
	{
		//x1不能为质数
		if (prime[i] == 0)
			continue;
		ans = min(ans, i - prime[i] + 1);
	}
	printf("%d\n", ans);
	return 0;
}

样例:

→Judgement Protocol
Test: #1, time: 15 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
14
Output
6
Answer
6
Checker Log
ok 1 number(s): "6"
Test: #2, time: 0 ms., memory: 7172 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
20
Output
15
Answer
15
Checker Log
ok 1 number(s): "15"
Test: #3, time: 0 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
8192
Output
8191
Answer
8191
Checker Log
ok 1 number(s): "8191"
Test: #4, time: 30 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1000000
Output
998677
Answer
998677
Checker Log
ok 1 number(s): "998677"
Test: #5, time: 15 ms., memory: 7172 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
959806
Output
239958
Answer
239958
Checker Log
ok 1 number(s): "239958"
Test: #6, time: 15 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
1452
Output
1206
Answer
1206
Checker Log
ok 1 number(s): "1206"
Test: #7, time: 15 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
4
Output
3
Answer
3
Checker Log
ok 1 number(s): "3"
Test: #8, time: 15 ms., memory: 7176 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
6
Output
3
Answer
3
Checker Log
ok 1 number(s): "3"
Test: #9, time: 15 ms., memory: 7176 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
8
Output
7
Answer
7
Checker Log
ok 1 number(s): "7"
Test: #10, time: 15 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
9
Output
7
Answer
7
Checker Log
ok 1 number(s): "7"
Test: #11, time: 0 ms., memory: 7168 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
10
Output
4
Answer
4
Checker Log
ok 1 number(s): "4"
Test: #12, time: 15 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
12
Output
6
Answer
6
Checker Log
ok 1 number(s): "6"
Test: #13, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
15
Output
8
Answer
8
Checker Log
ok 1 number(s): "8"
Test: #14, time: 15 ms., memory: 7172 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
16
Output
11
Answer
11
Checker Log
ok 1 number(s): "11"
Test: #15, time: 15 ms., memory: 7188 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
110880
Output
55440
Answer
55440
Checker Log
ok 1 number(s): "55440"
Test: #16, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
166320
Output
110879
Answer
110879
Checker Log
ok 1 number(s): "110879"
Test: #17, time: 15 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
221760
Output
110880
Answer
110880
Checker Log
ok 1 number(s): "110880"
Test: #18, time: 0 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
277200
Output
138600
Answer
138600
Checker Log
ok 1 number(s): "138600"
Test: #19, time: 0 ms., memory: 7172 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
332640
Output
166320
Answer
166320
Checker Log
ok 1 number(s): "166320"
Test: #20, time: 15 ms., memory: 7176 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
498960
Output
332639
Answer
332639
Checker Log
ok 1 number(s): "332639"
Test: #21, time: 0 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
554400
Output
415798
Answer
415798
Checker Log
ok 1 number(s): "415798"
Test: #22, time: 30 ms., memory: 7176 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
665280
Output
498958
Answer
498958
Checker Log
ok 1 number(s): "498958"
Test: #23, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
720720
Output
540538
Answer
540538
Checker Log
ok 1 number(s): "540538"
Test: #24, time: 30 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
510510
Output
255248
Answer
255248
Checker Log
ok 1 number(s): "255248"
Test: #25, time: 15 ms., memory: 7188 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
570570
Output
285282
Answer
285282
Checker Log
ok 1 number(s): "285282"
Test: #26, time: 15 ms., memory: 7176 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
690690
Output
460455
Answer
460455
Checker Log
ok 1 number(s): "460455"
Test: #27, time: 15 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
959818
Output
239958
Answer
239958
Checker Log
ok 1 number(s): "239958"
Test: #28, time: 31 ms., memory: 7184 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
959878
Output
239978
Answer
239978
Checker Log
ok 1 number(s): "239978"
Test: #29, time: 15 ms., memory: 7176 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
959902
Output
239978
Answer
239978
Checker Log
ok 1 number(s): "239978"
Test: #30, time: 31 ms., memory: 7188 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
974847
Output
324954
Answer
324954
Checker Log
ok 1 number(s): "324954"
Test: #31, time: 15 ms., memory: 7160 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
974859
Output
324978
Answer
324978
Checker Log
ok 1 number(s): "324978"
Test: #32, time: 30 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
974931
Output
324980
Answer
324980
Checker Log
ok 1 number(s): "324980"
Test: #33, time: 15 ms., memory: 7172 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
885481
Output
442272
Answer
442272
Checker Log
ok 1 number(s): "442272"
Test: #34, time: 15 ms., memory: 7172 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
896809
Output
447944
Answer
447944
Checker Log
ok 1 number(s): "447944"
Test: #35, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
908209
Output
453632
Answer
453632
Checker Log
ok 1 number(s): "453632"
Test: #36, time: 15 ms., memory: 7188 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
935089
Output
467064
Answer
467064
Checker Log
ok 1 number(s): "467064"
Test: #37, time: 15 ms., memory: 7188 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
720721
Output
355298
Answer
355298
Checker Log
ok 1 number(s): "355298"
Test: #38, time: 31 ms., memory: 7176 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
690691
Output
342864
Answer
342864
Checker Log
ok 1 number(s): "342864"
Test: #39, time: 15 ms., memory: 7188 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
959903
Output
479702
Answer
479702
Checker Log
ok 1 number(s): "479702"
Test: #40, time: 30 ms., memory: 7176 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
974932
Output
470060
Answer
470060
Checker Log
ok 1 number(s): "470060"
Test: #41, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
935090
Output
463950
Answer
463950
Checker Log
ok 1 number(s): "463950"
Test: #42, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
524288
Output
524287
Answer
524287
Checker Log
ok 1 number(s): "524287"
Test: #43, time: 0 ms., memory: 7188 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
524289
Output
174768
Answer
174768
Checker Log
ok 1 number(s): "174768"
Test: #44, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
524286
Output
262110
Answer
262110
Checker Log
ok 1 number(s): "262110"
Test: #45, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
531441
Output
526737
Answer
526737
Checker Log
ok 1 number(s): "526737"
Test: #46, time: 0 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
531442
Output
262490
Answer
262490
Checker Log
ok 1 number(s): "262490"
Test: #47, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
531440
Output
265704
Answer
265704
Checker Log
ok 1 number(s): "265704"
Test: #48, time: 15 ms., memory: 7188 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
81
Output
76
Answer
76
Checker Log
ok 1 number(s): "76"
Test: #49, time: 15 ms., memory: 7180 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
999958
Output
250008
Answer
250008
Checker Log
ok 1 number(s): "250008"
Test: #50, time: 15 ms., memory: 7160 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
2048
Output
1959
Answer
1959
Checker Log
ok 1 number(s): "1959"
close




A. Primal Sport
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

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
Copy
14
output
6
input
Copy
20
output
15
input
Copy
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.


分析:设 f(n) 表示 N最大的质数因子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值