Cod sursa(job #2342739)

Utilizator armigheGheorghe Liviu Armand armighe Data 13 februarie 2019 12:24:05
Problema Ratphu Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.68 kb
#include<fstream>
using namespace std;
ifstream f("ratphu.in");
ofstream g("ratphu.out");
int dp[300002][20],v[25];
int main()
{
    long long n;
    int 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--;
    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][(j*10+v[l])%p]+=dp[i][j];
            }
            z*=2;
        }
    }
    g<<dp[n][0];
    return 0;
}