#P16643. [GKS 2018 #B] King's Circle

    ID: 19006 远端评测题 20000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2018树状数组扫描线Google Kick Start

[GKS 2018 #B] King's Circle

Problem Description

For the first time in as long as anyone can remember, the kingdom of Kickstartia is alive with celebration: it is the coronation day for the new King. As is customary for the coronation, a Royal Parade will march its way through the streets of the capital.

The capital can be thought of as an infinite 22D plane, with infinitely many, infinitely long, streets spaced one meter apart, running horizontally and vertically throughout. The horizontal streets are labelled from negative infinity to infinity from top to bottom, while the vertical streets are labelled from negative infinity to infinity from left to right.

There are NN cafes in the capital, and the ii-th one is located at the intersection of vertical street ViV_i and horizontal street HiH_i. No two cafes lie on the same intersection. In order to keep the parade technicians happy and well-fed, you will pick exactly three of these cafes for them to stop at along the way.

To introduce some order to the chaos, you have additionally decided that a parade should end where it starts, and that its path through the streets must make the shape of a square (with straight sides, all of equal length). Each cafe can be anywhere along the square (on the sides or at a corner).

This immediately presents a problem: depending on which three cafes you pick, it may not be possible to make a square parade that goes through those three cafes. So, your task is to find out: how many different sets of three cafes are there such that there exists at least one square parade that includes all three cafes in the set? (Two sets are different if and only if there is a cafe in one that is not present in the other.)

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case consists of one line containing ten integers NN, V1V_1, H1H_1, AA, BB, CC, DD, EE, FF and MM.

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

The locations of the remaining cafes ViV_i, HiH_i, can be generated for i=2i = 2 to NN as follows:

  • $V_i = (A \times V_{i-1} + B \times H_{i-1} + C) \pmod M$
  • $H_i = (D \times V_{i-1} + E \times H_{i-1} + F) \pmod M$

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the number of sets of cafes satisfying the conditions explained above.

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
Case #1: 3
Case #2: 20
Case #3: 0

Hint

In Sample Case #1, there are four cafes and they are at (11, 11), (11, 00), (00, 33) and (44, 00). It would be possible for a square parade to go through the set of cafes (11, 11), (11, 00), (00, 33), or the set (11, 11), (11, 00), (44, 00) or the set (11, 00), (00, 33) (44, 00) as shown in the diagram below. There is no possible square parade that goes through the set of cafes (11, 11), (00, 33), (44, 00).

:::align{center} :::

In Sample Case #2, there are 66 cafes and they are at (33, 11), (44, 11), (55, 11), (66, 11), (77, 11) and (88, 11). Since they are all on the same vertical street, every triplet of cafes has a square parade going through it. So the answer is (63)6 \choose 3 = 2020.

In Sample Case #3, there are 33 cafes and they are at (77, 2424), (1919, 1717), (00, 3434). There is no square parade that goes through these cafes, so the answer is 00.

Note: We do not recommend using interpreted/slower languages for the Large dataset of this problem.

Limits

1T1001 \le T \le 100.

0A<M0 \le A < M.

0B<M0 \le B < M.

0C<M0 \le C < M.

0D<M0 \le D < M.

0E<M0 \le E < M.

0F<M0 \le F < M.

0V1<M0 \le V_1 < M.

0H1<M0 \le H_1 < M.

For all iji \neq j, (Vi,Hi)(Vj,Hj)(V_i, H_i) \neq (V_j, H_j).

Small dataset (Test set 1 - Visible)

3N10003 \le N \le 1000.

2M10002 \le M \le 1000.

Large dataset (Test set 2 - Hidden)

3N5×1053 \le N \le 5 \times 10^5.

2M1062 \le M \le 10^6.