Cod sursa(job #2352984)

Utilizator lnegreanuLucia Negreanu-Maior lnegreanu Data 23 februarie 2019 19:38:51
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <fstream>

using namespace std;
ifstream cin("fact.in");
ofstream cout("fact.out");

int nz (int n) {
    int r=0, f=5;
    while (f<=n) { 
        r+=n/f;
        f*=5;
    }
    return r;
}

int binarySearch(int left, int right, int p)
{
    int mid, temp, k = -1;
 
    while( left <= right )
    {
        mid = (left + right)/2;
        temp = nz(mid);
        if( temp == p ){
            k = mid;
            right = mid-1;
        }
        else if( temp > p )
            right = mid -1;
        else
            left = mid + 1;
    }
    return k;
}
const int SIZE = 2100000000; 
long long n,p;
int main() {    
    cin >> p;
    cout << binarySearch(1, SIZE, p);
    
    cin.close();
    cout.close();
    return 0;
}