Cod sursa(job #2831690)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 11 ianuarie 2022 21:27:33
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>

using namespace std;

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

int p;
long long exp5, st, md, dr, found=-1;

long long pow5(int fact){
    long long result = 0, add, crt = 5;
    do{
        add = fact / crt;
        result += add;
        crt *= 5;
    }while(add != 0);

    return result;
}

int main(){
    fin>>p;
    st = 1;
    dr = (1 << 30);
    while(st <= dr){
        md = (dr - st) / 2 + st;
        exp5 = pow5(md);

        if(exp5 == p)
            found = md;

        if(pow5(md) >= p)
            dr = md - 1;
        else
            st = md + 1;
    }
    fout<<found;
    return 0;
}