Cod sursa(job #2055382)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 3 noiembrie 2017 10:23:43
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
unsigned long long p,n,st=1,dr=1e18,med,x;
unsigned long long f(unsigned long long x)
{
    unsigned long long sol=0;
    while(x>0)
    {
        sol+=x/5;
        x/=5;
    }
    return sol;
}
int main()
{
    fin>>p;
    if(p==0)
    {
        fout<<1;
        return 0;
    }
    while(st<=dr)
    {
        med=(st+dr)/2;
        x=f(med);
        if(x==p)
        {
            if(f(med-1)!=p)
            {
                fout<<med;
                return 0;
            }
            dr=med;
            continue;
        }
        if(x<p)
            st=med+1;
        else
            dr=med-1;
    }
    fout<<-1;
    return 0;
}