Cod sursa(job #1027505)

Utilizator morlockRadu Tatomir morlock Data 12 noiembrie 2013 20:19:03
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 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 long fact(long long N)
{ long long a, b;

    a = N / 5;
    b = log(a)/log(5);

    return b;

}

int main()
{
    in >> P;

    if ( !P ) out << 1;
    else {

    step = 1ll<<30;
    for ( ; step >= 5; step >>= 1 )
    {
        N = step;
        Z = N / 5;
        nr = log(Z)/log(5);
        Z += nr;

       // cout << N << " " << fact(N) << " " << Z << '\n';

        if ( Z <= P )
        {
            if ( Z == P )  { out << N - (N % 5); break; }
                else
                {
                    N = N + (P - Z)*5;
                    if ( nr != fact(step) ) N -= 5;
                    out << N - (N % 5);


                    break;
                }
        }

    }

    }
return 0;
}