Cod sursa(job #3232651)

Utilizator Gergo123Schradi Gergo Gergo123 Data 31 mai 2024 19:18:25
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("fact.in");
ofstream fout("fact.out");

int p;

long long f(long long n)
{
    long long put=5;
    long long cnt=0;
    while(n>=put){
        cnt+=n/put;
        put*=5;
    }
    return cnt;
}

long long binariskereses()
{
    long long x=0;
    int l=0;
    long long r=p*5;
    while(l<=r){
        x=(l+r)/2+1;
        if(x==r&&f(x)!=p) return 0;
        if(f(x)<p) l=x;
        else if(f(x)>p) r=x;
        else return x;
    }
}

int main()
{
    fin >> p;
    int a = binariskereses();
    if(a==0){
        fout << -1;
    }
    else if(a<5){
        fout << 1;
    }
    else{
        fout << a/5 * 5;
    }
    return 0;
}