Cod sursa(job #1563695)

Utilizator IstrateSebastianIstrate Sebastian IstrateSebastian Data 6 ianuarie 2016 14:41:41
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#include <math.h>
#define dim 100000100
using namespace std;

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

ll nrz(ll c)
{   ll k=0;
    while(c>0)
        {
            k+=c/5;
            c/=5;
        }
    return k;
}

int main()
{
    fin>>p;
    if(p==0)
    {
        fout<<"1";
        return 0;
    }
    if (p<5) {fout<<5*p; return 0;}
    a=1;
    b=5*p;
    c=(a+b)/2;
    c-=c%5;
    while(a<=c&&c<=b&&a<=b&&!ok)
    {
        fn=nrz(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%5;
    }
    if(ok) fout<<i;
    else fout<<"-1";
    return 0;
}