#include #include std::vector primes(int n) { std::vector is_prime(n + 1, true); is_prime.at(0) = false; is_prime.at(1) = false; std::vector res; for (int i = 2; i < n + 1; i++) { if (is_prime.at(i)) { res.push_back(i); for (int j = i * 2; j < n + 1; j += i) { is_prime.at(j) = false; } } } return res; } int calc_hash(int p) { int hash = p; while (hash >= 10) { int new_hash = 0; while (hash != 0) { new_hash += hash % 10; hash /= 10; } hash = new_hash; } return hash; } int main() { int K, N; std::cin >> K; std::cin >> N; std::vector primes_candidate; for (auto &p: primes(N)) { if (p < K) { continue; } primes_candidate.push_back(p); } std::vector exist_indices(10, -1); int ans; int l = 0, M = 0; for (int r = 0; r < primes_candidate.size(); r++) { int hash = calc_hash(primes_candidate.at(r)); if (exist_indices.at(hash) != -1 && l < exist_indices.at(hash) + 1) { l = exist_indices.at(hash) + 1; } exist_indices.at(hash) = r; if (M <= r - l) { ans = primes_candidate.at(l); M = r - l; } } std::cout << ans << std::endl; }