PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000282. Khôi phục nguyên tố từ tổng tiền tố (Sum of primes (reverse mode))

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Khôi phục nguyên tố từ tổng tiền tố (Sum of primes (reverse mode))

Sum of primes (reverse mode)

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Consider the increasing sequence of primes 2,3,5,7,11,…2,3,5,7,11,\ldots. For a prime PP, define S(P)S(P) as the sum of all primes from 22 through PP, including PP.

You are given a value SS and are guaranteed that there is a prime PP such that S(P)=SS(P)=S. Find the last prime PP in that sum. The value SS may exceed the range of a signed 64-bit integer.

Input

The only line contains a positive integer SS, a prefix sum of the prime sequence.

Output

Print the unique prime PP satisfying S(P)=SS(P)=S.

Subtasks

  • Subtask 1 (20 points): 2≤S≤57363962\le S\le 5736396; there exists a prime PP with 2≤P≤100002\le P\le 10000 and S=∑p≤P, p primepS=\sum_{p\le P,\ p\text{ prime}}p. Time limit: 55 s.
  • Subtask 2 (30 points): 2≤S≤247395120922545352\le S\le 24739512092254535; there exists a prime PP with 2≤P≤10000000002\le P\le 1000000000 and S=∑p≤P, p primepS=\sum_{p\le P,\ p\text{ prime}}p. Time limit: 1010 s.
  • Subtask 3 (50 points): 2≤S≤184355885525507059113772\le S\le 18435588552550705911377; there exists a prime PP with 2≤P≤10000000000002\le P\le 1000000000000 and S=∑p≤P, p primepS=\sum_{p\le P,\ p\text{ prime}}p. Time limit: 2020 s.

Examples

Example 1

Input

77

Output

19

Explanation

The running prime sums are 2,5,10,17,28,41,58,772,5,10,17,28,41,58,77. The total 7777 is first reached when the prime 1919 is included; therefore print 1919.

Example 2

Input

24739512092254535

Output

999999937

Explanation

The sum of all primes at most 10910^9 is 24 739 512 092 254 53524\,739\,512\,092\,254\,535. The largest such prime is 999 999 937999\,999\,937, so it is the final term and the required answer.

Example 3

Input

2

Output

2

Explanation

The first prefix consists only of the prime 22, with sum 22. Thus the last prime is 22.