Cod sursa(job #1027635)

Utilizator morlockRadu Tatomir morlock Data 12 noiembrie 2013 21:37:34
Problema Factorial Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

ifstream in("fact.in");
ofstream out("fact.out");

long long P, step, N, Z, temp, nr, i, nr1;

long fact(long long N)
{ long long Z = 0, temp = 5;

    while ( temp <= N )
    {
        Z += N / temp;
        temp *= 5;
    }

   // if

    return Z;
}

int main()
{
    in >> P;

    if ( !P ) out << 1;

    else
    {
        step = 1ll<<30;
        for ( i = 0; step >= 5; step >>= 1 )
        {
           //cout << step << " " << fact(step) << '\n';
            if ( fact(i+step) <= P )
            {
               i += step;
            }
        }

    }

    if ( fact(i) != P ) out << -1;
        else out << i - (i % 5);
return 0;
}