Cod sursa(job #385007)

Utilizator remusmpRemus MP remusmp Data 21 ianuarie 2010 22:04:40
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int pow5[] = {5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625};

int nrzeros(int n)
{
     int q = (int)(log((long double)n)/1.6094);
     int nr = 0;
     for (int i = 0; i < q; i++)
     {
         nr += (int)(n/pow5[i]);
     }
     return nr;
}

int main()
{
    FILE* fin = fopen("fact.in", "r");
    FILE* fout = fopen("fact.out", "w");
    int P = 0;
    fscanf(fin, "%d", &P);
    if (P == 0)
    {
          fprintf(fout, "%d", 0);
          return 0;
    }
    int N = 5 * P;
    int Z = nrzeros(N);
    int D = 0;
    int D1 = 0;
    int D2 = 1;
    
    int done = 0;
    
    while (!done)
    {
          if (P == Z || D2 == D)
          {
                if (D2 == D)
                   N = -1;
                break;
          }
          D2 = D1;
          D1 = D;
          D = Z - P;
          N -= D*5;
          
          Z = nrzeros(N);
    }

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