Cod sursa(job #395621)

Utilizator attila3453Geiszt Attila attila3453 Data 13 februarie 2010 16:09:42
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <fstream>
using namespace std;
ifstream fi("fact.in");
ofstream fo("fact.out");
long long a[20], b[20];
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;
  if(p==0){
	fo<<1;
	return 0;
  }
  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;
	}
  }
  ls=a[i-2]+1;
  ld=a[i-1]-1;
  for(i=ls;i<=ld;){
	if(p1(i)==p){
	  fo<<i;
	  return 0;
	}
	if(p>p1(i))
	  i+=p-p1(i);
	if(p<p1(i))
	  i-=p1(i)-p;
  }
  fo<<-1;
  return 0;
}