Cod sursa(job #473460)

Utilizator dornescuvladVlad Eugen Dornescu dornescuvlad Data 29 iulie 2010 16:10:38
Problema Ratphu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#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);

int dp[nmax + 5][pmax], 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 = 1; k <= nr; k ++)
				if(!(i & (1 << k)))
					dp[i | (1 << k)][(j * 10 + a[k]) % P] += dp[i][j];
	fout << dp[(1 << nr) - 1][0];

	return 0;
}