Cod sursa(job #1946884)

Utilizator cristian.caldareaCaldarea Cristian Daniel cristian.caldarea Data 30 martie 2017 16:09:55
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

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

const int Dim = (1 << 18) ;

long long n, ps[Dim][20];
int p, c[20], nr;

int main()
{
    fin >> n >>  p;
    while ( n )
    {
        c[nr++] = n % 10;
        n /= 10;
    }
   // reverse(c, c + nr);
    ps[0][0] = 1;
    for ( int i = 0; i < ( 1 << nr); i++ )
    {
        for ( int j = 0; j < p; j++ )
            if ( ps[i][j] )
                for ( int k = 0; k < nr; k++ )
                {
                    if ( (i & ( 1 << k )) == 0 )
                        ps[i | ( 1 << k )][(j * 10 + c[k]) % p] += ps[i][j];
                }
    }
    fout << ps[(1 << nr) - 1][0];
    fin.close();
    fout.close();
    return 0;
}