Cod sursa(job #1197568)

Utilizator alexm456alexandru maican alexm456 Data 12 iunie 2014 19:01:44
Problema Pascal Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <cstdio>
using namespace std;

int r, d;

int pfct(int n, int p){
    if (p == 4) p = 2;
    int pw = 0;
    while (n % p == 0){
        pw++;
        n /= p;
    }
    return pw;
}

int main(){
    freopen("pascal.in", "r", stdin);
    freopen("pascal.out", "w", stdout);
    
    int l, ps, pj, pt = 0, ans = 0, ps2, pj2, ps3, pj3, pt2 = 0, pt3 = 0;
    bool ok = false;
    
    scanf("%d %d", &r, &d);
    
    l = r / 2;
    if (d != 6 && d != 4){
        for (int i = 1; i <= l; i++){
            ps = pfct(r - i + 1, d);
            pj = pfct(i, d);
            pt += (ps - pj);
            if (pt > 0) {
                ans++;
                ok = true;
            }
            else ok = false;
        }
    }
    else if (d == 6){
        for (int i = 1; i <= l; i++){
            ps2 = pfct(r - i + 1, 2);
            pj2 = pfct(i, 2);
            pt2 += (ps2 - pj2);
            ps3 = pfct(r - i + 1, 3);
            pj3 = pfct(i, 3);
            pt3 += (ps3 - pj3);
            if (pt2 > 0 && pt3 > 0) {
                ans++;
                ok = true;
            }
            else ok = false;
        }
    }
    else{
        for (int i = 1; i <= l; i++){
            ps = pfct(r - i + 1, d);
            pj = pfct(i, d);
            pt += (ps - pj);
            if (pt > 1) {
                ans++;
                ok = true;
            }
            else ok = false;
        }
    }
    
    if (!(r%2) && ok) printf ("%d", 2 * ans - 1);
    else printf ("%d", 2 * ans);
    
    
    return 0;
}