Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

The Cat in the Hat

107.c

/* The Cat in the Hat */
#include <stdio.h>
#include <math.h>

/*
 * w = N^a
 * h = (N+1)^a
 * 
 */

int h, w;

void
calc(void)
{
	int N,a;
	double f,g;
	double dist = 1000000.0;
	int i,n;

	if (w==1 && h==1) {
		printf("0 1\n");
		return;
	}

	f = log(w)/log(h);
	for(N=1; ; N++) {
		double t;
		g = log(N)/log(N+1);

		t = fabs(f-g);
		if (t < dist) {
			dist = t;
		} else {
			N--;
			break;
		}
	}
	a = log(h) / log(N+1) + 0.3;
#if DEBUG
	printf("N=%d, a=%d\n", N, a);
#endif
	n=0;
	for (i=0; i<a; i++) {
		n *= N;
		n++;
	}
/*	printf("%u %u\n",(w-1)/(N-1),h*(N+1)-N*w); */
	printf("%u %u\n",n,h*(N+1)-N*w);
}

int
main(void)
{
	while(1) {
		if (scanf("%d %d", &h, &w)!=2) {
			return 1;
		}
		if (h==0 && w==0) {
			return 0;
		}
		calc();
	}
	return 0;
}