Pagini recente » Cod sursa (job #2384880) | Cod sursa (job #2733688) | Cod sursa (job #625645) | Cod sursa (job #3275583) | Cod sursa (job #2859830)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
char s[25];
int dp[270000][25], p, a[25], n, rest[25][25];
int main()
{
fin >> (s + 1);
for(int i = 1; s[i]; i++)
a[n++] = s[i] - '0';
fin >> p;
long long P = (1 << n);
for(int i = 0; i <= 19; i++)
for(int j = 0; j < n; j++)
rest[i][j] = (i * 10 + a[j]) % p;
dp[0][0] = 1;
for(int stare = 0; stare < P; stare++)
for(int i = 0; i < n; i++)
if((stare & (1 << i)) == 0)
for(int r = 0; r < p; r++)
dp[stare | (1 << i)][rest[r][a[i]]] += dp[stare][r];
fout << dp[P - 1][0] << "\n";
fout.close();
return 0;
}