Cod sursa(job #1444192)

Utilizator radudorosRadu Doros radudoros Data 29 mai 2015 12:59:32
Problema Ratphu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include<fstream>
using namespace std;

const int NMAX = 20;
long long d[1 << NMAX][NMAX];
int c[NMAX];
long long n, p;
int l;

void solve(int x)
{
	for (int i = 1; i < x; i++)
	{
		for (int j = 0; j < l; j++)
		{
			if (((1 << j) & (i)) == 0)
			{
				for (int k = 0; j < p; k++)
				{
					d[i | (1 << j)][(k * 10 + c[j]) % p] += d[i][k];
				}
			}
		}
	}
}


int main()
{
	ifstream fin("ratphu.in");
	ofstream fout("ratphu.out");
	
	fin >> n >> p;
	
	long long aux = n;
	while (aux)
	{
		c[l] = aux % 10;
		aux /= 10;
		++d[(1 << l)][c[l] % p];
	}

	solve(1 << l);
	
	fout << d[(1<<l) - 1][0];
}