Count triples of generated tree points whose coordinate sums are divisible by 3 in both axes.
Medium4CombinatoricsNumber theoryBrute forceNo attempts yetTime limit5sMemory limit512 MBA few pranksters want to flatten a large triangle into a wheat field overnight. Seen from above the field is an evenly spaced grid, and trees grow at some of the grid points where two grid lines cross. The pranksters want all three vertices of the triangle to sit on trees. They also want the center of the triangle to land on a grid point.
The center of a triangle with vertices (x1,y1), (x2,y2) and (x3,y3) is (3x1+x2+x3,3y1+y2+y3).
You are given the integer coordinates of every tree on the field. Count the triangles whose three vertices are three distinct trees and whose center has integer coordinates in both axes. A triangle of area 0 counts as a valid triangle.
The first line contains the number of test cases N. Each of the next N lines contains the integers n, A, B, C, D, x0, y0 and M separated by exactly one space. n is the number of trees.
The coordinates of the trees are the values printed by the pseudocode below, driven by those eight numbers. mod is the remainder operation. The first tree uses x0 and y0 as given, without the remainder, so it can exceed M.
X = x0, Y = y0
print X, Y
for i = 1 to n-1
X = (A * X + B) mod M
Y = (C * Y + D) mod M
print X, Y
The parameters are chosen so that no two trees share a position.
Limits:
For each test case, print one line of the form Case #X: Y, where X is the test case number starting from 1 and Y is the number of triangles whose vertices are three distinct trees and whose center is a grid point.
In the first test case of the first sample, the eight numbers generate the four trees (0,1), (7,3), (17,5) and (17,7).