Cod sursa(job #1249262)

Utilizator lupuflaviu9lupuflaviu lupuflaviu9 Data 26 octombrie 2014 19:20:16
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>
 
using namespace std;
 
ifstream in("fact.in");
ofstream out("fact.out");
 
const int RMAX = (1<<29);
 
int N,P;
 
int FCT( int nr ) {
    int R= 0, step= 5;
    while( step <= nr ) {
        R+= nr/step;
        step*= 5;
    }
    return R;
}
 
int Bin_search( int val ) {
    int step= 1;
    step= RMAX;
    int i= 0;
    for( ; step;  step>>= 1 ) {
        if( i+step <= RMAX && FCT(i+step)<val ) {
            i+= step;
        }
    }
    return i+1;
}
 
int main() {
    in >> N;
    int sol= Bin_search( N );
    if( FCT(sol) == N ) out << sol;
    else out << "-1";
    out << '\n';
    return 0;
}