#SP0000282. Khôi phục nguyên tố từ tổng tiền tố (Sum of primes (reverse mode))
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 . For a prime , define as the sum of all primes from through , including .
You are given a value and are guaranteed that there is a prime such that . Find the last prime in that sum. The value may exceed the range of a signed 64-bit integer.
Input
The only line contains a positive integer , a prefix sum of the prime sequence.
Output
Print the unique prime satisfying .
Subtasks
- Subtask 1 (20 points): ; there exists a prime with and . Time limit: s.
- Subtask 2 (30 points): ; there exists a prime with and . Time limit: s.
- Subtask 3 (50 points): ; there exists a prime with and . Time limit: s.
Examples
Example 1
Input
77
Output
19
Explanation
The running prime sums are . The total is first reached when the prime is included; therefore print .
Example 2
Input
24739512092254535
Output
999999937
Explanation
The sum of all primes at most is . The largest such prime is , 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 , with sum . Thus the last prime is .