Pagini recente » Cod sursa (job #2949308) | Cod sursa (job #2653502) | Cod sursa (job #1165318) | Cod sursa (job #258410) | Cod sursa (job #465753)
Cod sursa(job #465753)
#include<cstring>
#include<fstream>
#include<queue>
using namespace std;
#define c(i) (n[i] - '0')
#define d(i) (ax[i] - '0')
char n[1 << 18], ax[1 << 18];
long long p, np[1 << 18][20]; // numarul de posibilitati luand configuratia 'i', sa am restul 'j'
bool sl[1 << 18], ps[1 << 18];
queue<int> q;
int main()
{
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
fin >> n >> p;
int s = strlen(n);
//PD
for (int i = 0; i < s; ++i)
{
ax[1 << i] = n[i];
np[1 << i][c(i) % p] = 1;
for (int j = 0; j < s; ++j)
if (j != i && ps[(1 << i) | (1 << j)] == false)
{
q.push((1 << i) | (1 << j));
ps[(1 << i) | (1 << j)] = true;
}
}
while (!q.empty())
{
int now = q.front(); q.pop();
int aux = now, i;
if (sl[now] == false)
{
while (aux)
{
i = aux & -aux;
if (now & i)
for (int j = 0; j < p; ++j)
np[now][(j * 10 + d(i)) % p] += np[now ^ i][j];
aux &= aux - 1;
}
for (int i = 0; i < s; ++i)
if ((now & (1 << i)) == 0)
if (ps[now | (1 << i)] == false)
{
q.push(now | (1 << i));
ps[now | (1 << i)] = true;
}
sl[now] = true;
}
}
fout << np[(1 << s) - 1][0];
}