Cod sursa(job #2037148)

Utilizator Alex_dudeDudescu Alexandru Alex_dude Data 11 octombrie 2017 19:57:23
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <stdio.h>
#include <math.h>
#define nmax 400000015
using namespace std;

int count5(long  fact)
{
    long  i=5;
    long zeros = 0;

    while(fact/i!=0)
    {
        zeros+=fact/i;
        i*=5;
    }

    return zeros;
}

int bs(long x)
{
    int hi=nmax+1, lo=-1, mid;

    while(hi-lo>1)
    {
        mid=(lo+hi)/2;
        if(count5(mid)<x)
            lo=mid;
        else
            hi=mid;
    }
    if(hi==nmax+1||count5(hi)!=x)
        return -1;
    return hi;
}

FILE *fin, *fout;

int main()
{
    fin = fopen("fract.in","r");
    fout = fopen("fract.out", "w");

    long  nr;

    fscanf(fin, "%ld", &nr);

    if(nr<0)
        fprintf(fout, "-1");
    else if(nr<5)
        fprintf(fout, "1");
    else

    fprintf(fout, "%ld", bs(nr));
}