Pagini recente » Cod sursa (job #1943233) | Cod sursa (job #688060) | Cod sursa (job #2396685) | Cod sursa (job #2289417) | Cod sursa (job #1465872)
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
unsigned long int p;
int Caut(unsigned long long int a, unsigned long long int b)
{
unsigned long long int mid, k=1, pw = 0, pw1 = 0, pw2 = 0;
//Find the middle point
mid = (a + b) / 2;
//Find the number of 5s
while (k * 5 <= mid + 5){
k = k * 5;
pw = pw + mid / k;
pw1 = pw1 + (mid - 5) / k;
pw2 = pw2 + (mid + 5) / k;
}
//Return the result if it is what we are searching
if (pw == p){
return mid-mid%5;
}
else
{
if ((pw > p && pw1 < p) || (pw < p && pw2 > p)) return -1;
//Continue searching
if (pw < p)
return Caut(mid+1,b);
else return Caut(a,mid-1);
}
}
int main()
{
ifstream f1("fact.in");
ofstream f2("fact.out");
f1 >> p;
if (p == 0) f2 << 1;
else f2 << Caut(0,pow(10,8));
return 0;
}