Polygons
Time limit10sMemory limit1024 MB
Count convex polygons on an X by Y lattice grid whose sides have no interior lattice points and integer length at most K, modulo 2^32.
- Level
Hard9 of 10
- Topics
- Number theory, Geometry, Combinatorics, Dynamic programming
- Solved
- No attempts yet
Problem
We are already tired and have no idea for an interesting story problem. The task is simply to count convex polygons with short integer sides and no extra lattice points. Let the vertices be . The following conditions must hold.
- ,
- The vertices are lattice points, so and are integers.
- No lattice point lies on any side except the vertices themselves.
- The length of each side is an integer not greater than .
- The polygon is convex, simple, and non-degenerate. Every interior angle is less than 180 degrees, no edges cross, and there are at least three vertices. No three vertices are collinear.
The figure below shows invalid polygons. The first and second have a lattice point on one of their sides. The second and third are not convex. The first and third have sides whose lengths are not integers.

Input
The first and only line contains three integers , , and (, ). They are the bounds on the coordinates and on the side length.
Each test group belongs to at least one of the following subtasks. For example, there is no test with , because it fits no subtask.
- ,
- and are divisible by
Output
Print the number of valid polygons modulo . Two polygons are different if some point is a vertex of one of them but not the other.
Hint
In the sample, , , . One of the 42 valid polygons has vertices , shown in the figure below.
