Cod sursa(job #1759008)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 18 septembrie 2016 12:56:43
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

const int MAXD = 19;
const int MAXP = 20;

int digit[MAXD], p;
long long dp[1 << MAXD][1 + MAXP];

int Modulo(int value) {
    while (value >= p)
        value -= p;
    return value;
}

int main() {
    long long n;
    cin >> n >> p;
    int digits = -1;
    while (n) {
        digits++;
        digit[digits] = n % 10;
        n /= 10;
    }
    int limit = (1 << (digits + 1)) - 1;
    dp[0][0] = 1;
    for (int mask = 0; mask < limit; mask++)
        for (int j = 0; j < p; j++)
            for (int k = 0; k <= digits; k++)
                if ((!(mask & (1 << k))))
                    dp[mask + (1 << k)][Modulo(j * 10 + digit[k])] += dp[mask][j];
    cout << dp[limit][0] << "\n";
    return 0;
}