#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream.h>
#include <string.h>
#include <cmath>
#include <limits>
#include "lcgamm.h"
using namespace std;

float gcf(const float a, const float x);
float gammln2(const float xx);

int main() {
  double a, x;
  cin >> a;
  cin >> x;
  cout << lcgf(a, x);
  exit(0);
  // Goal: compare the conditions
  // erfc(x) < e^-26, y > n + x sqrt(2 n)
  // lcgf(n, y) < -22  (or -26)
  // for n = 137
  int n = 10000;
  for(int i = n + 2; i < 2 * n; i++) {
    if(lcgf(n, i) < -26) {
      printf("lcgf(%d, %d) = %f\n", n, i, lcgf(n, i));
      break;
    }
  }
  for(float x = 2.0; x < 100.0; x+=0.000001) {
    if(lcgf(0.5, x*x) < -22) {
      printf("lcgf(%f, %f^2) = %f\n", 0.5, x, lcgf(0.5, x*x));
      printf("threshold = %f\n", n + x * sqrt(2 * n));
      break;
    }
  }
 /*
  float a, x;
  a = 4489216; x = 4500000;
  printf("gcf(%f, %f) = %f\n", a, x, gcf(a, x));
  printf("lcgf(%f, %f) = %f\n", a, x, lcgf((double)a, (double)x));
  */
  /* float num_dm_chunk_large = 111;
  float dm_chunk_large = 128;
  float num_signs = 2;
  float num_bins = 32 * 1024 * 1024;
  float num_workunits = 400;
  float num_potential_hits = num_dm_chunk_large * 
	dm_chunk_large * num_signs * num_bins * num_workunits;  
  int fold_log = 0;
  for(int fold = 1; fold < 1024; fold *= 2) {
    for(int pow = fold; pow < 1500; pow++) {
      float log_lower = lcgf(fold, pow) + log(num_potential_hits);
      float log_upper = lcgf(fold, pow + 1) + log(num_potential_hits);
      float num_per_power_interval = exp(log_lower) - exp(log_upper);
      // log_num_per_power_interval = 
      //      log_lower + log(1 - exp(log_upper - log_lower));
      printf("%d %d %f\n", fold_log, pow, num_per_power_interval);
    }
    fold_log++;
  }
  */
} 

float gammln2(const float a) 
{
	double x,y,tmp,ser;
	static double cof[6]={76.18009172947146,-86.50532032941677,
                        24.01409824083091,-1.231739572450155,
                        0.1208650973866179e-2,-0.5395239384953e-5};

	int j;
	y=x=a;
	tmp=x+5.5;
	tmp -= (x+0.5)*log(tmp);
        ser=1.000000000190015;
	for (j=0;j<=5;j++) ser += cof[j]/++y;
        return (float)(-tmp+log(2.5066282746310005*ser/x));
}

float gcf(const float a, const float x) 
{
	int i;
	float an, b, c, d, del, h, gln=gammln2(a);
	const int ITMAX = 1000;
	const float EPS = 1.0e-35;
	const float FPMIN = 1.0e-35;
	// const float EPS=numeric_limits<float>::epsilon();
	// const float FPMIN=numeric_limits<float>::min()/EPS;
	// printf("%f\n", log(numeric_limits<float>::epsilon()) / log(10));

	b=x+1.0f-a;
	c=1.0f/FPMIN;
	d=1.0f/b;
	h=d;
	for(i=1; i<=ITMAX;i++) {
	  an = -i*(i-a);
	  b += 2.0f;
	  d = an*d+b;
	  if(fabs(d) < FPMIN) d = FPMIN;
	  c = b+an/c;
	  if(fabs(c) < FPMIN) c=FPMIN;
	  d=1.0/d;
	  del=d*c;
	  h *= del;
	  if (fabs(del-1.0) <= EPS) break;
	}
	if(i > ITMAX) printf("a too large, ITMAX too small in gcf\n");
	double logh = log(h);
        double logx = log(x);
	double result = (logh-x+a*logx-gln);
	return (float) (logh-x+a*logx-gln);
}
