Cod sursa(job #1107219)

Utilizator terrorturtle01Ion George Mihai terrorturtle01 Data 13 februarie 2014 18:41:08
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.64 kb
#include <fstream>
using namespace std;
long P;

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){
    long 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{
        long n = bin_search(0, 500000000);
        while(zero(n-1)==P){
            n=n-1;
        }
        g<<n;
    }
    return 0;
}