Cod sursa(job #1796240)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 3 noiembrie 2016 11:30:42
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 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 pow2[21];
int k;

int modulo(int x){
    return x -= (x/k)*k;
}

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