Cod sursa(job #466385)

Utilizator irene_mFMI Irina Iancu irene_m Data 26 iunie 2010 14:06:33
Problema Ratphu Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <cstdio>
#define infile "ratphu.in"
#define outfile "ratphu.out"
#define MaxN 1<<18
#define MaxP 25

int sol[MaxN][MaxP];
int P;
long long N;
int nrc,cif[MaxP];

void cifre()
{
      long long aux=N;
      while(aux)
      {
            cif[nrc++]=aux%10;
            aux/=10;
      }
}

void solve()
{
      int i,j,k;
      sol[0][0]=1;
      for(i=0;i<(1<<nrc);i++)
            for(j=0;j<P;j++)
                  if(sol[i][j])
                        for(k=0;k<nrc;k++)
                              if(!((1<<k)&i))
                                    sol[i+(1<<k)][(j*10+cif[k])%P]+=sol[i][j];
}

int main()
{
      freopen(infile,"r",stdin);
      freopen(outfile,"w",stdout);

      scanf("%lld%d",&N,&P);
      cifre();
      solve();
      printf("%d\n",sol[(1<<nrc)-1][0]);

      fclose(stdin);
      fclose(stdout);
      return 0;
}