Cod sursa(job #93645)

Utilizator igorPirnau Igor igor Data 19 octombrie 2007 18:56:51
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include<fstream.h>

ifstream f("fact.in");
ofstream g("fact.out");
unsigned long a[]={5,30,155,780,3905,19530,97655,488280,2441405,12207030,61035155,305175780};

int main()
{ long long n,p,st,dr,sum,ok,k,i;
  f>>p;
  f.close();
  if(p==0){ g<<"1"; g.close(); return 0; }
  if(p==1){ g<<"5"; g.close(); return 0; }
  for(i=0;i<12;i++) if(p==a[i]) { g<<"-1"; g.close(); return 0; }  
  ok=1;
  st=0;
  dr=(long long)1<<30;
  while(ok){ n=(st+dr)>>1;
             sum=0;
             for(k=5;n>=k;k=k*5) sum=sum+n/k;
             if(sum>p) dr=n;
               else if(sum<p) st=n;
                      else ok=0;
                }
  n=n-n%5;
  g<<n;                 
  g.close();
  return 0;
}