Cod sursa(job #3166138)

Utilizator nicushor21Pirlog Marian Nicolae nicushor21 Data 7 noiembrie 2023 19:18:16
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int p,cnt,i,j,aux,x,st,dr,mid;
int main(){
    ifstream fin("fact.in");
    ofstream fout("fact.out");
    fin>>p;
    if(p!=0){
        st=1; dr=p; x=-1;
        while(st<=dr){
            mid=(st+dr)/2;
            i=1; cnt=0;
            aux=mid/5;
            while(aux>0){
                cnt+=aux;
                aux/=5;
            }
            aux=mid+cnt;
            if(aux==p){
                x=mid;
                break;
            }
            if(aux>p){
                dr=mid-1;
            } else{
                st=mid+1;
            }
        }
        x*=5;
        if(x>=0){
            fout<<x;
        } else{
            fout<<-1;
        }
    } else{
        fout<<1;
    }
    fin.close();
    fout.close();
    return 0;
}