Cod sursa(job #2764471)

Utilizator DragosC1Dragos DragosC1 Data 20 iulie 2021 23:59:33
Problema Pascal Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <fstream>
#include <iostream>
using namespace std;

int R, D;
int rez;

void read() {
    ifstream f("pascal.in");
    f >> R >> D;
    f.close();
}

void solve() {

    // randurile 0 si 1 nu pot avea elemente divizibile cu 2, 3, 4, 5, 6 => afisam 0
    if (R == 0 || R == 1)
        return;
    
    int i, p2, p3, aux, put, ok;
    for (i = 1; i <= R; i++) {
        p2 = 0, p3 = 0;

        if (D % 2 == 0) {
            aux = R, put = 2;
            while (aux >= put) {
                p2 += aux / put;
                put *= 2;
            }
            aux = i - 1, put = 2;
            while (aux >= put) {
                p2 -= aux / put;
                put *= 2;
            }
            aux = R - i + 1, put = 2;
            while (aux >= put) {
                p2 -= aux / put;
                put *= 2;
            }
        }
        if (D % 3 == 0) {
            aux = R, put = 3;
            while (aux >= put) {
                p3 += aux / put;
                put *= 3;
            }
            aux = i - 1, put = 3;
            while (aux >= put) {
                p3 -= aux / put;
                put *= 3;
            }
            aux = R - i + 1, put = 3;
            while (aux >= put) {
                p3 -= aux / put;
                put *= 3;
            }
        }

        ok = 1;
        if (D % 2 == 0 && p2 < 1)
            ok = 0;
        if (D % 4 == 0 && p2 < 2)
            ok = 0;
        if (D % 3 == 0 && p3 < 1)
            ok = 0;

        rez += ok;
    }

}

void output() {
    ofstream g("pascal.out");
    g << rez;
    g.close();
}

int main() {
    read();
    solve();
    output();
    return 0;
}