Pagini recente » Cod sursa (job #3151088) | Cod sursa (job #1939309) | Cod sursa (job #2194227) | Cod sursa (job #399410) | Cod sursa (job #466022)
Cod sursa(job #466022)
#include <fstream>
using namespace std;
const int MAX_P = 20;
const int MAX_N = (1 << 18);
ifstream fin ("ratphu.in");
ofstream fout ("ratphu.out");
long long N;
int P, Ncf, R[MAX_N][MAX_P];
char cif[MAX_P];
void trans(int N) {
while(N) {
cif[++Ncf] = N % 10;
N /= 10;
}
}
long long pow10(int x) {
long long sol = 1;
for(int i = 1; i <= x; ++i)
sol *= 10;
return sol;
}
int main() {
fin >> N >> P;
trans(N);
R[0][0] = 1;
for(int conf = 1; conf < (1 << Ncf); ++conf) {
int nr = 0;
for(int i = 0; i < Ncf; ++i) {
if(conf & (1 << i)) {
++nr;
}
}
for(int r = 0; r < P; ++r) {
for(int i = 0; i < Ncf; ++i) {
if(conf & (1 << i)) {
int actr = pow10(nr-1)*cif[i+1] % P;
int newr = (actr + r) % P;
R[conf][newr] += R[conf ^ (1 << i)][r];
}
}
}
}
fout << R[(1 << Ncf)-1][0];
}