Cod sursa(job #642280)

Utilizator penultim_oVijiala Tudor Gabriel penultim_o Data 30 noiembrie 2011 21:00:12
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>
#include <iostream>
#define L unsigned long
using namespace std;

L f(L x) //f(x) = exponentul lui 5 din descompunerea x!
{
    L e=0;
    while(x)
    {
        e+=x/5;
        x/=5;
    }
    return e;
}

int main()
{
    ifstream in("fact.in");
    ofstream out("fact.out");
    L min,max,p,mid,e;

    in >> p;
    if(p==0){out << 1; return 0;}
    min=0;max=-1;

    while(min<max)
    {
        mid = (min+max)/2;
        e = f(mid);
        if(e>p) max = mid-1;
        else if(e<p) min = mid+1;
        else
        {
            out << (mid - mid%5);
            return 0;
        }
    }

    out << -1;
    return 0;
}