Cod sursa(job #1480923)

Utilizator dorumusuroiFMI - Doru Musuroi dorumusuroi Data 3 septembrie 2015 13:59:08
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <cstdio>

const char iname[] = "fact.in";
const char oname[] = "fact.out";
const int INF = (1 <<31) -1;
const int MAXP = 100000000;

int getZeros(int n){
    int zeros = 0;
    while(n){
        zeros += n/5;
        n/=5;
    }
    return zeros;
}

int main()
{
    freopen(iname, "r", stdin);
    freopen(oname, "w", stdout);
    int p, a, b, mid;
    scanf("%d", &p);
    a = 1, b = 400000015;
    while(a < b){
        mid = a + ((b-a)>>1);
        int zeros = getZeros(mid);
        if(zeros == p)
            b = mid;
        else if(zeros < p)
                a = mid + 1;
            else b = mid - 1;
    }
    if(getZeros(a) != p)
        printf("-1");
    else
        printf("%d", a);
    return 0;
}