Pagini recente » Cod sursa (job #1913750) | Cod sursa (job #1783188) | Cod sursa (job #3169960) | Cod sursa (job #605811) | Cod sursa (job #466038)
Cod sursa(job #466038)
#include <fstream>
#include <cassert>
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, R[MAX_N][MAX_P];
int P, Ncf;
char cif[MAX_P];
void trans(long long N) {
for(; N; N /= 10) {
cif[Ncf++] = N % 10;
}
}
int main() {
fin >> N >> P;
trans(N);
R[0][0] = 1;
assert(N > 0);
for(int conf = 1; conf < (1 << Ncf); ++conf) {
int nr = 0, aux = conf;
for(int i = 0; i < Ncf; ++i) {
if(aux & 1) {
++nr;
}
aux >>= 1;
}
aux = conf;
for(int i = 0; i < Ncf; ++i) {
for(int r = 0; r < P; ++r) {
int newc = conf ^ (1 << i);
if(aux & 1) {
int newr = (r*10 + cif[i]) % P;
R[conf][newr] += R[newc][r];
}
}
aux >>= 1;
}
}
fout << R[(1 << Ncf)-1][0];
}