ClosestPoints
SRM 199 · 2004-06-16 · by legakis
Problem Statement
NOTE: This problem statement contains subscripts and superscripts which may not display properly for plugin users.
Write a program to generate a list of random 3D points in space, and then compute the distance between the pair of closest points. Also determine how many distinct pairs of points are this exact distance apart.
Generate the random points using the following pseudo-random number generator. Starting with a given seed0:
seedi+1 = (seedi * 16807) mod (231-1)
The ith random number (starting at i = 1) is given by:
randi = (seedi mod (2 * range)) - range
The 3D points are triples of 3 successive random numbers:
(rand1, rand2, rand3)
(rand4, rand5, rand6)
(rand7, rand8, rand9)
(rand10, rand11, rand12)
etc...
You will be given an initial seed, the range, and N (the number of points). The random numbers produced by the generator will be between -range and range-1, inclusive. Return a
NOTE: Be sure to use 64-bit arithmetic for the multiply and mod in the random-number generator, and for computing squared distances.
Notes
- Ignore any duplicate points. (See example 1.)
- There will be at least 2 unique points.
Constraints
- N will be between 2 and 150000, inclusive.
- range will be between 1 and 1000000, inclusive.
- seed will be between 1 and 1000, inclusive.
- The square of the distance of the closest pair of points will be less than 1000000000.
3
100
1
Returns: { 9163, 1 }
The three points are: (-93, -51, -27), (-42, 30, -28), and (44, -22, 23). The closest pair of points are the first and third, and the square of their distance is 9163. There is 1 pair of points with a squared distance of 9163.
10000
1
7
Returns: { 1, 12 }
With a range of -1 to 0, there are only 8 possible points. Ignoring duplicates, all 8 possible points are present, forming a 2x2x2 cube. The closest pairs of points are 1 unit apart, and there are 12 such pairs.
25
1
12
Returns: { 1, 9 }
This is similar to the previous example, except that only 7 of the 8 possible points exist.
646
64364
464
Returns: { 2774994, 1 }
150000
1000000
30
Returns: { 311985, 1 }
Submissions are judged against all 33 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ClosestPoints with a public method vector<int> distance(int N, int range, int seed) · 33 test cases · 2 s / 256 MB per case