#K74497. Reach Target Number with Custom Rules

    ID: 34210 Type: Default 1000ms 256MiB

Reach Target Number with Custom Rules

Reach Target Number with Custom Rules

You are given an initial number and a target number. In addition, you are provided with a set of rules. Each rule is given in the form \( A\; op\; B = C \) (where \( op \) can be one of \( +, -, \times, \div \)). Starting from the initial number, you can apply any rule as long as applying the operation \( op \) with the rule's second operand \( B \) to the current number yields exactly \( C \). Determine whether it is possible to reach the target number by applying a sequence of these rules. If it is possible, print "Possible"; otherwise, print "Impossible".

Note: The input consists of multiple datasets. Each dataset starts with a line containing three integers: the initial number, the target number, and the number of rules. This is followed by that many lines, each representing a rule in the format shown above. A dataset with the three numbers all equal to 0 indicates the end of the input.

inputFormat

The input is read from standard input (stdin) and consists of several datasets. Each dataset is formatted as follows:

  • The first line of a dataset contains three integers: initial, target, and num_rules.
  • The next num_rules lines each contain a rule in the format: A op B = C, where op is one of +, -, *, or /.
  • The input ends with a line containing "0 0 0", which should not be processed.

outputFormat

For each dataset, output a single line with either "Possible" if the target number can be reached following the rules, or "Impossible" if it cannot.

## sample
5 17 3
5 + 6 = 11
11 * 1 = 11
11 + 6 = 17
0 0 0
Possible

</p>