Cod sursa(job #1442345)

Utilizator claudiu.gatinaFMI Claudiu Gatina claudiu.gatina Data 25 mai 2015 09:37:31
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I, Semestrul 2 Marime 0.61 kb
#include <fstream>
#include <iostream>

using namespace std;

fstream f("ratphu.in", ios::in);
fstream g("ratphu.out", ios::out);

long long x,d[1<<19][21];

int cif[20],p,nr;

int main()
{
    f>>x>>p;

    while(x){
        cif[nr++] = x % 10;
        x /= 10;
    }
    x = 1<<nr;

    for(int i = 0; i < nr; ++i)
        d[1<<i][cif[i]%p] = 1;
    for(int i = 1; i < x; ++i)
        for(int j = 0; j < nr; ++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];
    g<<d[x-1][0];
    return 0;
}