No.6 A Useless Hash
Problem Statement
Frank, who became interested in hashing, came up with the following hash algorithm.
Take the sum of the digits of a natural number; if that sum also has two or more digits, repeat the process.
That is,
hash(4)=4,
hash(17)=hash(1+7)=hash(8)=8,
hash(119)=hash(1+1+9)=hash(11)=hash(2)=2, and so on.
However, when he actually tried it, there were many collisions (cases where computed values coincide), and it was not very useful.
To Frank, who still could not give up, you said, "Calm down, count the prime numbers."
Growing desperate, Frank is now trying to apply the above hash algorithm to sequences of consecutive primes among the natural numbers \([K,N]\) (meaning the range \(K \leq i \leq N\)) and to find the largest range in which no collisions occur (no values coincide).
Feeling sorry for having said that, you have decided to find it on Frank's behalf.
When the above hash algorithm is applied to sequences of consecutive primes contained in the range \([K,N]\) (the range \(K \leq i \leq N\)),
find the prime sequence of maximum length whose values are all distinct, and find the first prime of that original prime sequence.
(If there are multiple prime sequences of the same length, choose the one with the larger numbers.)
Input
\(K\) \(N\)
\( 1 \leq K \leq N \)
\( 2 \leq N \leq 200000 \)
It is guaranteed that the range $[K,N]$ contains a prime.
Output
Output the number as a string.
Print a newline at the end.
Sample
Sample 1
Input
1 11
Output
3
First, the sequence of primes contained in \([1,11]\) is \( 2,3,5,7,11 \).
Applying Frank's hash algorithm gives \( 2,3,5,7,2 \).
Extracting the prime sequences of maximum length with no collisions (no coinciding values) gives \( 2,3,5,7 \) and \( 3,5,7,11 \).
Since the one with the larger numbers is chosen, the first element of the prime sequence is \(3\).
Sample 2
Input
10 100
Output
31
Considering the prime sequence of \([10,100]\), the following has the greatest length.
\( 31, 37, 41, 43, 47, 53 \rightarrow 4,1,5,7,2,8 \)
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。