Pagini recente » Cod sursa (job #2385564) | Cod sursa (job #3162756)
// basic file operations
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
long n, m, P;
int suma(int j, int k)
{
long s = 0;
for (int i = 1; i <= k; i++)
s += floor(j / pow(5, i));
return s;
}
long factorial(long P)
{
int i;
long a, b, j;
if (P == 0)
{
return 1;
}
m = 1;
for (i = 1; m <= P; i++)
m += pow(5, i);
if (P == m - 1)
return -1;
else
m -= pow(5, --i);
a = ceil(P * pow(5, i) / m);
b = floor((P + i) * pow(5, i) / m);
for (j = a; j < b; j++)
if (suma(j, i) == P)
return j;
return -1;
}
int main()
{
fin >> P;
fout << factorial(P);
fin.close();
fout.close();
return 0;
}