#pragma GCC target ("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; #define rep(i, n) for(int i = 0; i < int(n); i++) using ll = long long; using P = pair; // i桁まで決定されていて、数字の残りがxで、数字 % K == jとなるものの通り数 int main() { cin.tie(0); ios_base::sync_with_stdio(false); int n, K; cin >> n >> K; vector c(10); rep(i, 9) cin >> c[i + 1]; vector sum(11); rep(i, 10) sum[i + 1] = sum[i] + c[i]; vector>> dp(n + 1, vector>(1 << n, vector(K, 0))); dp[0][0][0] = 1; rep(i, n) { rep(j, 1 << n) { rep(k, K) { if (dp[i][j][k] == 0) continue; for (int l = 1; l <= 9; l++) { int left = -1; for (int pos = sum[l]; pos < sum[l + 1]; pos++) { if (!(j >> pos & 1)) { left = pos; break; } } if (left == -1) continue; dp[i + 1][j | (1 << left)][(k * 10 + l) % K] += dp[i][j][k]; } } } } rep(i, n + 1) { rep(j, 1 << n) { rep(k, K) { // cout << "{" << i << " " << j << " " << k << "}" << " " << dp[i][j][k] << endl; } } } cout << dp[n][(1 << n) - 1][0] << endl; return 0; }