UVa 10323 - Factorial! You Must be Kidding!!!

10323 - Factorial! You Must be Kidding!!!

 * http://acm.uva.es/p/v103/10323.html

Summary
The concept behind the problem is quite simple - given $$n!$$, if it is greater than 6227020800, then print "Overflow!", if it is less than 10000, print "Underflow!", and otherwise print $$n!$$. Unforunately, there's a little trick..

Explanation
Though negative factorial are normally undefined this problem stretches the limit of well-known definitions.

For this problem, we have $$F(n) = n * F(n-1)$$, and $$F(0) = 1$$. With some manipulations, for negative factorials, we can get: $$F(0) = 0 * F(-1)$$, or $$F(-1) = \frac{F(0)}{0} = \infty$$. Continuing on this logic: $$F(-1) = -1 * F(-2)$$, or $$F(-2) = -F(-1)$$. Similarly, $$\frac{F(-1)}{-1} = F(-3) = -F(-2)$$.

To ignore all the non-sense, if the input is between 8 to 13, print $$n!$$. It's always overflow if $$n \geq 14$$, and always underflow from 0 to 7. For negative numbers, negative odd numbers are overflows, and negative even numbers are underflows.

Gotchas

 * Read above. The entire problem is a gotcha.

Input
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Output
Overflow! Underflow! Overflow! Underflow! Overflow! Underflow! Underflow! Underflow! Underflow! Underflow! Underflow! Underflow! Underflow! 40320 362880 3628800 39916800 479001600 6227020800 Overflow! Overflow!