Cod sursa(job #2182131)

Utilizator DanielRusuDaniel Rusu DanielRusu Data 22 martie 2018 10:09:18
Problema Pascal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <iostream>
#include <cstdio>

using namespace std;

#define DIM 5000005

int d2[DIM], d3[DIM], d5[DIM];
int R, D, ans;

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

    scanf("%d %d\n", &R, &D);

    for(int i = 1; i <= R; ++i) {
        if(D != 5) {
            d2[i] = d2[i - 1],
            d3[i] = d3[i - 1];

            int x = i;
            while((x & 1) == 0) {
                x >>= 1;
                d2[i]++;
            }

            x = i;
            while(x % 3 == 0) {
                x /= 3;
                d3[i]++;
            }
        }
        else {
            d5[i] = d5[i - 1];

            int x = i;
            while(x % 5 == 0) {
                x /= 5;
                d5[i]++;
            }
        }
    }

    for(int i = 1; i <= R / 2; ++i) {
        int toAdd = 0;
        if(D != 5) {
            int a = d2[R] - d2[i] - d2[R - i];
            int b = d3[R] - d3[i] - d3[R - i];

            if(D == 2 && a > 0) toAdd = 1;
            if(D == 3 && b > 0) toAdd = 1;
            if(D == 4 && a > 1) toAdd = 1;
            if(D == 6 && a > 0 && b > 0) toAdd = 1;
        }
        else {
            int c = d5[R] - d5[i] - d5[R - i];

            if(D == 5 && c > 0) toAdd = 1;
        }

        if(i < R / 2 + (R & 1)) toAdd <<= 1;
        ans += toAdd;
    }

    cout << ans << '\n';

    return 0;
}