Under construction

Still being built. Things may change

Operation (Small)

Time limit5sMemory limit512 MB

Summary
Given a start value S and up to 15 operation cards, order all cards to maximize the final rational result, printed as an irreducible fraction with positive denominator.
Level

Hard8 of 10

Topics
Brute force, Backtracking, Math, Implementation
Solved
No attempts yet

Problem

A card game called Operation is played with cards. Each card holds one arithmetic operation OiO_i (addition, subtraction, multiplication, or division) and one integer ViV_i, the right operand of that operation. A card can read + 0, - -2, or / -4. An operand can be negative or zero, but a division card never has 0 as its operand.

In one round you get a starting integer SS and a set of CC cards. You choose an order for the cards and use each card exactly once. The operations are applied to SS in that order, and the last value is the result of the round.

Every operand on a card is an integer, but the operations run on rational numbers. Suppose the starting value is 5 and the cards are + 1, - 2, * 3, / -2. Used in that order the result is (5+1−2)×3/(−2)=−6(5 + 1 - 2) \times 3 / (-2) = -6. Operator precedence does not apply, the cards are applied in the order you choose. Used in the order - 2, / -2, + 1, * 3 the result is ((5−2)/(−2)+1)×3=−3/2((5 - 2) / (-2) + 1) \times 3 = -3/2, which is the largest value this set of cards can reach.

Given a set of cards, find the largest final value. Write it as an irreducible fraction with a positive denominator.

Input

The first line has the number of test cases TT. Each test case begins with a line holding two integers SS and CC, the starting value and the number of cards. The next CC lines describe the cards. The ii-th of those lines has one character OiO_i for the operation and one integer ViV_i for the operand.

Limits

  • 1≤T≤1001 \le T \le 100
  • −1000≤S≤1000-1000 \le S \le 1000
  • OiO_i is one of +, -, *, / for all ii.
  • −1000≤Vi≤1000-1000 \le V_i \le 1000 for all ii.
  • If OiO_i is /, then Vi≠0V_i \ne 0.
  • 1≤C≤151 \le C \le 15

Output

For each test case, print one line Case #x: y z, where xx is the test case number starting from 1, and yy and zz are integers such that y/zy/z is the largest final value of the game, yy and zz have no common divisor other than 1 and -1, and zz is strictly greater than 0.

Notes

In the first sample case, playing * 2 before - 3 gives -1, and the required form of that value is -1 1.

The second sample case is the one described in the statement.

In the third sample case the order of the cards does not change the result. The numerator of the answer does not fit in a 64-bit integer.

In the fourth sample case the largest result is 1, reached for example by / -1, * 0, - -1.

In the fifth sample case 0 1 is the only valid answer. 0 2 can be reduced, and 0 -1 has a denominator that is not positive.

Examples1

  1. Example 1

    Input
    5
    1 2
    - 3
    * 2
    5 4
    + 1
    - 2
    * 3
    / -2
    1000 7
    * -1000
    * -1000
    * 1000
    * 1000
    * 1000
    * 1000
    * 1000
    -1 3
    - -1
    * 0
    / -1
    0 1
    + 0
    
    Expected output
    Case #1: -1 1
    Case #2: -3 2
    Case #3: 1000000000000000000000000 1
    Case #4: 1 1
    Case #5: 0 1