問題一覧 > 通常問題

No.12 Limited Primes

レベル : / AI推定難易度 : 1000 / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 243
作問者 : nmnmnmnmnmnmnm
お気に入りにしたユーザー ProblemId : 34 / 自分の提出
問題文最終更新日: 2020-03-19 16:10:29
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 12 (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。