This page is still under construction.

Parts of this page are still being built. What you see may change.

Fridge Lock

Time limit1sMemory limit128 MB

Summary
Given K rings each showing 3 to 49 positive integers and K linear equations, pick one number per ring satisfying all equations and print the selection.
Level

Medium6 of 10

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

Problem

A locksmith is called to open an unusual lock shown on a screen mounted on a fridge door.

The lock has KK concentric rings. Each ring displays a set of positive integers. The door opens when you pick exactly one number from every ring so that the KK chosen numbers satisfy KK clues.

Number the rings R1R_1 (outer) through RKR_K (inner), and let RiR_i also denote the number picked from ring ii. Each clue is a linear equation in those picked values:

c1R1+c2R2+⋯+cKRK=bc_1 R_1 + c_2 R_2 + \cdots + c_K R_K = b

Each clue is given already parsed into its integer coefficients c1,c2,…,cKc_1, c_2, \ldots, c_K and its right-hand side bb, listed from the outer ring to the inner ring. For example (with three rings), the clue "the outer ring minus twice the middle ring equals 00" is written as the coefficient line 1 -2 0 = 0.

Coefficients may be negative, zero, or positive. The clues always pin down exactly one valid selection.

Read the rings and the clues, then output the one selection that opens the lock, from the outer ring to the inner ring.

Input

The input contains several test cases. Each test case is given as:

  • A line with the integer KK, the number of rings (and the number of clues), where 3≤K≤93 \le K \le 9.
  • KK lines follow, one per ring from the outer ring to the inner ring. Each line lists the positive integers shown on that ring, separated by spaces. A ring shows between 33 and 4949 numbers, and different rings may show different amounts.
  • KK more lines follow, one per clue. Each clue line contains KK integer coefficients, then the token =, then the integer right-hand side: c_1 c_2 ... c_K = b.

Every displayed ring number is between 11 and 9999. A line containing a single 0 marks the end of the input and must not be processed.

Output

For each test case, print the selected numbers on one line, separated by single spaces, listed from the outer ring to the inner ring.

Examples1

  1. Example 1

    Input
    3
    11 4 2 15 7 10 9 2
    4 8 1 12 7 11 6 5
    3 4 1 13 2 14
    1 -2 0 = 0
    0 1 1 = 7
    -1 1 3 = 1
    0
    
    Expected output
    10 5 2