Cod sursa(job #2296834)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 5 decembrie 2018 01:14:55
Problema Ratphu Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 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 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);
    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 = (mod * 10 + v[bit]) % p;
                dp[conf + (1 << bit)][aux] += dp[conf][mod];
            }
        }
    }
    out << dp[(1 << n) - 1][0] << "\n";
    return 0;
}