Cod sursa(job #1993252)

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

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

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

  cin >> dig >> p; n = strlen(dig);
  for(i = 0; i < n; ++i) dig[i] -= '0';

  dp[0][0] = 1;

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

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

  return 0;
}