Pagini recente » Cod sursa (job #903531) | Cod sursa (job #2277232) | Cod sursa (job #2227153) | Cod sursa (job #1884865) | Cod sursa (job #1107984)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
const int NMAX = 20;
int P, L, C[NMAX];
long long N, A[1 << NMAX][NMAX];
int main()
{
fin >> N >> P;
long long AUX = N;
while(AUX)
{
C[L] = AUX % 10;
AUX /= 10;
++A[(1 << L)][C[L++] % P];
}
int confmax = (1 << L);
for(int conf = 1; conf < confmax; ++conf)
for(int r = 0; r < P; ++r)
if(A[conf][r])
for(int j = 0; j < L; ++j)
if(!((1 << j) & conf))
A[conf | (1 << j)][(r * 10 + C[j]) % P] += A[conf][r];
fout << A[confmax - 1][0];
return 0;
}