Pagini recente » Cod sursa (job #2019533) | Cod sursa (job #642721) | Cod sursa (job #205533) | Cod sursa (job #1737963) | Cod sursa (job #473458)
Cod sursa(job #473458)
#include <iostream>
#include <fstream>
#define nmax 1 << 20
#define pmax 20
using namespace std;
const char iname[] = "ratphu.in";
const char oname[] = "ratphu.out";
ifstream fin(iname);
ofstream fout(oname);
int dp[nmax][pmax], i, j, k, N, P, nr, a[nmax];
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;
for(i = 0; i < (1 << nr); i ++)
for(j = 0; j < P; j ++)
for(k = 1; k <= nr; k ++)
if(!(i & (1 << k)))
dp[i | (1 << k)][(j + a[k]) % P] += dp[i][j];
fout << dp[(1 << nr) - 1][0];
return 0;
}