Cod sursa(job #1322943)

Utilizator leontinLeontin leontin Data 20 ianuarie 2015 15:40:34
Problema Ratphu Scor 0
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul II Marime 0.89 kb
#include <fstream>
#define MAX_N (1 << 20) + 5

using namespace std;

ifstream f ("ratphu.in");
ofstream g("ratphu.out");

string s;
int p;
int d[MAX_N][25];

void read ()
{
    f >> s >> p;
}

void solve ()
{
    int lg = 1 << s.size();
    int dim = s.size();
    for (int i = 0; i < dim; i++)
    {
        //int x = (s[i] - '0') % p;
        s[i] = (s[i] - '0') % p;
        d[1 << i][x] = 1;
    }

    for (int i = 0; i < lg; i++)
        for (int j = 0; j < dim; j++)
            if ( (i & (1 << j) ) == 0)
                for (int k = 0; k < p; k++)
                {
                    //int c = (s[j] - '0') % p;
                    int x = k * 10 + s[j];
                    for( ; x >= p; x = x - p);
                    d[i | (1 << j)][x] += d[i][k];
                }

    g << d[lg - 1][0];
}

int main ()
{
    read();
    solve();
    return 0;
}