Cod sursa(job #689320)

Utilizator SchumiDumitru Andrei Georgian Schumi Data 24 februarie 2012 12:59:19
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;

long long conf, d[(1 << 18)][20];
int p;
string n;

int main()
{
    int conf, r, i;
    freopen ("ratphu.in", "r", stdin);
    freopen ("ratphu.out", "w", stdout);
    cin >> n;
    scanf("%d", &p);
    d[0][0] = 1;
    for(conf = 0; conf < (1LL << n.size()); ++conf) {
        for(r = 0; r < p; ++r) {
            for(i = 0; i < n.size(); ++i) {
                if(!(conf & (1LL << i))) {
                    d[conf + (1LL << i)][(r * 10 + n[i] - '0') % p] += d[conf][r];
                }
            }
        }
    }
    printf("%lld", d[(1LL << n.size()) - 1][0]);
    return 0;
}