The Total is Right
Time limit2sMemory limit512 MB
Decide whether a target N can be formed exactly from up to six given integers using +, -, times, and exact division with positive intermediates.
- Level
Medium6 of 10
- Topics
- Brute force, Recursion, Math, Implementation
- Solved
- No attempts yet
Problem
The Total is Right is a popular TV game that has been broadcast in many countries. The goal of the game is to reach a chosen number using the four arithmetic operations (+, −, ×, ÷) and six given integer numbers for . All intermediate numbers must be positive, and any division must be exact (for example, it is not possible to divide 5 by 2). The input numbers as well as all intermediate numbers can be used at most once, but you are not required to use all input numbers.
For instance, suppose and , , , , , and . We can then obtain by computing
- 75 − 1 = 74 (using /)
- 6 + 6 = 12 (using /)
and finally
- 74 × 12 = 888. (using / and / respectively)
Hence, in that example, the total is right!
Input
The input file consists of multiple test cases. The first line of the input file consists of a single integer indicating the number of test cases. Each test case follows, and consists of one single line that consists of seven integers , and , each separated by a single space. The integer is the number that must be reached, and the integers are the numbers that can be used to reach : for each , we have .
Output
For each test case in the input, your program should print one line consisting of the string The total is right (followed by a newline) or of the string Impossible (followed by a newline), depending on whether or not it is possible to obtain exactly using some or all of the numbers for according to the rules of the game. There should not be any blank lines in your output.