PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#SP0000064. Sàng Sundaram (Sieve of Sundaram)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Sàng Sundaram (Sieve of Sundaram)
Sieve of Sundaram
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given integer , list all primes not greater than in increasing order. The original problem requires the Sieve of Sundaram: consider indices and remove every index expressible as with ; each remaining index produces an odd prime . Handle prime separately.
Input
One line contains integer .
Output
Print all primes not greater than on one line, in ascending order, separated by a single space.
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- Subtask 3 (50 points): .
Examples
Input
7
Output
2 3 5 7
Explanation
For , consider indices . The smallest removal index is , beyond the limit ; none is removed. Add prime separately and convert into . Print 2 3 5 7.