#include #include #include #include #include #define getBit(x,b) (x)&(1<<(b)) #define setBit(x,b) (x)|(1<<(b)) #define delBit(x,b) (x)&~(1<<(b)) #define N_MAX 200000 using namespace std; int calcHash(int n) { int ans = 0; while (n > 0) { ans += n % 10; n /= 10; } if (ans >= 10) ans = calcHash(ans); return ans; } vector* calcPrime(int K, int N) { auto list = new bool[N + 1]; auto prime = new vector(); int sn = (int)sqrt(N); list[0] = list[1] = false; for (int i = 2; i <= N; ++i) list[i] = true; for (int i = 2; i <= sn; ++i) { if (list[i]) { int jmax = N / i; for (int j = i; j <= jmax; ++j) list[i*j] = false; } } for (int i = K; i <= N; ++i) { if (list[i]) prime->push_back(i); } delete[] list; return prime; } int main() { int K, N, NumPrime, BitChain = 0, max = 0, ans; cin >> K >> N; int pos[10], index = 0; auto prime = calcPrime(K, N); NumPrime = prime->size(); ans = (*prime)[0]; for (int i = 0; i < 10; ++i) { pos[i] = -1; } for (auto it = prime->rbegin(); it != prime->rend(); ++it) { int h = calcHash(*it); if (pos[h] != -1) { int count = 0; for (int i = 0; i < 10; ++i) { if (pos[i] != -1) ++count; if (pos[i] < pos[h]) { pos[i] = -1; } } if (count > max) { max = count; ans = *(it - 1); } if (count == 10) break; } pos[h] = index; ++index; } cout << ans << endl; delete prime; return 0; }