Pagini recente » Cod sursa (job #2616653) | Cod sursa (job #1345301)
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
const int pmax = 21;
const int lmax = (1 << 18) + 5;
int p, i, j, k, lim, m, a[pmax];
ll n, dp[lmax][pmax];
int main()
{
freopen("ratphu.in", "r", stdin);
freopen("ratphu.out", "w", stdout);
scanf("%lld%d", &n, &p);
while(n)
{
a[m++] = n % 10;
n /= 10;
}
dp[0][0] = 1;
lim = 1 << m;
for(i = 0; i < lim; i++)
for(j = 0; j < p; j++)
if(dp[i][j])
for(k = 0; k < m; k++)
if(((1 << k) & i) == 0)
dp[i + (1 << k)][(j * 10 + a[k]) % p] += dp[i][j];
printf("%lld\n", dp[lim - 1][0]);
return 0;
}