Pagini recente » Cod sursa (job #2710187) | Cod sursa (job #1549429) | Cod sursa (job #1369707) | Cod sursa (job #2897853) | Cod sursa (job #2254032)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
const int nmax=(1<<19);
int n,p,a[22];
long long dp[nmax][22];
char s[22];
int main()
{
fin>>s;
fin>>p;
for(int i=0;s[i];i++)
a[n++]=s[i]-'0';
const int Nmax=(1<<n);
dp[0][0]=1;
for(int stare=0;stare<Nmax;stare++)
for(int bit=0;bit<n;bit++)
if(!(stare & (1<<bit)))
for(int rest=0;rest<p;rest++)
dp[stare | (1<<bit)][(rest*10+a[bit])%p]+=dp[stare][rest];
fout<<dp[Nmax-1][0]<<"\n";
fin.close();
fout.close();
return 0;
}