Crop Triangles (Small)
Time limit5sMemory limit512 MB
Count triples of generated tree points whose coordinate sums are divisible by 3 in both axes.
- Level
Medium4 of 10
- Topics
- Combinatorics, Number theory, Brute force
- Solved
- No attempts yet
Problem
A 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 , and is .
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.
Input
The first line contains the number of test cases . Each of the next lines contains the integers , , , , , , and separated by exactly one space. 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 and as given, without the remainder, so it can exceed .
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:
Output
For each test case, print one line of the form Case #X: Y, where is the test case number starting from 1 and is the number of triangles whose vertices are three distinct trees and whose center is a grid point.
Hint
In the first test case of the first sample, the eight numbers generate the four trees , , and .