Cod sursa(job #1429835)

Utilizator hrazvanHarsan Razvan hrazvan Data 7 mai 2015 12:32:32
Problema Ratphu Scor 80
Compilator c Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <stdio.h>
#define MAXCIF 18
#define MAXP 20
long long d[1 << MAXCIF][MAXP];
int nrcif = 0;
char cif[MAXCIF];

int main(){
  FILE *in = fopen("ratphu.in", "r");
  long long n;
  int p;
  fscanf(in, "%lld%d", &n, &p);
  fclose(in);
  while(n > 0){
    cif[nrcif] = n % 10;
    n /= 10;
    nrcif++;
  }
  d[0][0] = 1;
  int i, j, k = 0;
  for(i = 0; i < (1 << nrcif); i++){
    for(j = 0; j < p; j++){
      for(k = 0; k < nrcif; k++){
        if(!(i & (1 << k))){
          d[i ^ (1 << k)][(j * 10 + cif[k]) % p] += d[i][j];
        }
      }
    }
  }
  FILE *out = fopen("ratphu.out", "w");
  fprintf(out, "%lld", d[(1 << nrcif) - 1][0]);
  fclose(out);
  return 0;
}