Cod sursa(job #1416621)

Utilizator VasilescuVasilescu Eliza Vasilescu Data 8 aprilie 2015 16:05:06
Problema Factorial Scor 90
Compilator c Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <stdio.h>

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

    int p, gasit, cn, n, e2, e5, k,r, x, ls, ld;

    fscanf(fin, "%d", &p);

    if(p==0){
        n=1;
    }
    else{
        ls=5;
        ld=400000100;
        gasit=0;

        while(gasit==0){
            n=(ld+ls)/2;
            x=5;
            r=0;
            while(x<=n){
               x*=5;
            }
            x=x/5;

            while(x>=5){
                r=r+n/x;
                x/=5;
            }

            if(r==p){
                gasit=1;
            }
            else{
                if(r>p){
                    ld=(ld+ls)/2;
                }
                else
                    ls=(ld+ls)/2;
            }


        }
        n=n-n%5;
    }

    fprintf(fout, "%d", n);
    fclose(fin);
    fclose(fout);
    return 0;
}