Pagini recente » Cod sursa (job #1043781) | Cod sursa (job #1071810) | Cod sursa (job #1221604) | Cod sursa (job #2320587) | Cod sursa (job #465213)
Cod sursa(job #465213)
#include <cassert>
#include <cstdio>
#include <cstring>
const int MAX_P = 20;
const int MAX_DIGIT = 20;
const int MOD = 666013;
char s[MAX_DIGIT];
int n, p;
long long ways[1 << MAX_DIGIT][MAX_P];
void read() {
assert(freopen("ratphu.in", "r", stdin) != NULL);
assert(freopen("ratphu.out", "w", stdout) != NULL);
assert(scanf("%s %d", s, &p) == 2);
assert(1 <= p && p < MAX_P);
}
void solve() {
n = strlen(s);
ways[0][0] = 1;
for (int conf = 0; conf < (1 << n); ++conf)
for (int r = 0; r < p; ++r)
if (ways[conf][r])
for (int i = 0; i < n; ++i)
if (((conf >> i) & 1) == 0) {
int newConf = conf | (1 << i);
int newR = r * 10 + (s[i] - '0');
if (newR >= p) newR %= p;
ways[newConf][newR] += ways[conf][r];
}
printf("%lld\n", ways[(1 << n) - 1][0]);
}
int main() {
read();
solve();
}