Cod sursa(job #1107984)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 15 februarie 2014 12:16:57
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;

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

int main()
{
    fin >> N >> P;

    long long AUX = N;
    while(AUX)
    {
        C[L] = AUX % 10;
        AUX /= 10;
        ++A[(1 << L)][C[L++] % 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;
}