#include #define HASH 1000003 const int H_Mod = HASH; typedef struct List { struct List *next; int r; long long x, num; } list; int hash_func(int x, int r) { return ((x << 10) + r) % H_Mod; } int n = 1; list *hash[15][HASH] = {}, d[10000000]; void BFS(int N, int K) { if (N == 0) return; int h, hh, i, j; long long y, z; list *p, *pp; for (h = 0; h < H_Mod; h++) { for (p = hash[N][h]; p != NULL; p = p->next) { for (i = 9, y = p->x, z = 1; i >= 1; i--, y >>= 4, z <<= 4) { if (y % 16 == 0) continue; j = (p->r * 10 + i) % K; hh = hash_func(p->x - z, j); for (pp = hash[N-1][hh]; pp != NULL; pp = pp->next) if (pp->x == p->x - z && pp->r == j) break; if (pp != NULL) pp->num += p->num; else { d[n].x = p->x - z; d[n].r = j; d[n].num = p->num; d[n].next = hash[N-1][hh]; hash[N-1][hh] = &(d[n++]); } } } } BFS(N - 1, K); } int main() { int h, i, j, N, K, c[10]; list *p; scanf("%d %d", &N, &K); for (i = 1, c[0] = 0, d[0].x = 0; i <= 9; i++) { scanf("%d", &(c[i])); d[0].x = (d[0].x << 4) + c[i]; } d[0].r = 0; d[0].num = 1; h = hash_func(d[0].x, d[0].r); d[0].next = hash[N][h]; hash[N][h] = &(d[0]); BFS(N, K); for (p = hash[0][0]; p != NULL; p = p->next) if (p->x == 0 && p->r == 0) break; if (p != NULL) printf("%lld\n", p->num); else printf("-1\n"); fflush(stdout); return 0; }