Cod sursa(job #2507664)

Utilizator DariusDCDarius Capolna DariusDC Data 10 decembrie 2019 17:47:14
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;

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

int n;
long long nr;
int p;

long long dp[(1<<18)+5][21];
int c[21];

int main()
{
    fin >> nr >> p;
    while (nr)
    {
        c[n++] = nr % 10;
        nr /= 10;
    }
    dp[0][0] = 1;
    for (int stare = 0; stare < (1 << n)-1; stare++)
    {
        for (int i = 0; i < n; i++)
        {
            if (stare & (1 << i))
                continue;
            for (int rest = 0; rest < p; rest++)
            {
                dp[stare + (1 << i)][(10 * rest + c[i]) % p] += dp[stare][rest];
            }
        }
    }
    fout << dp[(1<<n) - 1][0];
    return 0;
}