Cod sursa(job #157748)

Utilizator rethosPaicu Alexandru rethos Data 13 martie 2008 11:22:44
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.54 kb
#include <fstream.h>
long nrz(long long x)
{ long nr=0;
  while (x)
	{ x=x/5;
	  nr+=x;
	}
  return nr;
}
int main()
{ long long st=5,dr=1000000000;
  long long mij;
  long p,f;
  ifstream fin("fact.in");
  fin>>p;
  fin.close();
  ofstream g("fact.out");
  if (p==0) { g<<1;g.close();return 0;}
  while (st<=dr)
	{ mij=(st+dr)/2;
	  f=nrz(mij);
	  if (f==p) break;
	  if (f<p) st=mij+1;
		else dr=mij-1;
	}
  if (st<=dr) //sa gasit un mij
	{ while (mij%5!=0) mij--;
	  g<<mij;
	}
	else g<<-1;
  g.close();
  return 0;
}