Cod sursa(job #1337171)

Utilizator wGEORGEWGeorge Cioti wGEORGEW Data 8 februarie 2015 17:59:48
Problema Pascal Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <cstdio>
 
int r,d;
 
// i!/((i-j)!*j!)
 
inline void count(int n, const int primes[], bool divs[], int cnts[], int v) {
    for (int i = 0; i < 3; ++i) {
        if (!divs[i])
            continue;
        const int x = primes[i];
        if (x == 2)
            for (;!(n & 1); n >>= 1, cnts[i] += v);
        else
            for (;!(n % x); n /= x, cnts[i] += v);
    }
}
 
int main()
{
    freopen("pascal.in", "r", stdin);
    freopen("pascal.out", "w", stdout);
    scanf("%d%d", &r, &d);
 
    int cnt2 = 0, cnt3 = 0, cnt5 = 0, res = 0;
    const int primes[] = {2, 3, 5};
    bool divs[] = {0, 0, 0};
    int cnts[] = {0, 0, 0};
     
    for (int i = 0; i < 3; ++i)
        if (!(d % primes[i]))
            divs[i] = true;
 
    for (int j = 1; j <= r/2; ++j) {
        count(j, primes, divs, cnts, -1);
        count(r-j+1, primes, divs, cnts, +1);
 
        int x = 0;
        switch (d) {
            case 2: { x = bool(cnts[0]); break; }
            case 3: { x = bool(cnts[1]); break; }
            case 4: { x = bool(cnts[0] >= 2); break; }
            case 5: { x = bool(cnts[2]); break; }
            case 6: { x = bool(cnts[0] && cnts[1]); break; }
        }
        res += (j*2 < r) ? 2*x : x;
    }
    printf("%d\n", res);
 
    return 0;
}