Cod sursa(job #1879789)

Utilizator DDDECARRusu Dinu Stefan DDDECAR Data 15 februarie 2017 10:11:27
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
 
using namespace std ;
 
ifstream cin ("fact.in") ;
ofstream cout ("fact.out") ;
 
long long NrZero (long long x) {
    long long S = 0 ;
    while ( x / 5LL ) {
        S = S + (x / 5LL) ; 
        x = x / 5LL ;
    }
    return S ; 
}
 
int main()
{
    long long P ;
    cin >> P ; 
    long long st = 1 ; 
    long long dr = 2e9 ;
    long long found = -1 ; 
    while ( st <= dr ){
        long long mij = (st + dr) >> 1LL ;
        if (NrZero(mij) >= P) {
            dr = mij - 1 ; 
            found = mij ; 
        }
        else {
            st = mij + 1 ;
        }
    }
    if (found == -1 or NrZero(found) != P) {
        cout << -1 << '\n' ;
    }
    else {
        if ( found - found % 5 == 0 ) {
            cout << 1 << '\n' ;
            return 0 ; 
        }
        cout << found - found % 5 << '\n' ;
    }
    return 0 ; 
}