Cod sursa(job #187932)

Utilizator cotofanaCotofana Cristian cotofana Data 5 mai 2008 21:05:15
Problema Factorial Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <stdio.h>
#include <math.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)
{
        int k=0;
        while (nr%5==0)
        {
                k++;
                nr/=5;
        }
        return k;
}

void fct()
{
        int i=0,t;
        /*if (n==5 || n==30 || n==155 || n==780 || n==3905 || n==19530 || n==97655 || n==488280 || n==2441405 || n==12207030 || n==61035155 || n==305175780)
        {
                nr=-1;
                return;
        } */
        while (nr<n)
        {
                i+=5;
                nr+=5;
                if (nr<n)
                {
                t=i;
                do
                {

                        if (t%5==0) t/=5;
                        nr++;
                } while (t%5==0);
                }
                else if (nr>=n && nr<n+put5(i)) {nr=-1; return;}
                else
                {
                        i-=nr-n;
                }
        }
        nr=5*i;
}

int main()
{
        cit();
        fct();
        tip();
        return 0;
}