Cod sursa(job #466859)

Utilizator elfusFlorin Chirica elfus Data 27 iunie 2010 19:56:26
Problema Ratphu Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
//dinamica pe bitzisori 
#include<stdio.h>
#define LL long long
LL elfus[(LL)1<<18][20];
int cifre[30];
int main()
{
LL N;
int p,i,j,k;
freopen("ratphu.in","r",stdin);
freopen("ratphu.out","w",stdout);
scanf("%lld%d",&N,&p);
do
  {
  cifre[++cifre[0]]=N%10;
  N/=10;
  }
while(N);
elfus[0][0]=1;
for(i=0;i<= 1<<cifre[0] - 1 ; i++)
   for(j=0;j<p;j++)
     if(elfus[i][j])
       for(k=0;k<cifre[0];k++)
	 if(((1<<k)&i)==0)  //masca de bitzisori
	   elfus[i+(1<<k)][(j*10+cifre[k])%p]+=elfus[i][j]; //recurenta.. tot pe bitzisori
printf("%lld",elfus[(1<<cifre[0])-1][0]);
return 0;
}