#include #include #include #include #include #include #include using namespace std; vector primes(int n) { vector table(n + 1, true); table[0] = table[1] = false; int mid = (int)floor(sqrt(n)); for (int i = 2; i <= mid; i++) { if (table[i]) { for (int j = i+i; j <= n; j += i) table[j] = false; } } vector ans; for (int i = 2; i <= n; i++) if (table[i]) ans.push_back(i); return ans; } int sum_digit(int n) { int i = n; int acc = 0; for (;;) { int m = i % 10; int d = i / 10; acc += m; if (d == 0) { if (acc < 10) { break; } else { i = acc; acc = 0; } } else { i = d; } } return acc; } int main() { int K, N; cin >> K >> N; vector ps = primes(N); auto it = remove_if(ps.begin(), ps.end(), [K](int x) {return x < K; }); if (it != ps.end()) ps.erase(it); vector ss(ps.size()); transform(ps.begin(), ps.end(), ss.begin(), sum_digit); int len = ps.size(); int ans = 0; int ans_len = 0; for (int i = 0; i < len; i++) { set ks; ks.insert(ss[i]); int tmp = ps[i]; int tmp_len = 1; for (int j = i+1; j < len; j++) { if (ks.find(ss[j]) != ks.end()) { if (tmp_len <= (int)ks.size()) { // tmp = ps[i]; tmp_len = ks.size(); } break; } else { ks.insert(ss[j]); } } tmp_len = ks.size(); if (ans_len <= tmp_len) { ans_len = tmp_len; ans = tmp; } } cout << ans << endl; return 0; }