Pagini recente » Cod sursa (job #247583) | Cod sursa (job #1857574) | Cod sursa (job #2271883) | Cod sursa (job #1275139) | Cod sursa (job #473470)
Cod sursa(job #473470)
#include <iostream>
#include <fstream>
#define nmax 1 << 18
#define pmax 20
using namespace std;
const char iname[] = "ratphu.in";
const char oname[] = "ratphu.out";
ifstream fin(iname);
ofstream fout(oname);
long long dp[nmax + 5][pmax];
int i, j, k, N, P, nr, a[pmax];
int calc(int x)
{
int rez = 0;
while(x > 0)
{
int c = x % 10;
x = x / 10;
++ rez;
a[rez] = c;
}
return rez;
}
int main()
{
fin >> N >> P;
nr = calc(N);
dp[0][0] = 1;
reverse(a + 1, a + nr + 1);
for(i = 0; i < (1 << nr); i ++)
for(j = 0; j < P; j ++)
for(k = 0; k < nr; k ++)
if((i & (1 << k)) == 0)
dp[i | (1 << k)][(j * 10 + a[k + 1]) % P] += dp[i][j];
fout << dp[(1 << nr) - 1][0];
return 0;
}