UVa 10323

From Algorithmist
Jump to navigation Jump to search

10323 - Factorial! You Must be Kidding!!![edit]

Summary[edit]

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

Explanation[edit]

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

For this problem, we have , and . With some manipulations, for negative factorials, we can get: , or . Continuing on this logic: , or . Similarly, .

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

Gotchas[edit]

  • Read above. The entire problem is a gotcha.

Implementations[edit]

Input[edit]

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

Output[edit]

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