Problem:
Problem F: Directional Resemblance

Vectors have their directions and two vectors make an angle between them. Given a set of three-dimensional vectors, your task is to find the pair among them that makes the smallest angle.

Input

The input is a sequence of datasets. A dataset specifies a set of three-dimensional vectors, some of which are directly given in the dataset and the others are generated by the procedure described below.

Each dataset is formatted as follows.

m n S W
x1 y1 z1
x2 y2 z2
:
xm ym zm

The first line contains four integers m, n, S, and W.

The integer m is the number of vectors whose three components are directly specified in the
dataset. Starting from the second line, m lines have the three components of m vectors. The i-th line of which indicates the vector vi = (xi, yi, zi). All the vector components are positive integers less than or equal to 100.

The integer n is the number of vectors generated by the following procedure.

int g = S;
for(int i=m+1; i<=m+n; i++) {
x[i] = (g/7) %100 + 1;
y[i] = (g/700) %100 + 1;
z[i] = (g/70000)%100 + 1;
if( g%2 == 0 ) { g = (g/2); }
else { g = (g/2) ^ W; }
}

For i = m + 1, . . . , m + n, the i-th vector vi of the set has three components x[i], y[i], and z[i] generated by this procedure.

Here, values of S and W are the parameters S and W given in the first line of the dataset. You may assume 1 ≤ S ≤ 109 and 1 ≤ W ≤ 109.

The total number of vectors satisfies 2 ≤ m + n ≤ 12 × 104. Note that exactly the same vector may be specified twice or more in a single dataset.

A line containing four zeros indicates the end of the input. The total of m+n for all the datasets in the input never exceeds 16 × 105.

Output

For each dataset, output the pair of vectors among the specified set with the smallest non-zero angle in between. It is assured that there are at least two vectors having different directions.

Vectors should be indicated by their three components. The output for a pair of vectors va and vb should be formatted in a line as follows.

xa ya za xb yb zb

Two vectors (xa, ya, za) and (xb, yb, zb) are ordered in the dictionary order, that is, va < vb if xa < xb, or if xa = xb and ya < yb, or if xa = xb, ya = yb and za < zb. When a vector pair is output, the smaller vector in this order should be output first.

If more than one pair makes the equal smallest angle, the pair that is the smallest among them in the dictionary order between vector pairs should be output. The pair (vi, vj) is smaller than the pair (vk, vl) if vi < vk, or if vi = vk and vj < vl.

Sample Input

4 0 2013 1124
1 1 1
2 2 2
2 1 1
1 2 1
2 100 131832453 129800231
42 40 46
42 40 46
0 100000 7004674 484521438
0 0 0 0

Output for the Sample Input

1 1 1 1 2 1
42 40 46 83 79 91
92 92 79 99 99 85

Solution in C (judge verdict: Accepted)

// AOJ 1340: Directional Resemblance
// 2017.12.21 bal4u@uu

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct { int x, y, z, l; double a; } V;
V v[120002];
char buf[50], *p;

double angle(V *v1, V *v2)
{
	int a, b;

	b = v1->l * v2->l;
	a = v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
	a *= a;
	if (a == b) return 0;
	return acos(sqrt((double)a/b));
}

int cv(V *v1, V *v2)
{
	if (v1->x < v2->x) return -1; if (v1->x > v2->x) return 1;
	if (v1->y < v2->y) return -1; if (v1->y > v2->y) return 1;
	if (v1->z < v2->z) return -1; if (v1->z > v2->z) return 1;
	return 0;
}

int cmp(V *a, V *b)
{
	if (a->a < b->a) return -1;
	if (a->a > b->a) return 1;
	return 0;
}


int in()
{
	int n = 0;
	while (*p >= '0') n = (n<<3) + (n<<1) + (*p++ & 0xf);
	p++;
	return n;
}

int main()
{
	int m, n, mn, S, W, g, i, j;
	V v0, t, min1, min2, min3, min4;
	double min, a;

	while (1) {
		fgets(p=buf, 50, stdin);
		m = in(), n = in(), S = in(), W = in();
		if (!S) break;

	    for (i = 0; i < m; i++) {
			fgets(p=buf, 50, stdin);
			v[i].x = in(), v[i].y = in(), v[i].z = in();
		}

		g = S;
	    mn = m+n; for (i = m; i < mn; i++) {
			v[i].x = (g / 7    ) % 100 + 1;
			v[i].y = (g / 700  ) % 100 + 1;
			v[i].z = (g / 70000) % 100 + 1;
			if ((g & 1) == 0) g >>= 1;
			else g = (g >> 1) ^ W;
		}

	    v0.x = v0.y = v0.z = 1, v0.l = 3, v0.a = 0;
		for (i = 0; i < mn; i++) {
			v[i].l = v[i].x*v[i].x + v[i].y*v[i].y + v[i].z*v[i].z;
			v[i].a = angle(&v0, v+i);
		}

	    qsort(v, mn, sizeof(V), cmp);
		min = acos(0);
		for (i = 0; i < mn; i++) for(j = i+1; j < mn; j++) {
			if (fabs(v[i].a - v[j].a) > min) break;

			a = angle(v+i, v+j);
			if (a == 0) continue;
			if (a < min) {
				min1 = v[i], min2 = v[j], min = a;
				if (cv(&min1, &min2) > 0) t = min1, min1 = min2, min2 = t;
			} else if (fabs(a - min) < 1e-7) {
				min3 = v[i], min4 = v[j];
				if (cv(&min3, &min4) > 0) t = min3, min3 = min4, min4 = t;
				if (cv(&min1, &min3) > 0) min1 = min3, min2 = min4;
				else if (cv(&min1, &min3) == 0 && cv(&min2, &min4) > 0) min2 = min4;
			}
		}

		printf("%d %d %d %d %d %d\n", min1.x, min1.y, min1.z, min2.x, min2.y, min2.z);
	}
	return 0;
}