Cod sursa(job #1428426)

Utilizator sherban26FMI Mateescu Serban-Corneliu sherban26 Data 4 mai 2015 15:37:16
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I, Semestrul 2 Marime 0.71 kb
#include <iostream>
#include <fstream>

using namespace std;

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

long long n, d[263000][21], l;
int p, cif[21], nrcif;

int main()
{
    fin >> n >> p;

    while (n)
    {
        cif[nrcif++] = n % 10;
        n /= 10;
    }

    d[0][0] = 1;
    l = (1 << nrcif);

    for(long long i = 0; i < l; ++i)
    {
        for (int j = 0; j < nrcif; ++j)
        {
            if ((i & (1 << j)) == 0)
            {
                for (int k = 0; k < p; ++k)
                {
                    d[i | (1 << j)][(k * 10 + cif[j]) % p] += d[i][k];
                }
            }
        }
    }

    fout << d[l - 1][0];

    return 0;
}