Cod sursa(job #1526050)

Utilizator PaulStighiStiegelbauer Paul-Alexandru PaulStighi Data 15 noiembrie 2015 21:12:45
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>
#include <climits>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");

long long check(long long x)
{
    long long nr0=0,h5=5;
    while(h5<=x)
    {
        nr0=nr0+x/h5;
        h5=h5*5;
    }
    return nr0;
}

int main()
{
    long long p,st,dr,mid,sol,g;

    int ok;

    fin>>p;

    if (p==0){fout<<"1\n";}
    else
    {
        st=1;
        dr=LONG_LONG_MAX;
        ok=1;
        while(st<=dr)
        {
            mid=(st+dr)/2;
            g=check(mid);
            if(g<p){st=mid+1;}
            else
            {
                if(g==p){sol=mid;ok=0;}
                dr=mid-1;}
        }
        if(ok){fout<<"-1\n";}
        else{fout<<sol<<"\n";}
    }
}