Home Problems Volumes Ranking Forum Help News Mess

Linear Function

Volumes:   Simple Math

Who solved this?

from Rodion Gork's favorite books (what?):
Algorithms, 4th edition
by Robert Sedgewick
Another great source on algorithms
also gives a course at Coursera!

Very common problem in computational programming is to determine the underlying law to which some phenomenon obeys. For learning purpose let us practice a simple variant - discovering linear dependence by two given observations (for example, how the price for some product depends on its size, weight etc.)

Linear function is defined by an equation:

y(x) = ax + b

Where a and b are some constants.
For example, with a=3, b=2 function will yield values y = 2, 5, 8, 11...
for x = 0, 1, 2, 3...

Your task is to determine a and b by two points, belonging to the function.
I.e. you are told two pairs of values (x1, y1), (x2, y2) which satisfy the function equation - and you should restore the equation itself.

Test data contains number of test-cases in the first line and then test-cases themselves in separate lines. Each case contains 4 integer numbers. Results should be integer too and you are to write them in line, separating with spaces and enclosing each pair in parenthesis, for example:

input data:
0 0 1 1
1 0 0 1

(1 0) (-1 1)
You need to login to get test data and submit solution.