#P12103. Overlapping Rectangles Screensaver
Overlapping Rectangles Screensaver
Overlapping Rectangles Screensaver
On a very old operating system, a screensaver consists of two rectangles flying around the screen. The screen is given by a width \(W\) and a height \(H\). The coordinate system has its origin at the top-left corner, with the \(x\)-axis increasing rightwards and the \(y\)-axis increasing downwards.
For \(i = 1, 2\), rectangle \(i\) has a width of \(w_i\) pixels and a height of \(h_i\) pixels. Initially, its top-left corner is at \((x_i, y_i)\) and it moves in direction \((\delta x_i, \delta y_i)\) where each of \(\delta x_i\) and \(\delta y_i\) is either \(-1\) or \(1\). At the end of each second the top-left corner of rectangle \(i\) moves by \((\delta x_i, \delta y_i)\). Whenever rectangle \(i\) touches the left or right border of the screen the value of \(\delta x_i\) changes sign (and similarly for \(\delta y_i\) when it touches the top or bottom border). In the special case where it touches a corner (i.e. two borders simultaneously) both components change sign.
Because of these rules, the rectangles always remain fully within the screen. They are said to be overlapping during a second if the intersection between the two rectangles has a positive area.
Let \(f(t)\) be the number of seconds \(\tau = 0, 1, \ldots, t-1\) during which the rectangles overlap (noting that second \(0\) is the initial state before any movement). It can be shown that the limit \[ \lim_{t \to \infty} \frac{f(t)}{t} \] exists as an irrational number but is always a rational number. Your task is to compute this limit and output it as an irreducible fraction \(\frac{p}{q}\).
Note: The rectangle motions are discrete and perfectly elastic with respect to the borders.
inputFormat
The input consists of three lines:
- The first line contains two integers \(W\) and \(H\): the width and height of the screen.
- The second line contains six integers: \(x_1\), \(y_1\), \(w_1\), \(h_1\), \(\delta x_1\), \(\delta y_1\) describing the first rectangle.
- The third line contains six integers: \(x_2\), \(y_2\), \(w_2\), \(h_2\), \(\delta x_2\), \(\delta y_2\) describing the second rectangle.
It is guaranteed that the rectangles are initially completely within the screen (i.e. \(0 \le x_i \le W - w_i\) and \(0 \le y_i \le H - h_i\) for \(i=1,2\)).
outputFormat
Output a single line containing the irreducible fraction \(p/q\) (with no spaces) representing the limit \(\lim_{t\to\infty} f(t)/t\).
sample
10 10
1 1 3 3 1 1
6 6 3 3 -1 -1
2/7