Cod sursa(job #2342744)

Utilizator armigheGheorghe Liviu Armand armighe Data 13 februarie 2019 12:26:29
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include<fstream>
using namespace std;
ifstream f("ratphu.in");
ofstream g("ratphu.out");
long long dp[300002][20],v[25],r[25][25];
int main()
{
    long long n,p,k=0,i,j,x,y,z,l;
    f>>n>>p;
    while(n!=0)
    {
        v[++k]=n%10;
        n/=10;
    }
    n=1;
    for(i=1;i<=k;i++)
        n*=2;
    n--;
    for(i=0;i<p;i++)
    for(j=1;j<=k;j++)
        r[i][j]=(i*10+v[j])%p;
    dp[0][0]=1;
    for(i=0;i<=n;i++)
    for(j=0;j<p;j++)
    if(dp[i][j]!=0)
    {
        x=i;
        z=1;
        for(l=1;l<=k;l++)
        {
            y=x%2;
            x/=2;
            if(y==0)
            {
                dp[i+z][r[j][l]]+=dp[i][j];
            }
            z*=2;
        }
    }
    g<<dp[n][0];
    return 0;
}