Cod sursa(job #1702511)

Utilizator xtreme77Patrick Sava xtreme77 Data 15 mai 2016 12:31:00
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
/**
 * Code by Patrick Sava
 * "Spiru Haret" National College of Bucharest
 **/

#include <fstream>

using namespace std;

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

long long zeros ( int n )
{
    long long s = 0 ;
    int p = 5 ;
    while ( n / p ) {
        s += n / p ;
        p *= 5 ;
    }
    return s ;
}

int main()
{
    int p ;
    cin >> p ;
    if ( p == 0 ) {
        cout << 1 << '\n' ;
        return 0 ;
    }
    int st = 1 ;
    int dr = 1e9 ;
    int solutie = -1 ;
    while ( st <= dr )
    {
        int mij = ( st + dr ) / 2 ;
        if ( zeros ( mij ) >= p ) {
            dr = mij - 1 ;
            solutie = mij ;
        }
        else {
            st = mij + 1 ;
        }
    }
    if ( solutie == -1 ) {
        cout << solutie << '\n' ;
    }
    else {
        while ( solutie % 5 ) {
            solutie -- ;
        }
        cout << solutie << '\n' ;
    }
    return 0;
}