Pagini recente » Cod sursa (job #402715) | Cod sursa (job #2557696) | Cod sursa (job #2122700) | Cod sursa (job #1318322) | Cod sursa (job #2627842)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("ratphu.in");
ofstream cout("ratphu.out");
typedef long long ll;
ll st[23][(1<<18)+3],rest[23],cif[23];
ll cnt,a,p,lim,k,mask,i,j;
vector<ll> used;
int main()
{
cin>>a>>p;
for(;a;a/=10)
cif[cnt++]=a%10;
rest[0]=1LL;
for(i=1;i<cnt;++i)
rest[i]=(rest[i-1]*10LL)%p;
lim=(1LL<<cnt);
st[0][0]=1LL;
for(k=1;k<lim;++k)
{
mask=k;
used.clear();
for(i=0;i<cnt;++i,mask/=2)
if(mask%2) used.push_back(i);
mask=used.size()-1;
for(ll i:used) for(j=0;j<p;++j)
st[(j+cif[i]*rest[mask])%p][k]+=st[j][k-(1LL<<i)];
}
cout<<st[0][lim-1]<<'\n';
return 0;
}