Cod sursa(job #1488018)

Utilizator zacuscaAlex Iordache zacusca Data 17 septembrie 2015 19:35:58
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include<fstream>
using namespace std;
ifstream in("fact.in");
ofstream out("fact.out");
int p,n,sol;
int nr0(int x)
{
    int cnt=0;
    int p=5;
    while(p<=x)
    {
        cnt+=x/p;
        p*=5;
    }
    return cnt;
}
int main()
{
    in>>p;
    if(!p)out<<"1\n";
    else
    {
        int st=1,dr=5*p;
        while(st<=dr&&!sol)
        {
            int m=(dr+st)/2;
            int aux=nr0(m);
            if(aux>p)
                dr=m-1;
            else if(aux<p)
                st=m+1;
            else
            sol=m;
        }
        if(sol)out<<sol-sol%5<<'\n';
        else out<<-1;
    }
    out.close();
    return 0;
}