Cod sursa(job #472698)

Utilizator andrey932Andrei andrey932 Data 26 iulie 2010 13:02:29
Problema Factorial Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.54 kb
#include <iostream>
#include <fstream>
using namespace std;

fstream fin("fact.in",ios::in),fout("fact.out",ios::out);
int n,lo,hi,p,x;

int zero(int n) {
	int rez=0;
	for(int imp=5; imp<=n;imp*=5) {
		rez+=n/imp;
	}
	return rez;	
}

int main()
{
	fin>>n;
	lo=5; hi=100000000;
	x=-1;
	while (lo+1<hi) {
		p=zero((lo+hi)/2);
		if (n>p) lo=(lo+hi)/2;
		else if (n==p) {x=(lo+hi)/2; break;}
		else if (n<p) hi=(lo+hi)/2;
		else break;
	}
	while (x%5!=0) x--;
	if (x==-5) x=1;
	fout<<x<<"\n";
	fout.close();	
	return 0;
}