Cod sursa(job #1563606)

Utilizator xtreme77Patrick Sava xtreme77 Data 6 ianuarie 2016 12:57:45
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <cstdio>

using namespace std;
const int MAX=2000000000;
int fact(int x);

int main()
{
    freopen("fact.in","r",stdin);
    freopen("fact.out","w",stdout);
    int p;

    scanf("%d",&p);
    if(p==0)
    {
        printf("1\n");
        return 0;
    }

    int st=1,dr=MAX,mid,prec;

    bool ok=false;

    while(st<=dr and !ok)
    {
        mid = st+(dr-st)/2;
        prec=fact(mid);
        if(prec==p)ok=true;
        if(prec<p)st=mid+1;
        else dr=mid-1;
    }
    if(ok)
    {
        while(mid%5)mid--;
        printf("%d\n",mid);
    }
    else
        printf("-1\n");
    return 0;
}
int fact(int x)
{
    int a=5,rez=0;
    while(x/a)
    {
        rez+=x/a;
        a*=5;
    }
    return rez;
}