Pagini recente » Cod sursa (job #395679) | Cod sursa (job #950494) | Cod sursa (job #2625115) | Cod sursa (job #352926) | Cod sursa (job #1759008)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin("ratphu.in");
ofstream cout("ratphu.out");
const int MAXD = 19;
const int MAXP = 20;
int digit[MAXD], p;
long long dp[1 << MAXD][1 + MAXP];
int Modulo(int value) {
while (value >= p)
value -= p;
return value;
}
int main() {
long long n;
cin >> n >> p;
int digits = -1;
while (n) {
digits++;
digit[digits] = n % 10;
n /= 10;
}
int limit = (1 << (digits + 1)) - 1;
dp[0][0] = 1;
for (int mask = 0; mask < limit; mask++)
for (int j = 0; j < p; j++)
for (int k = 0; k <= digits; k++)
if ((!(mask & (1 << k))))
dp[mask + (1 << k)][Modulo(j * 10 + digit[k])] += dp[mask][j];
cout << dp[limit][0] << "\n";
return 0;
}