Cod sursa(job #2296831)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 5 decembrie 2018 01:12:58
Problema Ratphu Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("ratphu.in");
ofstream out("ratphu.out");
const int maxn = 22;
long long dp[maxn][(1 << maxn)];
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;
}