Cod sursa(job #1796228)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 3 noiembrie 2016 11:22:40
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <cstdio>
#include <queue>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

int cif[25];
long long dp[1<<21][25];

int main(){
    freopen("ratphu.in", "r", stdin);
    freopen("ratphu.out", "w", stdout);
    long long n,i,c,limit,j,k,l;
    scanf("%lld %lld", &n, &k);
    c = 0;
    while(n){
        cif[c++] = n%10;
        n /= 10;
    }
    limit = 1<<c;
    dp[0][0] = 1;
    for(i = 1;i < limit;i++){
        for(l = 0;l < k;l++){
            for(j = 0;j < c;j++){
                if(i&(1<<j)){
                    dp[i][(l*10+cif[j])%k] += dp[i^(1<<j)][l];
                }
            }
        }
    }
    printf("%lld", dp[limit-1][0]);
    return 0;
}