Cod sursa(job #787083)

Utilizator nicuvladNicu Vlad nicuvlad Data 12 septembrie 2012 16:30:42
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <math.h>
#define dim 100000100
using namespace std;

typedef long long tip;
tip a,b,c,p,i,fn;
bool ok=false;
ifstream fin("fact.in");
ofstream fout("fact.out");

tip get(tip c)
{   tip k=0;
    while(c>0)
        {
            k+=c/5;
            c/=5;
        }
    return k;
}

int main()
{
    fin>>p;
    if(p==0)
    {
        fout<<"1";
        return 0;
    }
    a=1;
    b=5*p;
    c=(a+b)/2;
    c=c-c%5;
    while(a<=c&&c<=b&&a<=b&&!ok)
    {
        fn=get(c); //get(c) returneaza zerourile pe care le are la sfarsit c!
        if(fn==p)
        {
            ok=true;
            i=c;
        }
        else if(fn<p) a=c+1;
            else  b=c-1;
        c=(a+b)/2;
        c=c-c%5;
    }
    if(ok) fout<<i;
    else fout<<"-1";
    return 0;
}