Cod sursa(job #980476)

Utilizator mircea.dobreanuMircea Dobreanu mircea.dobreanu Data 4 august 2013 19:16:37
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#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;
}