Cod sursa(job #1700225)

Utilizator mihai.constantinConstantin Mihai mihai.constantin Data 9 mai 2016 20:49:43
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream in("fact.in");
ofstream out("fact.out");

#define L 30

long P;

/* PRIMESC UN NR x. AFLU CU CATE ZEROURI SE TERMINA x! i.e. exp LUI 5 ( T. LEGENDRE ) */
int zero(long x)
{
    int exp = 0;

    while(x >= 5)
    {
        x /= 5;
        exp += x;
    }

    return exp;
}

int Binary_Search()
{
    int i, pas;

    i = 0;
    pas = 1 << L;

    while(pas != 0)
    {
        if(zero(i + pas) < P) i += pas;

        pas /= 2;
    }

    if(zero(i + 1) > P) return -1;
    else
        return i + 1;
}

int main()
{
    in >> P;

    out << Binary_Search();


    return 0;
}