Cod sursa(job #2199851)

Utilizator LivcristiTerebes Liviu Livcristi Data 29 aprilie 2018 12:55:18
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
#include <iostream>
using namespace std;
int n;
int check(int p, int n, int cod = 1)
{
    int f = 5, total = 0;
    while(p >= f)
    {
        total += p / f;
        f *= 5;
    }
    if(cod)
        return (total >= n);
    else
        return total == n;
}
int cauta(int n)
{
    if(n == 0)
        return 1;
    int st, dr, mij;
    st = 4 * n;
    dr = 5 * n;
    while(st < dr)
    {
        mij = st + (dr - st) / 2;
        if(check(mij, n))
            dr = mij;
        else
            st = mij + 1;
    }
    if(check(st, n, 0))
        return st;
    return -1;
}
int main()
{
    ifstream f("fact.in");
    ofstream g("fact.out");
    f >> n;
    g << cauta(n);
    f.close();
    g.close();
}