Cod sursa(job #1439364)

Utilizator YusukeFMI Mares Medar Razvan Yusuke Data 22 mai 2015 10:41:15
Problema Ratphu Scor 30
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I, Semestrul 2 Marime 0.51 kb
#include<cstdio>
using namespace std;
long long int N, M[300000][23];
int P, v[23], j, k, i, e;
int main()
{
    freopen("ratphu.in","r",stdin);
    freopen("ratphu.out","w",stdout);
    scanf("%d%d", &N, &P);
    for (;N;N/=10)
        v[e++] = N%10;
    M[0][0] = 1;
    for(i = 1;i < (1<<e); i++)
        for(j = 0;j < P; j++)
            for(k = 0;k < e; k++)
                if( i & (1<<k))
                    M[i][(j*10 + v[k]) % P] += M[i - (1<<k)][j];
    printf("%d", M[ (1<<e) - 1][0]);
}