Fridge Lock
Time limit1sMemory limit128 MB
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 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 chosen numbers satisfy clues.
Number the rings (outer) through (inner), and let also denote the number picked from ring . Each clue is a linear equation in those picked values:
Each clue is given already parsed into its integer coefficients and its right-hand side , 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 " 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 , the number of rings (and the number of clues), where .
- 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 and numbers, and different rings may show different amounts.
- more lines follow, one per clue. Each clue line contains integer coefficients, then the token
=, then the integer right-hand side:c_1 c_2 ... c_K = b.
Every displayed ring number is between and . 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.