Cod sursa(job #2624524)

Utilizator Cibotaru.MateiMatei-Stefan Cibotaru Cibotaru.Matei Data 4 iunie 2020 22:33:47
Problema Factorial Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <iostream>
#include <fstream>
using namespace std;

int getFives(int x) {
    int div = 5, res = 0, aux;
    do {
        aux = x / div;
        res += aux;
        div *= 5;
    } while (aux > 0);
    return res;
}

int searchFives(int p, int min, int max) {
    if (max - min == 5) return max;
    int pivot = min + (max - min) / 2;
    pivot -= pivot % 5;
    int fives = getFives(pivot);
    if (fives == p)
        return pivot;
    else if (fives < p)
        return searchFives(p, pivot, max);
    else
        return searchFives(p, min, pivot);
}

int main() {
    fstream f("fact.in", ios::in);
    int p;
    f >> p;
    f.close();
    
    f.open("fact.out", ios::out);

    if (p == 0)
        f << 1;
    else
        f << searchFives(p, 0, 5*p);

    f.close();
    return 0;
}