Cod sursa(job #2495995)

Utilizator Goth11Ciurcau Leonardo Goth11 Data 20 noiembrie 2019 08:50:01
Problema Ratphu Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;

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

VI C;
VVI V;
int n, p, nrCif;

int main()
{
    in >> n >> p;
    while (n)
        C.push_back(n % 10), n /= 10, ++nrCif;

    reverse(C.begin(), C.end());
    V = VVI(1 << nrCif, VI(p));
    V[0][0] = 1;
    for (int i = 0; i < (1 << nrCif); ++i)
        for (int j = 0; j < p; ++j)
            if (V[i][j])
                for (int k = 0; k < nrCif; ++k)
                    if (!(i & (1 << k)))
                        V[i | (1 << k)][(j * 10 + C[k]) % p] += V[i][j];

    out << V[(1 << nrCif) - 1][0];
}