System of Linear Equations

Time limit1sMemory limit128 MB

Summary
Parse two linear equations and report each unknown as a reduced fraction, or as don't know when it is not uniquely determined.
Level

Medium4 of 10

Topics
Math, Implementation, String
Solved
No attempts yet

Problem

You are given a system of two linear equations in the unknowns xx and yy. Write a program that solves the system.

Input

The first line contains the number of test cases TT.

Each test case consists of two equations, one per line. Different test cases are separated by a blank line.

In each equation, two or more terms are joined by the operators +, -, and =. Each term is either an integer or a variable (xx or yy); a variable may be preceded by a minus sign and/or a coefficient. The = sign appears exactly once per equation. Every operator is surrounded by spaces, and a single term contains no spaces.

Output

For each test case, print the value of xx and then the value of yy, one per line, as a reduced fraction. If a value is an integer, print just the integer with no denominator.

If the value of a variable is not uniquely determined — that is, the system has no solution, or that variable may take more than one value — print don't know in its place. The two variables are judged independently, so it is possible for one variable to have a fixed value while the other is don't know.

Print a blank line between the outputs of consecutive test cases.

Examples1

  1. Example 1

    Input
    7
    2x + 3y = x
    5 = x + y + 3
    
    2x + 3y = 0
    10x = -15y
    
    2x + 3y = 0
    10x = -15y + 1
    
    x = 1
    3x = 6y
    
    2x = 3x + -x + y
    x + y = x + y
    
    2x = -3
    -2y = 3
    
    1 = 2
    x = 3
    
    Expected output
    3
    -1
    
    don't know
    don't know
    
    don't know
    don't know
    
    1
    1/2
    
    don't know
    0
    
    -3/2
    -3/2
    
    don't know
    don't know