Cod sursa(job #1107215)

Utilizator terrorturtle01Ion George Mihai terrorturtle01 Data 13 februarie 2014 18:37:25
Problema Factorial Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>
#define MAX 50000000
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, MAX);
        while(zero(n-1)==p){
            n=n-1;
        }
        g<<n;
    }
    return 0;
}