Pagini recente » Cod sursa (job #2102536) | Cod sursa (job #2141158) | Cod sursa (job #2163884) | Cod sursa (job #3201779) | Cod sursa (job #1348516)
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "ratphu";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif
typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;
const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
int N, M, P;
char S[25];
lld DP[(1 << 18) + 5][25];
int main() {
int i, p, mask, new_p, new_mask;
#ifndef ONLINE_JUDGE
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
#endif
scanf("%s", S);
N = strlen(S);
scanf("%d", &P);
for(i = 0; i < N; i++)
S[i] -= '0';
M = (1 << N) - 1;
DP[0][0] = 1;
for(mask = 0; mask < M; mask++)
for(p = 0; p < P; p++) {
for(i = 0; i < N; i++)
if(!(mask & (1 << i))) {
new_mask = mask | (1 << i);
new_p = (p * 10 + S[i]) % P;
DP[new_mask][new_p] += DP[mask][p];
}
}
printf("%lld\n", DP[M][0]);
return 0;
}