Cod sursa(job #2075743)

Utilizator alexge50alexX AleX alexge50 Data 25 noiembrie 2017 17:21:50
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <stdio.h>

int getNumberOf0(int i);

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

    fscanf(fin, "%d", &p);

    int step = 1 << 30;
    int r = 0;

    while (step != 0)
    {
        if(getNumberOf0(r + step) < p)
            r += step;
        step /= 2;
    }

    if(getNumberOf0(r + 1) == p)
        fprintf(fout, "%d", r + 1);
    else fprintf(fout, "-1");

    fcloseall();
    return 0;
}

int getNumberOf0(int n)
{
    int p = 5;
    int n0 = 0;
    while(p <= n)
    {
        n0 += n / p;
        p *= 5;
    }

    return n0;
}