Cod sursa(job #831497)

Utilizator Teodor94Teodor Plop Teodor94 Data 8 decembrie 2012 18:15:06
Problema Pascal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <cstdio>

const int MAX_FACT = 6;

int fact[MAX_FACT];

void factors(int x, int add) {
    while (x % 2 == 0) {
        fact[2] += add;

        x >>= 1;
    }

    while (x % 3 == 0) {
        fact[3] += add;

        x /= 3;
    }

    while (x % 5 == 0) {
        fact[5] += add;

        x /= 5;
    }
}

inline bool ok(int d) {
    if (d == 4)
        return fact[2] > 1;

    if (d == 6)
        return fact[3] > 0 && fact[2] > 0;

    return fact[d] > 0;
}

void solve(int r, int d) {
    int half = r >> 1, res = 0;
    for (int i = 1; i <= half; ++i) {
        int top = r - i + 1, bottom = i;

        factors(top, 1);

        factors(bottom, -1);

        if (ok(d))
            res += 2;
    }

    if (r % 2 == 0 && ok(d))
        --res;

    printf("%d\n", res);
}

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

    int r, d;
    scanf("%d%d", &r, &d);

    solve(r, d);
}