Cod sursa(job #1196692)

Utilizator MaarcellKurt Godel Maarcell Data 8 iunie 2014 19:47:39
Problema Factorial Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <stdio.h>
using namespace std;
long long int p;
int main(){
    long long int z=0, i=1, d=0,c=0,aux;
    freopen("fact.in","r",stdin);
    freopen("fact.out","w",stdout);
    scanf("%lld", &p);
    while (z<p)
    {

        aux=i;
        while ((aux % 2) == 0){
            d++;
            aux/=2;
        }
        aux=i;
        while ((aux % 5) == 0){
            c++;
            aux/=5;
        }

        if ((d) && (c)){
            if (d>c) { d-=c; z+=c; c=0; }
            else {c-=d; z+=d; d=0; }
        }
        i++;
    }
    if (p==0) printf("1");
    else if (z==p) printf("%lld",i-1);
    else printf("-1");


    return 0;
}