Cod sursa(job #395605)

Utilizator attila3453Geiszt Attila attila3453 Data 13 februarie 2010 15:55:29
Problema Factorial Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 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;
  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;
  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;
}