Cod sursa(job #2664569)

Utilizator andrei_ciobanuciobanu andrei andrei_ciobanu Data 28 octombrie 2020 20:24:00
Problema Factorial Scor 60
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <stdio.h>
#include <stdlib.h>

#define N 100000000

int five_count(int n){
    int put = 5, fives = 0;
    while (put <= n){
        fives += n / put;
        put *= 5;
    }
    return fives;
}

int find (int n){
    int min = 0, max = N, mij;
    int fives;
    while (max - min > 1){
        mij = (max + min) / 2;
        fives = five_count(mij);
        if (n < fives){
            max = mij - 1;
        }else if (n > fives){
            min = mij + 1;
        }else{
            min = mij;
            max = 0;
        }
    }
    if (n == five_count(min) || n == five_count(max)) return min;
    else return -1;
}

int main()
{
    FILE *fin, *fout;
    fin = fopen("fact.in", "r");
    fout = fopen("fact.out", "w");
    int n;
    fscanf(fin, "%d", &n);
    if (n == 0){
        fprintf(fout, "1");
    }else{
        int x = find(n);
        if (x == -1) fprintf(fout, "%d", x);
        else {
            x = x - (x % 5);
            fprintf(fout, "%d", x);
        }
    }
    return 0;
}