Cod sursa(job #2616553)

Utilizator pavelandrei17Pavel Andrei-Gabriel pavelandrei17 Data 18 mai 2020 20:53:00
Problema Factorial Scor 45
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" );

long long p;

int nr_zerouri_fact( int n )
{
    int k = 5, s = 0;
    while( k <= n )
    {
        s += n / k;
        k *= 5;
    }
    return s;
}

int cautare_binara( int s, int d )
{
    long long m;
    int  x;

    while( s <= d )
    {
        m = ( s + d ) / 2;
        x = nr_zerouri_fact (m);
        if( x < p )
            s = m + 1;
        else d = m - 1;
    }

    if( nr_zerouri_fact (m) == p )
        return m;
    else
        return -1;
}

int main()
{
    fin>> p ;
    fout<< cautare_binara(1, 100000000);
}