Pagini recente » Cod sursa (job #1762115) | Cod sursa (job #1148136) | Cod sursa (job #270454) | Cod sursa (job #2144371) | Cod sursa (job #2495972)
#include <fstream>
using namespace std;
ifstream cin ("ratphu.in");
ofstream cout ("ratphu.out");
long long n;
int p;
int d[1 << 18][20];
int cif[20], nrcif;
int main()
{
cin >> n >> p;
while (n)
{
cif[++ nrcif] = n % 10;
n /= 10;
}
d[0][0] = 1;
for (int i = 0; i < (1 << nrcif); ++ i)
for (int j = 1; j <= nrcif; ++ j)
if (!(i & (1 << (j - 1))))
for (int r = 0; r < p; ++ r)
d[i | (1 << (j - 1))][(r * 10 + cif[j])%p] += d[i][r];
cout << d[(1 << nrcif) - 1][0];
return 0;
}