#include #include #include using namespace std; const int MAX_PRIME = 200100; bool isPrime[MAX_PRIME + 1]; vector primes; int main() { fill(&isPrime[0], &isPrime[MAX_PRIME] + 1, true); isPrime[0] = false; isPrime[1] = false; for (int i = 2; i <= MAX_PRIME; ++i) { if (isPrime[i]) { for (int k = i * 2; k <= MAX_PRIME; k += i) { isPrime[k] = false; } } } int k, n; cin >> k >> n; for (int i = k; i <= n; ++i) { if (isPrime[i]) { primes.push_back(i); } } vector hashes; for (size_t i = 0; i < primes.size(); ++i) { int x = primes[i]; int hash; do { hash = 0; for (; x > 0; x /= 10) { hash += x % 10; } x = hash; } while (hash >= 10); hashes.push_back(hash); } int positions[10]; fill(&positions[0], &positions[9] + 1, -1); int maxLength = 0; int maxFirst = 0; int first = 0; for (size_t last = 0; last < hashes.size(); ++last) { int h = hashes[last]; if (positions[h] >= first) { first = positions[h] + 1; } positions[h] = last; int length = last - first + 1; if (maxLength <= length) { maxLength = length; maxFirst = first; } } cout << primes[maxFirst] << endl; return 0; }