Cod sursa(job #1444198)

Utilizator radudorosRadu Doros radudoros Data 29 mai 2015 13:04:18
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 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; k < 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];
		l++;
	}
	int pow = 1 << l;
	
	solve(pow);
	
	fout << d[pow - 1][0];
}