Cod sursa(job #1423593)
Utilizator | Data | 22 aprilie 2015 00:24:21 | |
---|---|---|---|
Problema | Ratphu | Scor | 30 |
Compilator | cpp | Status | done |
Runda | Teme Pregatire ACM Unibuc 2014, Anul I, Semestrul 2 | Marime | 0.87 kb |
#include <fstream>
using namespace std;
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
int n, p, cifre[21], nr, q, aux;
long long d[262500][21];
int main()
{
fin >> n >> p;
while(n)
{
cifre[nr++]=n%10;
n/=10;
}
q=1<<nr;
d[0][0]=1;
for(int i=0; i<q; i++)
{
for(int j=0; j<p; j++)
{
if(d[i][j])
{
for(int k=0; k<nr; k++)
{
if(!(i&(1<<k)))
{
aux=j*10+cifre[k];
while(aux>=p)
{
aux-=p;
}
d[i|(1<<k)][aux]+=d[i][j];
}
}
}
}
}
fout << d[q-1][0];
return 0;
}