Cod sursa(job #1991809)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 18 iunie 2017 14:04:16
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>

using namespace std;

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

const int nmax = 18;
const int pmax = 20;

int cif[nmax + 1];
long long d[(1 << nmax)][ pmax ];

int main() {
    int n, p; long long x;
    fin >> x >> p;

    n = 0;
    while (x > 0) {
        cif[n ++] = x % 10;
        x /= 10;
    }

    d[ 0 ][ 0 ] = 1;
    for (int i = 0; i < (1 << n) - 1; ++ i) {
        for (int j = 0; j < n; ++ j) {
            if (i & (1 << j)) continue;

            int st = i + (1 << j);
            for (int r = 0; r < p; ++ r) {
                d[ st ][(r * 10 + cif[ j ]) % p] += d[ i ][ r ];
            }
        }
    }

    fout << d[(1 << n) - 1][ 0 ] << "\n";

    return 0;
}