Pagini recente » Cod sursa (job #2103009) | Cod sursa (job #2951949) | Cod sursa (job #1145990) | Cod sursa (job #1817288) | Cod sursa (job #1796257)
#include <cstdio>
using namespace std;
int cif[22];
long long dp[1<<20][20];
int k;
int modulo(int x){
return x -= (x/k)*k;
}
int main(){
freopen("ratphu.in", "r", stdin);
freopen("ratphu.out", "w", stdout);
long long n,i,c,limit,j,l;
scanf("%lld %lld", &n, &k);
c = 0;
while(n){
cif[c++] = n%10;
n /= 10;
}
limit = 1<<c;
for(i = 0;i < c;i++){
dp[1<<i][modulo(cif[i])] = 1;
}
for(i = 1;i < limit;i++){
for(l = 0;l < k;l++){
if(dp[i][l]){
for(j = 0;j < c;j++){
if((i&(1<<j)) == 0){
dp[i|(1<<j)][modulo(l*10+cif[j])] += dp[i][l];
}
}
}
}
}
printf("%lld", dp[limit-1][0]);
return 0;
}