Cod sursa(job #2182122)

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

using namespace std;

#define DIM 2500005

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) {
        d2[i] = d2[i - 1],
        d3[i] = d3[i - 1],
        d5[i] = d5[i - 1];
        int x = i;
        while((x & 1) == 0) {
            x >>= 1;
            d2[i]++;
        }

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

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

    for(int i = 1; i <= R / 2; ++i) {
        int a = d2[R] - d2[i] - d2[R - i];
        int b = d3[R] - d3[i] - d3[R - i];
        int c = d5[R] - d5[i] - d5[R - i];

        int toAdd = 0;
        if(D == 2 && a > 0) toAdd = 1;
        if(D == 3 && b > 0) toAdd = 1;
        if(D == 4 && a > 1) toAdd = 1;
        if(D == 5 && c > 0) toAdd = 1;
        if(D == 6 && a > 0 && b > 0) toAdd = 1;

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

    cout << ans << '\n';

    return 0;
}