Cod sursa(job #1796873)

Utilizator silkMarin Dragos silk Data 3 noiembrie 2016 20:55:00
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <cstdio>
#define NMax 20
#define CMax 1<<18
#define ll long long

ll dp[CMax+1][NMax+1];
int v[NMax+1];

int main(){
    freopen("ratphu.in","r",stdin);
    freopen("ratphu.out","w",stdout);

    int i,j,k,nr,P;
    ll N;

    scanf("%lld %d",&N,&P);

    nr = 0;
    while(N) { v[++nr] = N%10; N/=10; }

    dp[0][0] = 1;
    for(i = 0; i < 1<<nr; ++i)
        for(j = 0; j < nr; ++j)
        if( !(i & (1<<j)) )
        {
            for(k = 0; k < P; ++k)
            dp[ i|(1<<j) ][ ( k*10 + v[j+1] ) % P ] += dp[i][k];
        }

    printf("%lld\n", dp[(1<<nr)-1][0] );



return 0;
}