Cod sursa(job #1423594)

Utilizator Vladut-Vlad Panait Vladut- Data 22 aprilie 2015 00:27:11
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I, Semestrul 2 Marime 0.88 kb
#include <fstream>

using namespace std;

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

int p, cifre[21], nr, q, aux;
long long int d[262500][21], n;

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

    while(n)
    {
        cifre[nr++]=n%10;
        n/=10;
    }

    q=1<<nr;
    d[0][0]=1;

    for(int i=0; i<q; i++)
    {
        for(int j=0; j<p; j++)
        {
            if(d[i][j])
            {
                for(int k=0; k<nr; k++)
                {
                    if(!(i&(1<<k)))
                    {
                        aux=j*10+cifre[k];
                        while(aux>=p)
                        {
                            aux-=p;
                        }
                        d[i|(1<<k)][aux]+=d[i][j];
                    }
                }
            }
        }
    }

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

    return 0;
}