Cod sursa(job #1345305)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 17 februarie 2015 15:20:12
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

using namespace std;

const int pmax = 21;
const int lmax = (1 << 18) + 5;

int p, i, j, k, lim, m, a[pmax];
ll n, dp[lmax][pmax];

int fix(int x)
{
    while(x >= p)
        x -= p;
    return x;
}

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

    scanf("%lld%d", &n, &p);

    while(n)
    {
        a[m++] = n % 10;
        n /= 10;
    }

    dp[0][0] = 1;
    lim = 1 << m;

    for(i = 0; i < lim; i++)
        for(j = 0; j < p; j++)
            if(dp[i][j])
                for(k = 0; k < m; k++)
                    if(((1 << k) & i) == 0)
                        dp[i + (1 << k)][fix(j * 10 + a[k])] += dp[i][j];

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

    return 0;
}