Pagini recente » Cod sursa (job #504744) | Cod sursa (job #2823853) | Cod sursa (job #2297231) | Cod sursa (job #766376) | Cod sursa (job #489548)
Cod sursa(job #489548)
#include<fstream>
#include<algorithm>
#define MMAX 21
#define NMAX (1<<18)+1
using namespace std;
ifstream f("ratphu.in");
ofstream g("ratphu.out");
int a[21], n, p;
long long pos[NMAX][MMAX];
void inverseaza()
{
int p=0, u=n;
while (p<u)
{
swap(a[p],a[u]);
++p;--u;
}
}
void citeste()
{
char c='x';
while (c!=' ')
{
f.get(c);
if (c!=' ') a[n++]=c-'0';
}
--n;
f>>p;
inverseaza();
}
void rezolva()
{
int i, r, j, aux, m=(1<<n+1)-1, rest;
for(i=0; i<=n; ++i) pos[1<<i][a[i]%p]=1;
for(i=1; i<=m; ++i)
for(r=0; r<p; ++r)
if (pos[i][r]>0)
for (j=0; j<=n; ++j)
{
aux=i|(1<<j);
if (aux!=i)
{
rest=(r*10+a[j])%p;
pos[aux][rest]+=pos[i][r];
}
}
}
int main()
{
citeste();
rezolva();
g<<pos[(1<<n+1)-1][0]<<"\n";
f.close();
g.close();
return 0;
}