Large Banner
Time limit1sMemory limit128 MB
Count pairs of lattice posts in an (M+1) by (N+1) grid whose segment contains no other post and whose Euclidean length lies in [L, H], modulo B.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Geometry, Binary search
- Solved
- No attempts yet
Problem
Bessie is returning from a long trip abroad, and Farmer John wants to mount a nice "Welcome Home" banner for her arrival. Farmer John's field has integer dimensions (). He has installed a post at every point with integer coordinates, using a coordinate system in which is the lower-left corner and is the upper-right corner. Of these posts, Farmer John must choose two as the endpoints of the banner.
Being a perfectionist, Farmer John insists that the banner be perfectly straight: for the two chosen posts, no other post may lie on the straight line segment between them. For instance, posts and cannot both be chosen, because post lies between them.
In addition, the banner's length must be at least and at most (), where the length is the Euclidean distance between the two endpoints.
The banner is reversible, so swapping its two endpoints yields the same way of hanging it. Count the number of distinct ways Farmer John can hang the banner. Because this number can be very large, report it modulo ().
Input
A single line with five space-separated integers: , , , , and .
Output
A single line with one integer: the number of valid ways to hang the banner, taken modulo .