No.12 Limited Primes
タグ : / 解いたユーザー数 243
作問者 :
nmnmnmnmnmnmnm
Problem Statement
\(N\) distinct digits from '\(0\)' to '\(9\)' are given.
Next, from the range between \(1\) and \(5000000\), inclusive, choose a range from \(K\) to \(L\), inclusive.
Extract all the primes, and only the primes, from the range from \(K\) to \(L\).
Examine the digits used across all of these primes.
For example, if the range from \(10\) to \(20\) is chosen as the range from \(K\) to \(L\),
the primes are the \(4\) numbers "\(11\)", "\(13\)", "\(17\)", and "\(19\)".
The digits used are the \(4\) digits '\(1\)', '\(3\)', '\(7\)', and '\(9\)'.
We want these used digits to be equal to the digits given at the beginning.
(All of them must be used.)
Find the maximum value of the difference \(L-K\) between \(K\) and \(L\) in this situation.
If no such case exists, the answer is \(-1\).
The following are invalid examples.
Suppose the given digits are \([3,5,7]\).
The primes in the range from \(4\) to \(7\) are \(5,7\), which do not include \(3\), so this is an invalid range.
The primes in the range from \(2\) to \(10\) are \(2,3,5,7\), which include an extra \(2\), so this is an invalid range.
In this case, ranges such as "from \(3\) to \(7\)" and "from \(3\) to \(10\)" are valid.
Input
\(N\) \(A_1\ \dots\ A_i\ \dots\ A_N\)
Line \(1\) contains the number of given digits \(N\ (1 \leq N \leq 10)\).
Line \(2\) contains the \(N\) given digits \(A_i\ (0 \leq A_i \leq 9)\), separated by single-byte spaces.
If $i \neq j$, then $A_i \neq A_j$.
Output
Find the maximum value of the difference \(L-K\) between \(K\) and \(L\).
If nothing satisfies the conditions, the answer is \(-1\).
Print a newline at the end.
Sample
Sample 1
Input
1 5
Output
2
Extracting all the primes in the range from \(4\) to \(6\) gives only '\(5\)',
so the given digits match the digits used in the primes.
In this case, \(6-4=2\) is the maximum value of \(L-K\).
(Other than \(5\), there is no prime consisting only of \(5\).)
Sample 2
Input
5 1 2 3 4 5
Output
158
When \(K=2354300\) and \(L=2354458\),
the primes are
the \(4\) numbers "\(2354311\)", "\(2354351\)", "\(2354353\)", and "\(2354413\)".
These use exactly the digits '\(1\)', '\(2\)', '\(3\)', '\(4\)', and '\(5\)'.
There are other cases satisfying the condition, but
\(2354458-2354300=158\) in this case is the maximum value.
Sample 3
Input
2 0 3
Output
-1
No range can be chosen whose primes use \(0\) and \(3\), so print \(-1\).
Sample 4
Input
10 0 1 2 3 4 5 6 7 8 9
Output
4999999
It is possible to choose the entire range from \(1\) to \(5000000\).
Sample 5
Input
2 1 3
Output
94
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。