Cod sursa(job #1174395)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 22 aprilie 2014 18:31:56
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <iostream>
#include <fstream>

using namespace std;

const int Nmax = 18;
const int Pmax = 20;

long long dp[1 << Nmax][Pmax];
long long N;
int cif[Nmax], C;
int P;

inline int BIT( int n, int i )
{
	return ( n & ( 1 << i ) );
}

int main()
{
    ifstream in("ratphu.in");
    ofstream out("ratphu.out");

    in >> N >> P;

    while ( N )
	{
		cif[ C++ ] = N % 10;
		N /= 10;
	}

    dp[0][0] = 1;

	for ( int i = 0; i < ( 1 << C ); ++i )
	{
		for ( int r = 0; r < P; ++r )
		{
			if ( dp[i][r] )
			{

				for ( int c = 0; c < C; ++c )
				{
					if ( BIT( i, c ) == 0 )
					{
						int aux = (r * 10 + cif[c]);

						while ( aux >= P ) aux -= P;

						dp[i | (1 << c)][aux] += dp[i][r];
					}
				}
			}
		}
	}

	out << dp[(1 << C) - 1][0] << "\n";

	in.close();
	out.close();

    return 0;
}