Cod sursa(job #2205453)

Utilizator triscacezarTrisca Vicol Cezar triscacezar Data 19 mai 2018 11:02:56
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.54 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int p;
int factorial(int x)
{
    int ans=0,f=5;
    while(x>=f)
    {
        ans+=x/f;
        f*=5;
    }
    return ans;
}
int main()
{
        fin>>p;
        int st=1 , dr=5*p;
        while(st <= dr)
        {
            int mij=(st+dr)/2;
            if(factorial(mij) < p) st=mij+1;
            else dr=mij-1;
        }
        if(factorial(st)==p) fout<<st;
        else fout<<-1;
    return 0;

}