Cod sursa(job #187990)

Utilizator cotofanaCotofana Cristian cotofana Data 5 mai 2008 22:27:18
Problema Factorial Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <stdio.h>   
  
long n;   
long nr;   
  
void cit()   
{   
        FILE *f=fopen("fact.in", "r");   
        fscanf(f, "%ld", &n);   
        fclose(f);   
}   
  
void tip()   
{   
        FILE *f=fopen("fact.out", "w");   
        fprintf(f, "%ld\n", nr);   
        fclose(f);   
}

int put5(int nr)
{
        while (nr%5==0) nr/=5;
        if (nr==1) return 1;
        return 0;
}
  
void fct()   
{   
        int i=0,t, der1, der;
        while (nr<n)   
        {   
                i+=5;   
                nr+=5;   
                if (nr<n)   
                {   
                t=i;
                do  
                {
                        if (t%5==0) t/=5;   
                        nr++;
                        der1++;
                        //if (max>der) der=max;
                } while (t%5==0);
                }
                else if (nr==n)
                {
                        if (put5(i))
                        {
                                der=0;
                                t=i;
                                while (t%5==0)
                                {
                                        t/=5;
                                        der++;
                                }
                                if (nr>=i+der1 && nr<n+der)
                                {
                                        nr=-1;
                                        return;
                                }
                        }
                }
                else  
                {   
                        i-=nr-n;   
                }   
        }
        if (nr!=n)
        {
                nr=-1;
                return;
        }
        nr=5*i;
        if (n==0) nr=1;
}   
  
int main()   
{   
        cit();   
        fct();   
        tip();   
        return 0;   
}