Banner
Time limit1sMemory limit128 MB
Count unordered pairs of integer grid points in a W by H post grid whose distance lies in [L1, L2] and whose connecting segment contains no other grid post.
- Level
Medium7 of 10
- Topics
- Math, Number theory, Geometry, Brute force
- Solved
- No attempts yet
Problem
Bessie is returning from a long trip abroad, and Farmer John wants to erect a nice "Welcome Home" banner in her pasture for her arrival. The banner hangs between two poles on a wire whose length must lie in the range ().
The pasture measures (; ), and Farmer John has installed a post at every point with integer coordinates. From these points he must pick exactly two to hold the two ends of the wire.
To keep the hanging banner from being disturbed, Farmer John requires that no other post lie directly under the tight wire. In other words, no post may sit exactly on the segment joining the two chosen endpoints (the endpoints themselves do not count).
Count how many ways Farmer John can hang the banner — that is, how many unordered pairs of posts have a straight-line distance within and no other post lying on the segment between them. The answer can be large and may exceed the range of a 32-bit integer.
Worked illustration. Consider a pasture with and . The posts form the grid below.
* * *
* * *
Suppose the banner length must lie in . This pasture has posts and candidate pairs. Only four of them have a length within :
Among these four, (0,0)-(2,0) and (0,1)-(2,1) each have another post lying exactly on the segment between the endpoints, so they are unsuitable. Only the remaining two pairs are acceptable, giving the answer 2.
Input
A single line with four space-separated integers , , , and .
Output
A single integer: the number of ways the banner can be hung.