Cod sursa(job #2495974)

Utilizator ancabdBadiu Anca ancabd Data 20 noiembrie 2019 08:26:47
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <fstream>

using namespace std;

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

long long n;
int p;
long long d[1 << 18][20];
int cif[20], nrcif;

int main()
{
    cin >> n >> p;

    while (n)
    {
        cif[++ nrcif] = n % 10;
        n /= 10;
    }

    d[0][0] = 1;

    for (long long i = 0; i < (1 << nrcif); ++ i)
        for (int j = 1; j <= nrcif; ++ j)
            if (!(i & (1 << (j - 1))))
                for (int r = 0; r < p; ++ r)
                    d[i | (1 << (j - 1))][(r * 10 + cif[j])%p] += d[i][r];

    cout << d[(1 << nrcif) - 1][0];
    return 0;
}