Pagini recente » Cod sursa (job #429944) | Cod sursa (job #2093040) | Cod sursa (job #963846) | Cod sursa (job #821340) | Cod sursa (job #1796262)
#include <fstream>
using namespace std;
int cif[22];
long long dp[1<<20][20];
int pow2[22];
int k;
int modulo(int x){
return x -= (x/k)*k;
}
int main(){
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
long long n;
int i,c,limit,j;
int l;
fin>>n>>k;
c = 0;
pow2[0] = 1;
for(i = 1;i <= 20;i++){
pow2[i] = pow2[i-1]<<1;
}
while(n){
cif[c++] = n%10;
n /= 10;
}
limit = pow2[c];
for(i = 0;i < c;i++){
dp[pow2[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&pow2[j]) == 0){
dp[i|pow2[j]][modulo(l*10+cif[j])] += dp[i][l];
}
}
}
}
}
fout<<dp[limit-1][0];
return 0;
}