Cod sursa(job #1681357)

Utilizator popabogdanPopa Bogdan Ioan popabogdan Data 9 aprilie 2016 13:28:31
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <iostream>
#include <fstream>
#include <climits>
#define ll long long
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int p;
ll nz(ll n)
{
    ll i=5,s=0;
    while(i<=n)
    {
        s=s+n/i;
        i=i*5;
    }
    return s;
}
ll caut(int p)
{
    ll s=1,d=LLONG_MAX-3,m,best=0;
    while(s<=d)
    {
        m=(s+d)>>1;
        if(nz(m)>=p)
        {
            best=m;
            d=m-1;
        }
        else s=m+1;
    }
    if(nz(best)!=p)return -1;
    return best;
}
int main()
{
    fin>>p;
    fout<<caut(p);
    return 0;
}