#include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; ll n, k; cin >> n >> k; vector c(10); vector u; for(ll i = 1; i <= 9; i++) { cin >> c[i]; for(ll j = 0; j < c[i]; j++) u.push_back(i); } vector pow10(n); pow10[0] = 1%k; for(ll i = 1; i < n; i++){ pow10[i] = (pow10[i-1]*10)%k; } set> st; ll m = n/2; auto inv = [&](vector v){ assert(c.size() == 10); vector ans(10); for(ll i = 1; i <= 9; i++) ans[i] = c[i]-v[i]; return ans; }; for(ll i = 0; i < 1< v(10); for(ll j = 0; j < n; j++){ if(i&(1< vv)->map{ vector v; for(ll i = 1; i <= 9; i++){ for(ll j = 0; j < vv[i]; j++) v.push_back(i); } ll n = v.size(); // debug_value(n) vector u(n); for(ll i = 0; i < n; i++) u[i] = i; set st; do{ ll sum = 0; for(ll i = 0; i < n; i++){ sum = sum*10+v[u[i]]; } st.insert(sum); }while(next_permutation(u.begin(), u.end())); map ans; for(ll x: st){ ll rem = x%k; if(ans.count(rem)) ans[rem]++; else ans[rem] = 1; } return ans; }; auto cnt = [&](vector v)->ll{ assert(accumulate(v.begin(), v.end(), 0) == m); auto u = inv(v); auto mpv = list(v); auto mpu = list(u); ll ans = 0; for(auto [x, c]: mpv){ ll rem = (k-x*pow10[n-m])%k; rem = (rem+k)%k; // debug_value(rem) if(mpu.count(rem) > 0) ans += mpu[rem]*c; } return ans; }; ll ans = 0; for(auto v: st) ans += cnt(v); cout << ans << endl; }