Pagini recente » Cod sursa (job #2344104) | Cod sursa (job #1828662) | Cod sursa (job #1205058) | Cod sursa (job #3277379) | Cod sursa (job #2496000)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef vector<ll> VI;
typedef vector<VI> VVI;
VI C;
VVI V;
ll n, p, nrCif;
int main()
{
freopen("ratphu.in", "r", stdin);
freopen("ratphu.out", "w", stdout);
scanf("%lld %lld", &n, &p);
while (n)
C.push_back(n % 10), n /= 10, ++nrCif;
reverse(C.begin(), C.end());
V = VVI(1 << nrCif, VI(p));
V[0][0] = 1;
for (int i = 0; i < (1 << nrCif); ++i)
for (int j = 0; j < p; ++j)
if (V[i][j])
for (int k = 0; k < nrCif; ++k)
if (!(i & (1 << k)))
V[i | (1 << k)][(j * 10 + C[k]) % p] += V[i][j];
printf("%lld", V[(1 << nrCif) - 1][0]);
}