Cod sursa(job #1937952)

Utilizator TornerroRusu Marius Tornerro Data 24 martie 2017 14:29:36
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");

int nr0(int x)
{
    int p=5,s=0;
    while(p<=x)
    {
        s=s+x/p;
        p*=5;
    }
    return s;
}

int main()
{
    int i,p,a=1,b=INT_MAX-1,x=0;
    fin>>p;
    if(p==0)fout<<1;
    else
    {
        while(x!=p)
        {
            if(b-a<5)break;
            i=(a+b)/2;
            x=nr0(i);
            if(x<p)a=i+1;
            else if(x>p)b=i-1;
        }
        i=i-i%5;
        if(x!=p)fout<<-1;
        else fout<<i;
    }
    return 0;
}