Cod sursa(job #1197545)

Utilizator alexm456alexandru maican alexm456 Data 12 iunie 2014 16:35:14
Problema Pascal Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 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;
    
    scanf("%d %d", &r, &d);
    
    l = (r + 1) / 2;
    if (d != 6){
        for (int i = 1; i <= l; i++){
            ps = pfct(r - i + 1, d);
            pj = pfct(i, d);
            pt += (ps - pj);
            if (d == 4){
                if (pt && pt % 2 == 0) ans++;
            }
            else {
                if (pt > 0) ans++;
            }
        }
    }
    else{
        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++;
        }
    }
    
    if (!(r%2) && !(pt > 0 || (pt2 > 0 && pt3 > 0))) printf ("%d", 2 * ans - 1);
    else printf ("%d", 2 * ans);
    
    
    return 0;
}