#include #include #include #include #define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++) using namespace std; typedef vector VI; /** * Segment Tree. This data structure is useful for fast folding on intervals of an array * whose elements are elements of monoid M. Note that constructing this tree requires the identity * element of M and the operation of M. * Header requirement: vector */ template class SegTree { int n; std::vector dat; BiOp op; I e; public: SegTree(int n_, BiOp op, I e) : op(op), e(e) { n = 1; while (n < n_) { n *= 2; } // n is a power of 2 dat.resize(2 * n); for (int i = 0; i < 2 * n - 1; i++) { dat[i] = e; } } /* ary[k] <- v */ void update(int k, I v) { k += n - 1; dat[k] = v; while (k > 0) { k = (k - 1) / 2; dat[k] = op(dat[2 * k + 1], dat[2 * k + 2]); } } void update_array(int k, int len, const I *vals) { for (int i = 0; i < len; ++i) { update(k + i, vals[i]); } } /* Updates all elements. O(n) */ void update_all(const I *vals, int len) { for (int k = 0; k < min(n, len); ++k) { dat[k + n - 1] = vals[k]; } for (int k = min(n, len); k < n; ++k) { dat[k + n - 1] = e; } for (int b = n / 2; b >= 1; b /= 2) { for (int k = 0; k < b; ++k) { dat[k + b - 1] = op(dat[k * 2 + b * 2 - 1], dat[k * 2 + b * 2]); } } } /* l,r are for simplicity */ I querySub(int a, int b, int k, int l, int r) const { // [a,b) and [l,r) intersects? if (r <= a || b <= l) return e; if (a <= l && r <= b) return dat[k]; I vl = querySub(a, b, 2 * k + 1, l, (l + r) / 2); I vr = querySub(a, b, 2 * k + 2, (l + r) / 2, r); return op(vl, vr); } /* [a, b] (note: inclusive) */ I query(int a, int b) const { return querySub(a, b + 1, 0, 0, n); } }; const int N = 5000001; int tbl[N]; int dset(int v) { int bits = 0; while (v > 0) { bits |= 1 << (v % 10); v /= 10; } return bits; } struct bor { int operator()(int a, int b) const { return a | b; } }; void solve(int bits) { tbl[0] = tbl[1] = 0; REP(i, 2, N) { tbl[i] = dset(i); } REP(i, 2, sqrt(N) + 1) { if (tbl[i]) { REP(j, 2, (N + i - 1) / i) { tbl[i * j] = 0; } } } SegTree st(N, bor(), 0); st.update_all(tbl, N); int ma = -1; REP(i, 1, N) { int acc = 0; int j = i; for (; j < N; ++j) { if (tbl[j] & ~bits) { break; } acc |= tbl[j]; if (acc == bits) { ma = max(ma, j - i); } } i = j; } cout << ma << endl; } int main(void){ int sn; cin >> sn; int bits = 0; REP(i, 0, sn) { int y; cin >> y; bits |= 1 << y; } solve(bits); }