Cod sursa(job #1107955)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 15 februarie 2014 10:34:44
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 20;

long long N, P, L, C[NMAX], A[1 << NMAX][NMAX];

int main()
{
    fin >> N >> P;
    while(N)
    {
        C[L++] = N % 10;
        N /= 10;
    }

    for(int i = 0; i < L; i++)
        A[(1 << i)][C[i] % P]++;

    int confmax = (1 << L);
    for(int conf = 1; conf < confmax; conf++)
        for(int r = 0; r < P; r++)
            if(A[conf][r])
                for(int j = 0; j < L; j++)
                    if(!((1 << j) & conf))
                        A[conf | (1 << j)][(r * 10 + C[j]) % P] += A[conf][r];

    fout << A[confmax - 1][0];
    return 0;
}