Cod sursa(job #2127433)

Utilizator AndreiG23Ghiurcuta Andrei AndreiG23 Data 10 februarie 2018 17:39:45
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.57 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");

long long p;
int zero(int x)
{
    long long s=0;
    while(x>=5)
    {
        s=s+x/5;
        x=x/5;
    }
    return s;
}
int bsearch(int x)
{
    long long st=0,m,dr=1000000000;
    while(st<dr)
    {
        m=(st+dr)/2;
        if(zero(m)>p)dr=m;
        else st=m+1;
    }
    return st-5;
}


int main()
{
   fin>>p;
   if(p==0)fout<<1;
   else if(zero(bsearch(p)-5) != p)
            fout<<-1;
   else fout<<bsearch(p);
}