Cod sursa(job #1107173)

Utilizator terrorturtle01Ion George Mihai terrorturtle01 Data 13 februarie 2014 17:51:26
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
using namespace std;
int P, nrz, N;

int zero(int k){
    long m = 5, z = 0;
    while(k/m != 0){
        z+=k/m;
        m*=5;
    }
    return z;
}

int bin_search(int a, int b){
    int c;
    c = (a+b)/2;
    if(zero(c) < P){
        bin_search(c+1, b);
    }else if(zero(c) > P){
        bin_search(a, c-1);
    }else{
        return c;
    }
}

int main()
{
    ifstream f("fact.in");
    ofstream g("fact.out");
    f>>P;
    if(P==0){
        g<<1;
    }else if(P==5){
        g<<-1;
    }else{
        long n = bin_search(0, 500000000);
        while(zero(n-1)==P){
            n=n-1;
        }
        g<<n;
    }
    return 0;
}