Cod sursa(job #3259722)

Utilizator lessan98leonard savu lessan98 Data 27 noiembrie 2024 17:23:46
Problema Factorial Scor 80
Compilator c-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <stdio.h>
#include <stdlib.h>

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

    int p, start = 0, cstart, i = 0;
    //precomputed arrays
    int pow_of_5[13] = {5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625};
    int count_pow_of_5[13] = {1, 6, 31, 156, 781, 3906, 19531, 97656, 488281, 2441406, 12207031, 61035156};
    fscanf(r, "%d%d", &p);

    if(p == 0){
        fprintf(w, "1");
        return 0;
    }

    while(p > count_pow_of_5[i]){
        i++;
    }
    i--;
    p -= count_pow_of_5[i];
    start = pow_of_5[i];

    while(p > 0){
        start += 5;
        cstart = start;
        while(cstart%5 == 0){
            cstart /= 5;
            p--;
        }
    }

    if(p == 0){
        fprintf(w, "%d", start);
        return 0;
    }

    fprintf(w, "-1");
    return 0;
}