Cod sursa(job #1420280)

Utilizator StefanRARapeanu-Andreescu Stefan StefanRA Data 18 aprilie 2015 02:27:12
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I, Semestrul 2 Marime 0.48 kb
#include <stdio.h>
int n[20],c,p,i,j,k,t,a;
long long r[1<<19][20];
int main()
{
	freopen("ratphu.in","r",stdin);
	freopen("ratphu.out","w",stdout);
	while ((n[c]=getc(stdin))!=' ')
		n[c++]-='0';
	scanf("%d",&p);
	t=1<<c;
	r[0][0]=1;
	for(i=0;i<t;i++)
		for(j=0;j<p;j++)
			if(r[i][j])
				for(k=0;k<c;k++)
					if(!(i&(1<<k)))
					{
						a=j*10+n[k];
						while(a>=p)
							a-=p;
						r[i|(1<<k)][a]+=r[i][j];
					}
	printf("%lld",r[t-1][0]);
	return 0;
}