Coloring the Infinite Plane
Time limit2sMemory limit1024 MB
Given N integer vectors, find the density of the lattice they generate in the plane, printed as a fraction modulo 1,000,000,007.
- Level
Medium6 of 10
- Topics
- Math, Number theory
- Solved
- No attempts yet
Problem
Every lattice point on the coordinate plane is painted white. Jiho stands on a point and can move by or by from that point.
Starting from the origin , color black every point that Jiho can reach.
For example, suppose Jiho can move by , , and . The figure below shows this case.

The red point is the origin, and the blue points are the points Jiho can reach from the origin in one move. Since Jiho can also move by , the green points are also reachable from the origin in one move. Points reachable after several moves are colored black. Every point except the white ones can be reached from the origin.
The fraction of lattice points that are black is a rational number. In the example above, only points with both coordinates even are colored, so the fraction is .
Write this rational number as an irreducible fraction , and print . Here is prime.
If the fraction is , print . If it is , print . The fraction is always rational, and no test case has a denominator that is a multiple of . In other words, the answer always exists.
Input
The first line contains . Each of the next lines contains and separated by a space, for .
Output
Print the answer on the first line.
Constraints
- ()
Hint
Moving from a point by takes you to the point .
The black ratio is defined precisely as follows. For every , there is a positive integer such that for every integer , the fraction of colored points among all lattice points with differs from by less than . The problem guarantees that always exists and is rational.
For a prime and integers with , is the smallest nonnegative integer that satisfies .