Cod sursa(job #2496003)

Utilizator Goth11Ciurcau Leonardo Goth11 Data 20 noiembrie 2019 09:09:30
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>
#include <vector>

using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VVL = vector<VL>;

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

ll n;
int p, nrCif;
VVL V;
VI C;

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

    V = VVL(1 << nrCif, VL(p));
    V[0][0] = 1;
    for (ll i = 0; i < (1 << nrCif); ++i)
        for (int j = 0; j < nrCif; ++j)
            if (!(i & (1 << j)))
                for (int k = 0; k < p; ++k)
                    V[i | (1 << j)][(k * 10 + C[j]) % p] += V[i][k];

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