Under construction

Still being built. Things may change

Ultimate Finishing Strike

Time limit1sMemory limit128 MB

Summary
Given a rectangular room, start and target points, and a required number of wall bounces, find all bounce-count vectors for the shortest reflecting path.
Level

Hard8 of 10

Topics
Math, Geometry, Brute force, Binary search
Solved
No attempts yet

Problem

Saito Hajime is a ninja who must fight many opponents foolish enough to challenge his might. Most of them fall easily to his martial arts, but from time to time Saito faces a particularly powerful and skilled foe. Such a foe appears only after dozens of lesser minions have already been defeated, and the duel always takes place in an empty rectangular room.

To defeat such a foe, Saito performs a special technique called the Zero Stance Ultimate Finishing Strike. He launches a flying kick from his current position. A single kick is not enough against a powerful foe, but Saito can build up power by bouncing off the walls before he lands the hit. Every bounce makes the attack stronger, so with enough bounces any foe can be defeated. Whenever Saito bounces off a wall he obeys the rule that the angle of incidence equals the angle of reflection.

Saito knows exactly how many times he must bounce off the walls to defeat a given foe. He must be careful, though: if the strike takes too long the foe might dodge it, so the total distance Saito travels must be as short as possible.

Given the room, Saito's starting position, the foe's position, and the required number of bounces, determine how many times Saito hits each of the four walls during the shortest possible strike.

Input

The first line contains a single integer: the number of test cases. Each test case consists of three lines:

  • A line with three integers LL, WW (3≤L,W≤1003 \le L, W \le 100) and BB (0≤B≤1050 \le B \le 10^5): the length and width of the room, and the number of bounces required to defeat the foe.
  • A line with two integers xSx_S and ySy_S (0<xS<L0 < x_S < L, 0<yS<W0 < y_S < W): Saito's starting coordinates.
  • A line with two integers xfx_f and yfy_f (0<xf<L0 < x_f < L, 0<yf<W0 < y_f < W): the foe's coordinates.

The bottom-left corner of the room is at (0,0)(0, 0); the north wall lies in the positive yy-direction and the east wall in the positive xx-direction. Saito and the foe never start at the same position. If Saito hits a corner of the room, it counts as two bounces, one for each of the two walls that meet there. Saito may fly over the foe while performing the strike.

Output

For each test case, output:

  • One or more lines, each containing four integers: the number of times Saito hits the north, east, south, and west wall, respectively, during a shortest strike that uses exactly BB bounces. If several bounce patterns achieve the shortest distance, print every distinct four-tuple, each on its own line, ordered lexicographically (compare the north count first, then east, then south, then west).
  • One more line containing the single number 00.

Examples2

  1. Example 1

    Input
    2
    3 3 1
    1 1
    2 2
    6 6 3
    3 1
    2 4
    
    Expected output
    0 0 0 1
    0 0 1 0
    0 1 0 0
    1 0 0 0
    0
    1 0 1 1
    0
    
  2. Example 2

    Input
    1
    5 5 0
    1 1
    3 3
    
    Expected output
    0 0 0 0
    0