Template:Problem

106 - The Cat in the Hat

 * http://acm.uva.es/p/v1/107.html

Summary
We need to find two integer numbers N and M, where N is the number of cats inside each (non-smallest) cat's hat and M is the number of cats with different heights.

Explanation
In order to solve this problem we have to find the two integers N and M. We consider the height of the first cat = A and the number of working cats = B. Using some simple math logics we can write this equation:

( N / (N + 1) ) ^ (M - 1) = (B / A)

We search M in the interval (2, HighValue) and binary search for R such that R ^ (M - 1) = B. We do the same for T such that T ^ (M - 1) = A. If T = R + 1 then we find N = R. With the curent values of N and M we compute the output numbers:

1 + N + N ^ 2 + ... + N ^ (M - 2)

and

A * (1 + 1 / (N + 1) + 1 / (N + 1) ^ 2 + ... + 1 / (N + 1) ^ (M - 1))

Gotchas
Take care of "1 1" input case. Solving this case doesn't work with the upmentioned algorithm.

Implementations
Notes/Hints on actual implementation here.

Optimizations
Optimizations here.

Input
Input Here

Output
Output Here