#SP0000257. Bài học về sàng số nguyên tố đầu tiên (The First Lesson on the Sieve of Eratosthenes)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bài học về sàng số nguyên tố đầu tiên (The First Lesson on the Sieve of Eratosthenes)
The First Lesson on the Sieve of Eratosthenes
Source: VNOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Consider the following sieve procedure, given an integer and an array containing integers between and :
- Initially mark every integer as not removed, and start with an empty output list.
- Process values in their exact order in . If has already been removed, skip it.
- Otherwise append to the output list, then mark all multiples strictly greater than , namely , as removed.
You are given a permutation of the integers . In an arbitrary order, this sieve may append composite numbers.
You may perform the following operation any number of times: swap two adjacent elements, provided exactly one is prime and the other is composite. Swapping two primes or two composites is forbidden.
Find the minimum number of swaps needed so that running the sieve on the resulting array appends only primes. A valid sequence of swaps is guaranteed to exist.
Input
The first line contains the sieve limit .
The second line contains exactly integers , a permutation of .
Output
Print a single 64-bit integer: the minimum number of allowed adjacent swaps so that the sieve output contains no composite number.
Subtasks
- Subtask 1 (50 points): ; for every ; all are pairwise distinct.
- Subtask 2 (50 points): ; for every ; all are pairwise distinct.
Examples
Input
8
7 6 2 3 4 5 8
Output
1
Explanation
For , the original permutation is .
If the sieve is run immediately, is appended, followed by composite because none of its prime divisors appeared earlier. Thus zero swaps cannot be sufficient.
Swap the adjacent composite and prime once, obtaining . When is processed, it removes ; thus is skipped. The resulting sieve output is , containing primes only.
At least one swap is necessary and one is sufficient, so the answer is .