Cod sursa(job #1718730)

Utilizator dani_mocanuDani Mocanu dani_mocanu Data 18 iunie 2016 22:00:15
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
const int cif = 18;
int p,a[25],n,M;
char s[25];
long long dp[(1 << cif)  + 5][25],P2[25];
vector < int > biti[(1 << cif) + 5];

inline void Build()
{
    int i,j;
    P2[0] = 1;
    for(i = 1; i <= cif; i++) P2[i] = 2*P2[i-1];
    for(i = 0; s[i]; i++) a[n++] = s[i]-'0';
    M = (1 << n) -1;
    for(i = 0; i <= M; i++)
        for(j = 0; j < n; j++)
            if((P2[j]&i) == 0)
                biti[i].push_back(j);
}


inline void Solve()
{
    int stare,i,j,k,z;
    dp[0][0] = 1;
    for(stare = 0; stare <= M; stare++)
        for(j = 0; j < p; j++)
            for(k = 0; k < biti[stare].size(); k++)
            {
                z =  biti[stare][k];
                dp[stare + P2[z]][(10*j + a[z])%p] += dp[stare][j];
            }
    fout << dp[M][0] << "\n";
}

int main()
{
    fin >> (s) >> p;
    Build();
    Solve();
    fout.close();
    return 0;
}