Pagini recente » Cod sursa (job #646556) | Cod sursa (job #2041450) | Cod sursa (job #2999904) | Cod sursa (job #748942) | Cod sursa (job #980476)
Cod sursa(job #980476)
#include<fstream>
using namespace std;
int zero(int x)
{
int nr=0,d=5;
while (d<=x)
{
nr+=x/d;
d*=d;
}
return nr;
}
int binary_search(int low,int high,int value)
{
int mid,nr_zero;
while (low<=high)
{
mid=low+((high-low)>>1);
nr_zero=zero(mid);
if (nr_zero==value)
{
int aux=mid-1;
while (zero(aux)==value)
--aux;
return aux+1;
}
else if (value<nr_zero)
high=mid-1;
else if (value>nr_zero)
low=mid+1;
}
return -1;
}
int main()
{
int p;
ifstream fin("factorial.in");
fin>>p;
fin.close();
ofstream fout("factorial.out");
fout<<binary_search(1,1<<30,p);
fout.close();
return 0;
}