Cod sursa(job #424849)

Utilizator hendrikHendrik Lai hendrik Data 25 martie 2010 11:38:20
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;
const ll oo=(ll)1e18;
ll lo,hi,mid,ans,p;

void open(){
    freopen("fact.in","r",stdin);
    freopen("fact.out","w",stdout);
}

ll cnt(ll a){
    ll res=0;
    while (a){
        res+=a/5;
        a/=5;
    }
    return res;
}

int main(){
    open();
    scanf("%lld",&p);
    ans=-1;
    lo=1;hi=oo;
    while (1){
        mid=(lo+hi)>>1;
        ll tmp=cnt(mid);
        if (tmp<p){
            lo=mid+1;
        }
        else {
            if (tmp==p) ans=mid;
            hi=mid-1;
        }
        if (lo>hi) break;
    }
    printf("%lld\n",ans);
    //system("pause");
    return 0;
}