#SP0000005. Số nguyên tố lớn nhất trong dãy (Largest Prime in an Array)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Số nguyên tố lớn nhất trong dãy (Largest Prime in an Array)
Largest Prime in an Array
Source: LQDOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of integers . A prime is an integer greater than having exactly two positive divisors, and itself. For example, , , , and are prime, while , , , , and are not.
Among all prime-valued entries in the array, find the largest value and list every position at which it occurs, in left-to-right order. Array positions are numbered from to . Print original array positions, not ranks among prime entries.
Phuoc Hung OJ Extended clarification: If the array contains no prime, the answer is -1. The original statement does not specify this case; this convention is added so that every valid input has a well-defined output.
Input
- The first line contains a positive integer , the array length.
- The second line contains exactly space-separated integers in array order.
Output
If at least one array entry is prime:
- On the first line, output the largest prime value in the array.
- On the second line, output all positions satisfying equals that largest prime, in ascending order, separated by single spaces.
If there is no prime entry, output only -1 on one line.
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Example 1
Input
9
19 7 81 33 17 4 19 21 13
Output
19
1 7
Explanation
The array contains nine entries. Reading them from left to right:
- is prime, so we record value at position .
- is prime but smaller than .
- and are composite.
- is prime but smaller than .
- is composite.
- is prime and equals the current largest prime value.
- is composite. is prime, but smaller than .
The maximum prime value is therefore , which occurs exactly at positions and . Those values form the two output lines.
Example 2
Input
6
-7 0 1 4 9 100
Output
-1
Explanation
The values , , and are not prime. The remaining values are composite: , , and . The array has no prime, so the added convention requires printing -1.