Cod sursa(job #793528)

Utilizator cristiprgPrigoana Cristian cristiprg Data 3 octombrie 2012 14:33:36
Problema Factorial Scor 55
Compilator c Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <stdio.h>

int f(int x)
{
    if (x < 5)
        return 0;

    int i = 5, putere = 0;
    while(i <= x)
    {
        putere += x / i;
        i *= 5;
    }

    return putere;

}

int bin_search(int left, int right, int P)
{


    if(right - left <= 1)
    {
        if (P < f(right))
            return -1;//nu exista
        return right;
    }


    int middle = (left + right) / 2;
  //  printf("%d %d %d\nf(%d) = %d\n", left, middle,  right, middle, f(middle));

    if ( f (middle) >= P)
        return bin_search(left, middle, P);
    else
        return bin_search(middle, right, P);

}

int main()
{
    FILE *f = fopen("fact.in", "r");
    int P;
    fscanf(f, "%d", &P);
    fclose(f);

    f = fopen("fact.out", "w");
    fprintf(f, "%d\n", bin_search(1, 100000000, P));
    fclose(f);




    return 0;
}