All Squares
Time limit1sMemory limit128 MB
Given starting size k, count the nested corner squares whose border or interior holds the query point.
Problem
Geometrically every square has one well defined centre point. On a grid that holds only when the sides of the square span an odd number of points. Every odd number can be written as , so a square whose sides are points long is said to have size .
A pattern of squares is built with these rules.
- The largest square has size , so its sides are points long, and it sits at the centre of a grid of size 1024, whose sides are 2049 points long.
- The smallest permissible square has size 1 and the largest has size 512, so .
- A square of size has a square of size centred on each of its 4 corners. The division is integer division, so . A square of size 1 puts nothing on its corners.
- The top left corner of the grid has coordinates .
A value of therefore fixes one pattern. The picture below shows the pattern for .

Any point of the grid is surrounded by zero or more squares. A point lying on the border of a square counts as surrounded by that square.
Read a value of and the coordinates of a point, then report how many squares surround the point.
Input
Each line of input holds 3 integers: , the coordinate of the point, and the coordinate of the point, with and . The input ends with a line holding three zeroes, 0 0 0, which is not a query.
Output
Print one line for every query line of the input. Each line holds the number of squares that surround the given point.