Pagini recente » Cod sursa (job #1766982) | Cod sursa (job #2507369) | Cod sursa (job #2642642) | Cod sursa (job #1592242) | Cod sursa (job #1107983)
#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;
}
for(int i = 0; i < L; i++)
++A[(1 << i)][C[i] % 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;
}