Cod sursa(job #788735)

Utilizator SebiSebiPirtoaca George Sebastian SebiSebi Data 15 septembrie 2012 19:00:58
Problema Ratphu Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include<iostream>
#include<fstream>
using namespace std;
#define bit( x , nr ) ( ( (x) & ( 1 << (nr) ) ) != 0 )
const int NMAX = 18;
const int PMAX = 20;
const int INF = 1<<30;
int cif[10],d[1<<NMAX][PMAX];
void citeste(int &p)
{
	long long x;
	ifstream f("ratphu.in");
	f>>x>>p;
	while(x) {
		cif[++cif[0]]=x%10;
		x=x/10;
	}
	f.close();
}
int main()
{
	int i,j,k,n,p;
	citeste(p);
	ofstream g("ratphu.out");
	n=cif[0];
	for(i=1;i<=n;i++)
		d[1<<(i-1)][cif[i]%p]++;
	for(i=1;i<(1<<n);i++) {
		for(j=0;j<=n-1;j++)
			if(bit(i,j)==0) {
				for(k=0;k<=p-1;k++) 
					d[i|(1<<j)][(k*10+cif[j+1])%p]+=d[i][k];
			}
	}
	g<<d[(1<<n)-1][0]<<'\n';
	g.close();
	return 0;
}