Under construction

Still being built. Things may change

The Total is Right

Time limit2sMemory limit512 MB

Summary
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 NN using the four arithmetic operations (+, −, ×, ÷) and six given integer numbers mim_i for 1≤i≤61 \le i \le 6. 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 mim_i 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 N=888N = 888 and m1=100m_1 = 100, m2=6m_2 = 6, m3=75m_3 = 75, m4=3m_4 = 3, m5=1m_5 = 1, and m6=6m_6 = 6. We can then obtain N=888N = 888 by computing

  • 75 − 1 = 74 (using m3m_3/m5m_5)
  • 6 + 6 = 12 (using m2m_2/m6m_6)

and finally

  • 74 × 12 = 888. (using m3m_3/m5m_5 and m2m_2/m6m_6 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 N,m1,m2,m3,m4,m5N, m_1, m_2, m_3, m_4, m_5, and m6m_6, each separated by a single space. The integer 101≤N≤999101 \le N \le 999 is the number that must be reached, and the integers mim_i are the numbers that can be used to reach NN: for each 1≤i≤61 \le i \le 6, we have mi∈{1,2,3,4,5,6,7,8,9,10,25,50,75,100}m_i \in \{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100\}.

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 NN using some or all of the numbers mim_i for 1≤i≤61 \le i \le 6 according to the rules of the game. There should not be any blank lines in your output.

Examples1

  1. Example 1

    Input
    2
    888 100 6 75 3 1 6
    449 2 6 100 10 2 8
    
    Expected output
    The total is right
    Impossible