Pagini recente » Cod sursa (job #581867) | Cod sursa (job #1529722) | Cod sursa (job #1863870) | Cod sursa (job #2306466) | Cod sursa (job #614322)
Cod sursa(job #614322)
#include <fstream>
using namespace std;
int calc(unsigned long h);
int binary(unsigned long p);
unsigned long p,no;
int main()
{
ifstream f ("fact.in");
ofstream g ("fact.out");
f>>p;
f.close ();
no = binary(p/2);
if (!p)
g<<"1";
else if ( no==0 )
g<<"-1";
else
g<<no*5;
return 0;
}
int calc(unsigned long h)
{
unsigned long sum = h;
while (h>4)
{
sum = sum + h/5;
h = h/5;
}
return sum;
}
int binary(unsigned long n)
{
unsigned long q = calc(n);
if (n == p)
return 0;
else if (q == p)
return n;
else if (n < p )
{
if ( (n+p)%2 )
return binary( (n+p)/2+1 );
else
return binary( (n+p)/2 );
}
else if (n > p )
return binary( n/2 );
}