Cod sursa(job #1563694)

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

typedef long long ll;
ll a,b,c,p,i,fn;
bool ok=false;

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

int main()
{
    cin>>p;
    if(p==0)
    {
        cout<<"1";
        return 0;
    }
    if (p<5) {cout<<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) cout<<i;
    else cout<<"-1";
    return 0;
}