Cod sursa(job #2296840)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 5 decembrie 2018 01:18:22
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("ratphu.in");
ofstream out("ratphu.out");
const int maxn = 20;
long long dp[(1 << maxn)][maxn + 5];
int module[250];
int v[maxn];
int main()
{
    long long n, p;
    in >> n >> p;
    int poz = -1;
    while(n > 0)
    {
        v[++poz] = n % 10;
        n = n / 10;
    }
    n = poz + 1;
    reverse(v, v + n);
    for(int i = 1; i <= 190 + 9; i++)
        module[i] = i % p;
    dp[0][0] = 1;
    for(int conf = 0; conf < (1 << n); conf++)
    {
        for(int mod = 0; mod < p; mod++)
        {
            for(int bit = 0; bit < n; bit++)
            {
                if(conf & (1 << bit))
                    continue;
                int aux = module[mod * 10 + v[bit]];
                dp[conf + (1 << bit)][aux] += dp[conf][mod];
            }
        }
    }
    out << dp[(1 << n) - 1][0] << "\n";
    return 0;
}