Cod sursa(job #2481589)

Utilizator Ionut28Porumb Palincas Ionut Ionut28 Data 27 octombrie 2019 09:41:21
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
const int nmax = 262144;
unsigned long long n;
long long p, cifre, dp[nmax + 7][21], r[300];
vector < int > v, mask[nmax];
int main()
{
    fin >> n >> p;
    while(n)
    {
        cifre++;
        v.push_back(n % 10);
        n /= 10;
    }
    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];
    return 0;
}