Cod sursa(job #3153963)

Utilizator amunnumeVlad Patrascu amunnume Data 2 octombrie 2023 15:15:24
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int n,p,i,st,dr,mid,sol=-1;
short verif(int x)
{
    int q=5,t=0,k;
    while(1)
    {
        k=(x/q);
        if(!k) break;
        t+=k;
        q*=5;
    }
    if(t<p) return -1;
    else if(t==p) return 0;
    return 1;
}
int main()
{
    fin>>p;
    if(!p) {fout<<1; return 0;}
    st=1; dr=5*p;
    while(st<=dr)
    {
        mid=(st+dr)>>1;
        if(!verif(mid)){dr=mid-1; sol=mid;}
        else if(verif(mid)==1) dr=mid-1;
        else st=mid+1;
    }
    fout<<sol;
    return 0;
}