Cod sursa(job #2627859)

Utilizator loraclorac lorac lorac Data 12 iunie 2020 21:19:48
Problema Ratphu Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <stack>
using namespace std;
ifstream cin("ratphu.in");
ofstream cout("ratphu.out");
typedef long long ll;
ll st[21][262150];
ll cif[21],rest[21];
stack<ll> q;
ll a,p,nr;
int main()
{
    cin>>a>>p;
    ll lim=1;
    while(a)
    {
        cif[nr++]=a%10;
        lim*=2;
        a/=10;
    }
    rest[0]=1%p;
    for(ll i=1;i<=18;++i)
        rest[i]=(rest[i-1]*10)%p;
    st[0][0]=1;
    ll mask,cat,x,bk,ew;
    for(ll k=1;k<lim;++k)
    {
        mask=k;
        for(ll i=0;i<nr and mask;++i)
        {
            if(mask%2)
                q.push(i);
            mask/=2;
        }
        cat=q.size()-1;
        while(!q.empty())
        {
            x=q.top();
            bk=k-(1<<x);
            ew=cif[x]*rest[cat];
            q.pop();
            for(ll j=0;j<p;++j)
                st[(j+ew)%p][k]+=st[j][bk];
        }
    }
    cout<<st[0][lim-1];
    return 0;
}