Cod sursa(job #1796268)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 3 noiembrie 2016 11:45:35
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.53 kb
#include <fstream>
using namespace std;
ifstream f("ratphu.in");
ofstream g("ratphu.out");
long long n,dp[1<<18][21];
int p,i,l,j,k,c[23],M[23];
inline int MoD(int x)
{
    if(x>=p) x-=p;
    return x;
}
int main()
{
    f>>n>>p;
    while(n) c[l++]=(n%10)%p,n/=10;
    for(i=0;i<=p;++i) M[i]=i*10%p;
    for(i=dp[0][0]=1;i<(1<<l);++i)
        for(j=0;j<p;++j)
            for(k=0;k<l;++k)
                if(i&(1<<k))
                    dp[i][MoD(M[j]+c[k])]+=dp[i-(1<<k)][j];
    g<<dp[(1<<l)-1][0];
    return 0;
}