HCI '16 - Unexpected Factorial

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 32M

Authors:
Problem types

In mathematics, the factorial of an integer (denoted by an ! symbol) is as follows:

\displaystyle \begin{align*}
0! &= 1 \\
1! &= 1 \\
2! &= 2 \times 1 = 2 \\
3! &= 3 \times 2 \times 1 = 6 \\
4! &= 4 \times 3 \times 2 \times 1 = 24 \\
5! &= 5 \times 4 \times 3 \times 2 \times 1 = 120
\end{align*}

On Reddit, there is a subreddit /r/unexpectedfactorial, dedicated to trolling users who have accidentally included a factorial in their post or comment.

Here are some examples of unexpected factorials:

Original: By 2007, I was only 5!

Response: Wow, 120 years old is quite old.

Original: Calculators explode when dividing by 0!

Response: Hmm, dividing by 1 shouldn't make your calculator explode…

Input Specification

Your program is required to detect an unexpected factorial N! (0 \le N \le 20) in a single line of text input, and output the actual value of N! (luckily for you, you are not required to phrase it into a statement). If the input does not contain any unexpected factorial, output -1. The input may contain spaces, punctuation and numbers.

The input should only contain 1 unexpected factorial, but may contain multiple exclamation marks.

Output Specification

Your program should output a single integer, the full actual value of N!.

Constraints

Subtask 1 [0%]

Sample test cases.

Subtask 2 [30%]

0 \le N \le 10

Subtask 3 [70%]

0 \le N \le 20

Sample Input 1

Nice! I rate 5/7!

Sample Output 1

5040

Sample Input 2

How obvious - how necessary - was the mathematical ratio of its sides, the quadratic sequence 1:4:9! (from 2001: A Space Odyssey)

Sample Output 2

362880

Sample Input 3

This string does not contain any unexpected factorial!

Sample Output 3

-1

Comments


  • -3
    gavin_chen  commented on May 23, 2022, 12:59 a.m.

    why am i getting #16 in batch 3 wrong?


  • -3
    YoInferno  commented on Sept. 11, 2021, 7:31 p.m.

    Can an input include no exclamation marks?


    • -2
      Spitfire720  commented on Dec. 29, 2021, 6:13 p.m.

      If the input does not contain any unexpected factorial, output -1


  • -1
    Swarley  commented on Jan. 13, 2019, 12:15 a.m.

    /r/theydidthemath