Pagini recente » Profil OJIparitateIsKillingMe | Diferente pentru utilizator/deehoroejkoli intre reviziile 36 si 37 | Cod sursa (job #1356579) | Diferente pentru utilizator/deehoroejkoli intre reviziile 59 si 60 | Cod sursa (job #2017095)
#include <bits/stdc++.h>
using namespace std;
int cif[20];
long long dp[300000][25];
long long pow2[30];
void put() {
pow2[0] = 1;
for (int i = 1; i <= 20; i++) {
pow2[i] = pow2[i - 1] * 2;
}
}
int main() {
freopen("ratphu.in", "r", stdin);
freopen("ratphu.out", "w", stdout);
put();
long long n;
int p;
cin >> n >> p;
int c = 0;
while (n) {
cif[c] = (int) (n % 10);
n = n / 10;
c++;
}
/*for (int i=0; i<c; i++){
cout<<cif[i]<<" ";
}*/
dp[0][0] = 1;
for (int i = 0; i < pow2[c]; i++) {
for (int j = 0; j < c; j++) {
if (i & (1 << j)) {
continue;
}
for (int rest = 0; rest < p; rest++) {
dp[i ^ pow2[j]][(rest * 10 + cif[j]) % p] += dp[i][rest];
}
}
}
cout << dp[pow2[c] - 1][0];
return 0;
}