Pagini recente » Cod sursa (job #2880769) | Cod sursa (job #228074) | Cod sursa (job #453691) | Cod sursa (job #3279984) | Cod sursa (job #1322943)
#include <fstream>
#define MAX_N (1 << 20) + 5
using namespace std;
ifstream f ("ratphu.in");
ofstream g("ratphu.out");
string s;
int p;
int d[MAX_N][25];
void read ()
{
f >> s >> p;
}
void solve ()
{
int lg = 1 << s.size();
int dim = s.size();
for (int i = 0; i < dim; i++)
{
//int x = (s[i] - '0') % p;
s[i] = (s[i] - '0') % p;
d[1 << i][x] = 1;
}
for (int i = 0; i < lg; i++)
for (int j = 0; j < dim; j++)
if ( (i & (1 << j) ) == 0)
for (int k = 0; k < p; k++)
{
//int c = (s[j] - '0') % p;
int x = k * 10 + s[j];
for( ; x >= p; x = x - p);
d[i | (1 << j)][x] += d[i][k];
}
g << d[lg - 1][0];
}
int main ()
{
read();
solve();
return 0;
}