An empty plane, adding a point each time, its coordinates are calculated based on the previous point: (x [I-1] * Ax + Bx) mod Cx, (y [I-1] * Ay + By) mod Cy calculates the square of the distance from the nearest point of a certain set, adding n points in total, calculate the sum of squares obtained each time (n <= 5*100000, T (T <= 10) group of test data ).
--> It takes 20 seconds for a long time, but the brute force command still applies to TLE ...... Greedy pruning is performed here.
In the set of existing points, the point is sorted by the x coordinate. Each time a point is added, the point is scanned from the start to the right, and the square of the single abscissa difference> = Min jumps out, because the square of the deviation of the vertical coordinates must be greater than or equal to Min, and the greater the vertical coordinate is, the same way, scanning from the first point on the left of the point to the left, after scanning the statistical results.
# Include <cstdio> # include <set> using namespace std; const int maxn = 500000 + 10; const long INF = (1LL <60); int n, x [maxn], y [maxn]; struct Point {int x; int y; bool operator <(const Point & e) const {return x <e. x ;}}; void read (int * a) {int A, B, C, I; a [0] = 0; scanf ("% d ", & A, & B, & C); for (I = 1; I <= n; I ++) {a [I] = (long) a [I-1] * A + B) % C ;}} long solve () {int I; long M In = INF, ret = 0; multiset <Point> se; se. clear (); Point v; v. x = x [1]; v. y = y [1]; se. insert (v); for (I = 2; I <= n; I ++) {v. x = x [I]; v. y = y [I]; multiset <Point>: iterator p = se. lower_bound (v), iter; for (iter = p; iter! = Se. end (); iter ++) {// starts from p and ends with se. long dx = v. x-iter-> x; dx * = dx; if (dx> = Min) break; // pruning long dy = v. y-iter-> y; dy * = dy; Min = min (Min, dx + dy);} for (iter = p; iter! = Se. begin ();) {// start from the last position of p and finish calculating se. begin () iter --; long dx = v. x-iter-> x; dx * = dx; if (dx> = Min) break; // pruning long dy = v. y-iter-> y; dy * = dy; Min = min (Min, dx + dy);} ret + = Min; se. insert (v) ;}return ret ;}int main () {int T; scanf ("% d", & T); while (T --) {scanf ("% d", & n); read (x); read (y); printf ("% I64d \ n", solve ());} return 0 ;}