Cod sursa(job #2210207)

Utilizator nicolaefilatNicolae Filat nicolaefilat Data 5 iunie 2018 21:36:36
Problema Ratphu Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <fstream>
#define MAXN 1<<18 - 1
#define MAXP 20

using namespace std;

ifstream in("ratphu.in");
ofstream out("ratphu.out");

int v[19],n,p,nr_cifre;
long long dp[MAXN][MAXP];

int main()
{
    in>>n>>p;
    while(n){
        v[nr_cifre] = n % 10;
        ++nr_cifre;
        n /= 10;
    }
    dp[0][0] = 1;

    int mask = (1<<(nr_cifre)) - 1;
    for(int i = 0; i < mask; i++){
        for(int j = 0; j < p; j++){
            if(dp[i][j]){
                for(int k = 0; k < nr_cifre; k++)
                    if((i & (1<<k)) == 0)
                        dp[(i + (1<<k))][(j*10 + v[k]) % p] += dp[i][j];
            }
        }
    }
    out<<dp[mask][0];
    return 0;
}