Cod sursa(job #1993238)

Utilizator DjokValeriu Motroi Djok Data 22 iunie 2017 16:20:55
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul II Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;

int i, mask, n, p, rest, now;
long long dp[20][1 << 18];
string dig;

int main() {
  ifstream cin("ratphu.in");
  ofstream cout("ratphu.out");
  ios_base::sync_with_stdio(0);

  cin >> dig >> p; n = dig.size();

  dp[0][0] = 1;

  for(mask = 0; mask < (1 << n); ++mask) {
    for(rest = 0; rest < p; ++rest) {
      if(!dp[rest][mask]) continue;

      for(i = 0; i < n; ++i) {
        if(mask & (1 << i)) continue;
        now = (rest * 10 + dig[i] - '0') % p;
        dp[now][mask | (1 << i)] += dp[rest][mask];
      }
    }
  }

  cout << dp[0][(1 << n) - 1] << '\n';

  return 0;
}