Crystal
Time limit1sMemory limit128 MB
Sum the signed charges of all three-colored unit triangles in a hexagonal crystal filled row by row from a modular generator.
- Level
Hard9 of 10
- Topics
- Math, Geometry, Simulation, Prefix sum
- Solved
- No attempts yet
Problem
Bajtazar works at a company that manufactures crystals. Each atom of a crystal is one of three kinds, written , , . A crystal of order consists of atoms arranged into a regular hexagon with side , which is divided into equilateral triangles of side . The picture below shows an example crystal of order .

Crystals have an electrical property. Any three atoms that form a triangle of side and whose kinds are pairwise different produce a field of unit charge: positive if the atoms appear in the order , , when the triangle is traversed counterclockwise, or negative if that same order appears when the triangle is traversed clockwise. The charge of the whole crystal is the sum of the charges of all such triples.
Bajtazar's machine is programmed with six numbers , , , , , . It builds the crystal of order , placing atoms row by row starting from the topmost row, and left to right within each row. The kind of each successive atom is chosen by running the following steps in order:
s := ((a * s + b) div k) mod (3 * m)
r := s div m
Here div is integer division (the quotient) and mod is the remainder. The two statements run one after another, once for each atom.
Compute the charge of the crystal produced for the given input parameters.
Input
The first line contains six integers , , , , , describing the machine's input parameters (, , , ).
Output
Print one integer: the charge of the crystal the machine produces.