Cod sursa(job #2190369)

Utilizator razviii237Uzum Razvan razviii237 Data 30 martie 2018 17:12:16
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <cstdio>

#define fs fscanf
#define fp fprintf

using namespace std;

FILE *f, *g;

int p;

typedef long long lint;

int works(lint x, int p)
{
    ///return -1; // daca x! are mai putine zerouri
    ///return 1; // daca x! are mai multe zerouri
    lint nr0 = x / 5;
    nr0 += x / 25;
    if(nr0 > p)
        return 1;
    if(nr0 < p)
        return -1;
    if(nr0 == p)
        return 0;
}

lint bsearch(int x)
{
    lint p = 1LL << 40;
    lint r = 0;
    while(p)
    {
        if(works(5*(p + r), x) < 0)
            r+=p;
        p = p >> 1;
    }
    if(works((r+1) * 5, x) == 0)
        return (r+1)*5;
    else
        return -1;
}

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

    fs(f, "%d", &p);

    if(p == 0)
        fp(g, "1");
    else
    {
        lint rez = bsearch(p);
        fp(g, "%lld", rez);
    }

    fclose(f);
    fclose(g);
    return 0;
}