Cod sursa(job #1225474)

Utilizator mihaimusatMihai Musat mihaimusat Data 2 septembrie 2014 17:16:46
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.49 kb
#include <cstdio>
#include <cstring>
#define MAXP 20

using namespace std;

long long DP[1<<MAXP][MAXP];
char v[MAXP];
int p,x;

int main()
{
	freopen("ratphu.in","r",stdin);
	freopen("ratphu.out","w",stdout);
	scanf("%s %d",v,&p);
	int n=strlen(v);
	x=1<<n;
	DP[0][0]=1;
	for(int i=0; i<x; i++)
		for(int j=0; j<p; j++)
			if(DP[i][j])
				for(int k=0; k<n; k++)
					if((i&(1<<k))==0)
						DP[i|(1<<k)][(j*10+v[k]-'0')%p]+=DP[i][j];
	printf("%lld",DP[x-1][0]);
	return 0;
}