Cod sursa(job #1156549)

Utilizator xtreme77Patrick Sava xtreme77 Data 27 martie 2014 19:26:11
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <cstdio>

using namespace std;
const int MAX=1000000000;
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;
    }

    bool ok=false;
    int dr=fact(MAX);

    int i, step;
    for (step = 1; step <= dr; step <<= 1);
    for (i = 0; step; step >>= 1)
        if (fact(i+step)<= dr and fact(i+step)<=p){
           if(fact(i+step)==p)ok=true;
           i += step;
        }
    if(i and ok)
    {
        while(i%5)i--;
        printf("%d\n",i);
    }
    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;
}