Cod sursa(job #2022574)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 16 septembrie 2017 19:01:57
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
using namespace std;
ifstream cin("fact.in");
ofstream cout("fact.out");
long long f(long long n)
{
    long long s=0;
    while(n>0)
    {
        n/=5;
        s+=n;
    }
    return s;
}
int main()
{
    ios_base::sync_with_stdio();
    long long p,st=1,dr=1e18,ok=0,med,x,sol;
    cin>>p;
    if(p==0)
    {
        cout<<1;
        return 0;
    }
    while(st<dr)
    {
        med=(st+dr)/2;
        x=f(med);
        if(x>p)
        {
            dr=med-1;
            continue;
        }
        if(x<p)
        {
            st=med+1;
            continue;
        }
        ok=1;
        sol=med;
        break;
    }
    if(ok)
    {
        while(f(sol-1)==x)
            sol--;
        cout<<sol;
    }
    else
        cout<<-1;
    return 0;
}