Cod sursa(job #1234259)

Utilizator vladrochianVlad Rochian vladrochian Data 26 septembrie 2014 22:56:48
Problema Ratphu Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.56 kb
#include <fstream>
using namespace std;

char str[20];
int p, N, digit[18], dp[1 << 18][20], lim;

ifstream fin("ratphu.in");
ofstream fout("ratphu.out");

int main() {
	fin >> str >> p;
	for (N = 0; str[N]; ++N) {
		digit[N] = str[N] - '0';
		dp[1 << N][digit[N] % p] = 1;
	}
	lim = 1 << N;
	for (int conf = 1; conf < lim; ++conf)
		for (int dg = 0; dg < N; ++dg)
			if (!(conf & (1 << dg)))
				for (int rest = 0; rest < p; ++rest)
					dp[conf | (1 << dg)][(rest * 10 + digit[dg]) % p] += dp[conf][rest];
	fout << dp[lim - 1][0] << "\n";
	return 0;
}