#include #include using namespace std; int M, N; int D[4]; int T[100000], S[1000000], c[6]; int main() { ifstream fin("tsuk.in"); fin >> M; for (int i = 0; i < M; i++) fin >> D[i]; fin >> N; for (int i = 0; i < N; i++) fin >> T[i]; int n = 0, m = 0; ofstream fout("tsuk.out"); for (c[5] = 0; c[5] <= 9; c[5]++) { int t5 = 0; for (int i = 0; i < M; i++) { if (c[5] == D[i]) t5++; } for (c[4] = 0; c[4] <= 9; c[4]++) { int t4 = t5; for (int i = 0; i < M; i++) { if (c[4] == D[i]) t4++; } for (c[3] = 0; c[3] <= 9; c[3]++) { int t3 = t4; for (int i = 0; i < M; i++) { if (c[3] == D[i]) t3++; } for (c[2] = 0; c[2] <= 9; c[2]++) { int t2 = t3; for (int i = 0; i < M; i++) { if (c[2] == D[i]) t2++; } for (c[1] = 0; c[1] <= 9; c[1]++) { int t1 = t2; for (int i = 0; i < M; i++) { if (c[1] == D[i]) t1++; } for (c[0] = 0; c[0] <= 9; c[0]++, n++) { if (n == 0) continue; int t0 = t1; for (int i = 0; i < M; i++) { if (c[0] == D[i]) t0++; for (int nd = n; nd % D[i] == 0; nd /= D[i]) t0++; } S[n] = t0; if (m == N) { fout << t0; goto end; } else { if (S[n] == T[m]) { m++; } else { while (m > 0) { bool match = true; for (int j = 0; match && j < m; j++) { if (S[n - m + 1 + j] != T[j]) match = false; } if (match) break; else m--; } } } } } } } } } fout << 0; end: return 0; }