Cod sursa(job #561961)

Utilizator cernat.catallinFMI Cernat Catalin Stefan cernat.catallin Data 22 martie 2011 01:17:41
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <fstream>
using namespace std;
int numarzerouri(int n)
{   int fact=0;
    while(n) {fact+=n/5; n/=5;}
    return fact;
}
int main()
{
    ifstream f ("fact.in");
    ofstream g ("fact.out");
    unsigned long long n,a,b,p;
    bool gasit=false;
    f>>p;
    f.close();
    if(p==0) g<<1;
    else
    {
        a=1;
        b=10000000000;
        while(a<=b && !gasit)
        {
            n=(a+b)/2;
            if(numarzerouri(n)==p) gasit=true;
            else if(numarzerouri(n)>p) b=n-1;
            else a=n+1;
        }
        if(gasit==true)
        {
            while(n%5!=0) n--;
            g<<n;
        }
        else g<<-1;
    }
}