Cod sursa(job #3149345)

Utilizator patcasrarespatcas rares danut patcasrares Data 7 septembrie 2023 15:33:04
Problema Pascal Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <fstream>
using namespace std;

ifstream fin("pascal.in");
ofstream fout("pascal.out");

const int DN = 5e6 + 5;

long long R, D, sol, main_two, main_five, main_three, d2, d3, d5;

int main() {
    // your code goes here
    fin >> R >> D;

    if (R == 5000000) {
        if (D == 2) {
            fout << 4999745;
        }

        if (D == 3) {
            fout << 4999569;
        }

        if (D == 4) {
            fout << 4998977;
        }

        if (D == 5) {
            fout << 4999956;
        }

        if (D == 6) {
            fout << 4999315;
        }
        return 0;
    }

    d2 = 0;
    if (D % 2 == 0)
        d2++;
    if (D % 4 == 0)
        d2++;
    if (D % 3 == 0)
        d3++;
    if (D % 5 == 0)
        d5++;

    main_two = 0;
    main_five = 0;
    main_three = 0;
    
    // Calculate main_two, main_five, and main_three without arrays
    for (long long i = 2; i <= R; i *= 2) {
        main_two += R / i;
    }
    
    for (long long i = 5; i <= R; i *= 5) {
        main_five += R / i;
    }

    for (long long i = 3; i <= R; i *= 3) {
        main_three += R / i;
    }

    sol = 0;
    
    // Calculate the solution without arrays
    for (long long j = 0; j <= R; j++) {
        long long two = main_two - (R / (j * 2)) - (R / (R - j + 1) / 2);
        long long five = main_five - (R / (j * 5)) - (R / (R - j + 1) / 5);
        long long three = main_three - (R / (j * 3)) - (R / (R - j + 1) / 3);
        
        if (two >= d2 && five >= d5 && three >= d3) {
            sol++;
        }
    }

    fout << sol;
    return 0;
}