Cod sursa(job #395597)

Utilizator attila3453Geiszt Attila attila3453 Data 13 februarie 2010 15:25:53
Problema Factorial Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
using namespace std;
ifstream fi("fact.in");
ofstream fo("fact.out");
long a[16], b[16];
int p1(int n){
  int p=0, i;
  long nr=5;
  for(i=1;i<=16;i++)
	if(nr<=n){
	  p+=n/nr;
	  nr*=5;
	}
	else break;
  return p;
}
int main(){
  int p, i=1, ls, ld;
  fi>>p;
  a[0]=0;
  a[1]=25;
  b[0]=1;
  while(p>b[i-1]){
	if(i>1)
	  a[i]=a[i-1]*5;
	b[i]=b[i-1]*5+1;
	i++;
	if(b[i-1]==p){
	  fo<<a[i-1];
	  return 0;
	}
  }
  i--;
  ls=a[i-1]+1;
  ld=a[i]-1;
  if(p-b[i-1]<b[i]-p){
	for(i=ls;i<=ld;i++)
	  if(p1(i)==p){
		fo<<i;
		return 0;
	  }
	fo<<-1;
	return 0;
  }else{
	for(i=ld;i>=ls;i--)
	  if(p1(i)==p){
		fo<<i;
		return 0;
	  }
	fo<<-1;
	return 0;
  }
}