Cod sursa(job #2075477)

Utilizator AndreiVisoiuAndrei Visoiu AndreiVisoiu Data 25 noiembrie 2017 14:29:02
Problema Factorial Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("fact.in");
ofstream out("fact.out");

int p, rasp;

int zeroFinal(int n) {
    int r = 0, exp = 5;
    while (exp <= n) {
        r += n / exp;
        exp *= 5;
    }

    return r;
}

int cauta(const int s, const int d) {
    int mij = (s+d)/2;
    if(zeroFinal(mij) == p) return (mij-mij%5);
    else {
        if(mij == s) return -1;
        else if(zeroFinal(mij) >= p) return cauta(s, mij-1);
        else return cauta (mij+1, d);
    }
}

int main()
{
    in >> p;
    if(p == 0) { out << 1; return 0; }
    rasp = cauta(0, 80000003);
    out << rasp << "\n";
    return 0;
}