Cod sursa(job #2627842)

Utilizator loraclorac lorac lorac Data 12 iunie 2020 19:51:37
Problema Ratphu Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#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;
}