I have posted replies to several people on this problem. I have ran my program against several inputs, all from this site, and my program answers every one of them correctly. I have tested this program with every possible set of data I can find, or make up, including the extreme cases. However, the online judge says that I have an incorrect answer ("Wrong Answer"). If you have solved this problem, let me know what is wrong with mine and give me some kind of clue as to what is wrong, because I just cannot see it.
[cpp]//---------------------------------------------------------------------------
// Ron Buchanan
// ACM Volume I - #107
// "The Cat in the Hat"
//---------------------------------------------------------------------------
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define DEBUG 0
void findTheRoot();
long bigCatHeight;
long workerCats;
long theRoot;
double thePower;
long theLazyCats;
long theStackHeight;
long theHeight;
long cats;
void main()
{
while (true) {
scanf("%ld %ld", &bigCatHeight, &workerCats);
if (bigCatHeight == 0 && workerCats == 0) break;
if (bigCatHeight == 1) {
theLazyCats = 0;
theStackHeight = workerCats;
}
else {
// initialize
theRoot = 1;
thePower = log(bigCatHeight)/log(workerCats);
theLazyCats = 1;
theStackHeight = bigCatHeight;
// try to somewhat brute-force the root
findTheRoot();
#if DEBUG
printf("%ld^%f\n", theRoot, thePower);
#endif
// calculate the height of the stack and the number of lazy cats
theHeight = bigCatHeight / (theRoot+1);
cats = 1;
while (theHeight > 1) {
cats = cats * theRoot;
theLazyCats += cats;
theStackHeight += (long)(theHeight*cats);
theHeight = theHeight / (theRoot+1);
#if DEBUG
printf("%ld %ld %ld %f\n", cats, theLazyCats, theStackHeight, theHeight);
#endif
}
theStackHeight += workerCats;
/* OLD
theLazyCats = (long)((pow(theRoot, thePower+1) - 1)/(theRoot - 1)
- workerCats);
theStackHeight = 0;
for (int i = 0; i <= thePower; i++)
theStackHeight +=
(long)(pow(theRoot, i)*pow(theRoot+1, thePower-i));
*/
}
printf("%ld %ld\n", theLazyCats, theStackHeight);
}
}
//---------------------------------------------------------------------------
// attempt to brute-force the root and power
//---------------------------------------------------------------------------
void findTheRoot() {
while (log(theRoot+1)/log(theRoot) - thePower > 1e-15) {
theRoot++;
}
thePower = (long)(log(workerCats)/log(theRoot));
}[/cpp]
Any and all help would be greatly appreciated.

[/cpp]