Cod sursa(job #475728)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 8 august 2010 06:30:24
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.5 kb
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXL = 20;
const int MAXX = 262144;

char v[MAXL];
int p, n;
long long c[MAXX][MAXL];

int main() {
	freopen("ratphu.in", "r", stdin);
	freopen("ratphu.out", "w", stdout);

	scanf("%s %d", v, &p);
	n = strlen(v);

	c[0][0] = 1;
	for (int i = 0; i < 1<<n; ++i)
		for (int j = 0; j < p; ++j)
			for (int k = 0; k < n; ++k)
				if ((i&(1<<k)) == 0)
					c[i^(1<<k)][(j*10+v[k]-'0')%p] += c[i][j];

	printf("%lld\n", c[(1<<n)-1][0]);

	return 0;
}