Cod sursa(job #2378251)

Utilizator butnaru_vlad2003Butnaru Vlad butnaru_vlad2003 Data 11 martie 2019 21:37:58
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>
using namespace std;
ifstream in ("fact.in");
ofstream out ("fact.out");
int main (void)
{
    unsigned long long a,l=0,best=1;
    in>>a;
    int r=a*5;
    if (a==0)
    {
        out<<'1';
        return 0;
    }

    while (l<=r)
    {
        int mid=(l+r)/2,sol=0;
        for (int d=5;d<=mid;d*=5)
            sol+=mid/d;
        if (sol>=a)
        {
            best=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    int x=0,d=5;
    while (d<=best)
    {
        x+=best/d;
        d*=5;
    }
    if (x==a)
        out<<best;
    else
        out<<-1;
    return 0;
}