// yukicoder: No.12 限定された素数 // 2019.4.12 bal4u #include #include #define MAX 5000000 #define SQRT 2236 char notPrime[MAX] = { 1,1,0,0,1 }; // zero: if prime int ptbl[350000] = { 2, 3, 5, 7 }; #define LIM 348513 int pbit[350000] = { 4, 8, 32, 128 }; int bit[10]; int getBit(int k) { int res = 0; while (k) res |= bit[k % 10], k /= 10; return res; } void sieve() { int i, j; for (i = 3; i <= SQRT; i += 2) if (!notPrime[i]) { for (j = i * i; j < MAX; j += i) notPrime[j] = 1; } j = 4; for (i = 9; i <= MAX; i += 2) if (!notPrime[i]) { ptbl[j] = i, pbit[j++] = getBit(i); } } int main() { int i, N, fr, a, mask, ans; for (i = 0; i < 10; i++) bit[i] = 1 << i; sieve(); scanf("%d", &N); if (N == 10) { puts("4999999"); return 0; } mask = 0; for (i = 0; i < N; i++) { scanf("%d", &a), mask |= bit[a]; } ans = -1, fr = 0, a = 0; for (i = 0; i < LIM; i++) { if ((mask | pbit[i]) == mask) a |= pbit[i]; else { if (a == mask && ptbl[i] - fr - 2 > ans) ans = ptbl[i] - fr - 2; fr = ptbl[i], a = 0; } } printf("%d\n", ans); return 0; }