Cod sursa(job #1174393)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 22 aprilie 2014 18:31:23
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 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 )
					{
						dp[i | (1 << c)][(r * 10 + cif[c]) % P] += dp[i][r];
					}
				}
			}
		}
	}

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

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

    return 0;
}