Cod sursa(job #2466736)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 2 octombrie 2019 20:53:19
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <bits/stdc++.h>
#define NMAX 262144

using namespace std;

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

unsigned long long n;
long long p, cifre, dp[NMAX + 7][21], r[300];
vector <int> v, mask[NMAX + 5];

int main()
{
    fin >> n >> p;
    while (n)
    {
        v.push_back(n % 10);
        n = n / 10;
        ++cifre;
    }
    int stare = (1 << cifre);
    for (int i = 0; i < stare; ++i)
        for (int j = 0; j < cifre; ++j)
            if ((i >> j) & 1 == 1)
                mask[i].push_back(j);
    dp[0][0] = 1;
    for (int i = 0; i <= 208; ++i)
        r[i] = i % p;
    for (int i = 1; i < stare; ++i)
    {
        for (int rest = 0; rest < p; ++rest)
        {
            for (int j = 0; j < mask[i].size(); ++j)
            {
                int poz = mask[i][j];
                dp[i][rest] += dp[i - (1 << poz)][r[rest * 10 + v[poz]]];
            }
        }
    }
    fout << dp[stare - 1][0];
    fin.close();
    fout.close();
    return 0;
}