Cod sursa(job #1321123)

Utilizator bghimisFMI Ghimis Bogdan bghimis Data 18 ianuarie 2015 19:51:57
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul II Marime 0.6 kb
#include<cstdio>
#include<cstring>
#include<algorithm>
 
long long p, np[1 << 18][20]; 
int main()
{
  long long n, c[20];
  
  freopen("ratphu.in", "r", stdin);
  freopen("ratphu.out", "w", stdout);
  scanf("%lld %lld", &n, &p);
  
  int s = 0;
  while (n)
    {
      c[s++] = n % 10;
      n /= 10;
    }
  std::reverse(c, c + s);
  
  np[0][0] = 1;
  for (int i = 0; i < (1 << s); ++i)
    for (int j = 0; j < p; ++j)
      if (np[i][j])
	for (int k = 0; k < s; ++k)
	  if ((i & (1 << k)) == 0)
	    np[i | (1 << k)][(j * 10 + c[k]) % p] += np[i][j];
  
  printf("%lld", np[(1 << s) - 1][0]);
  return 0;
}