Cod sursa(job #2430147)

Utilizator memecoinMeme Coin memecoin Data 12 iunie 2019 21:38:11
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "ratphu";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

int n;
int p;
int64_t nr;
int a[50];

int64_t d[1 << 19][20];

int main() {
    
    fin >> nr;
    fin >> p;
    
    while (nr > 0) {
        a[n++] = nr % 10;
        nr /= 10;
    }
    
    d[0][0] = 1;
    
    for (int i = 0; i < (1 << n); ++i) {
        for (int j = 0; j < n; ++j) {
            if (i & (1 << j)) {
                continue;
            }
            for (int r = 0; r < p; ++r) {
                int nr = (10 * r + a[j]) % p;
                
                d[i | (1 << j)][nr] += d[i][r];
            }
        }
    }
    
    fout << d[(1 << n) - 1][0];
    
    return 0;
}