Under construction

Still being built. Things may change

King's Circle

Time limit100sMemory limit1024 MB

Summary
Count the sets of three cafes on a grid that all lie on the sides or corners of a common square, with cafe positions generated from a recurrence.
Level

Medium7 of 10

Topics
Geometry, Sorting, Hash map
Solved
No attempts yet

Problem

For the first time in living memory, the Kingdom of Kickstartia is celebrating. The new King is being crowned today, and as is customary for a coronation, a Royal Parade will march through the streets of the capital.

The capital is an infinite 2D plane with infinitely many, infinitely long streets, spaced one meter apart, running horizontally and vertically. The horizontal streets are numbered from negative infinity to infinity from top to bottom, and the vertical streets are numbered from negative infinity to infinity from left to right.

There are NN cafes in the capital. The ii-th cafe is located at the intersection of vertical street ViV_i and horizontal street HiH_i. No two cafes share an intersection. You will pick exactly three of these cafes for the parade technicians to stop at along the way.

The parade must end where it starts, and its path must form a square with straight sides of equal length. Each cafe can be anywhere along the square, on a side or at a corner.

Some sets of three cafes cannot lie on a single such square. Count the number of different sets of three cafes for which at least one square parade passes through all three. Two sets are different if and only if one contains a cafe that the other does not.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains ten integers NN, V1V_1, H1H_1, AA, BB, CC, DD, EE, FF, and MM.

NN is the number of cafes, and the first cafe is at the intersection of vertical street V1V_1 and horizontal street H1H_1.

For i=2i = 2 to NN, the locations are generated as follows.

Vi=(A×Vi−1+B×Hi−1+C) mod MV_i = (A \times V_{i-1} + B \times H_{i-1} + C) \bmod M

Hi=(D×Vi−1+E×Hi−1+F) mod MH_i = (D \times V_{i-1} + E \times H_{i-1} + F) \bmod M

Output

For each test case, output one line in the format Case #x: y, where xx is the test case number starting from 1 and yy is the number of sets of three cafes that lie on a common square parade.

Constraints

  • 1≤T≤1001 \le T \le 100.
  • 0≤A,B,C,D,E,F<M0 \le A, B, C, D, E, F < M.
  • 0≤V1,H1<M0 \le V_1, H_1 < M.
  • For all i≠ji \ne j, (Vi,Hi)≠(Vj,Hj)(V_i, H_i) \ne (V_j, H_j).

Hint

In Sample Case #1, there are four cafes at (1, 1), (1, 0), (0, 3), and (4, 0). A square parade can pass through the set (1, 1), (1, 0), (0, 3), the set (1, 1), (1, 0), (4, 0), or the set (1, 0), (0, 3), (4, 0). No square parade passes through the set (1, 1), (0, 3), (4, 0).

In Sample Case #2, there are 6 cafes at (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), and (8, 1). They all lie on the same vertical street, so every triplet has a square parade through it. The answer is 6 choose 3 = 20.

In Sample Case #3, there are 3 cafes at (7, 24), (19, 17), and (0, 34). No square parade passes through all three, so the answer is 0.

We do not recommend interpreted or slower languages for the Large dataset of this problem.

Examples1

  1. Example 1

    Input
    3
    4 1 1 4 1 1 4 2 4 5
    6 3 1 1 0 1 0 1 0 9
    3 7 24 34 11 17 31 15 40 50
    
    Expected output
    Case #1: 3
    Case #2: 20
    Case #3: 0