Cod sursa(job #2496000)

Utilizator Goth11Ciurcau Leonardo Goth11 Data 20 noiembrie 2019 09:01:56
Problema Ratphu Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef vector<ll> VI;
typedef vector<VI> VVI;

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

int main()
{
    freopen("ratphu.in", "r", stdin);
	freopen("ratphu.out", "w", stdout);
	scanf("%lld %lld", &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];

    printf("%lld", V[(1 << nrCif) - 1][0]);
}