IterateOverACube
SRM 775 · 2020-01-15 · by misof
Problem Statement
We use the following pseudocode to visit each cell of an N x N x N cube exactly once:
for sum = 0 .. 3*N-3:
for x = 0 .. N-1:
for y = 0 .. N-1:
for z = 0 .. N-1:
if x+y+z == sum:
visit (x,y,z)
You are given the
Constraints
- N will be between 1 and 10^6, inclusive.
- index will be between 0 and N^3 - 1, inclusive.
3
9
Returns: {2, 0, 0 }
The first ten cells visited are (0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,0,2), (0,1,1), (0,2,0), (1,0,1), (1,1,0), and (2,0,0).
3
10
Returns: {0, 1, 2 }
The next visited cell is (0,1,2).
4747
106968940722
Returns: {4746, 4746, 4746 }
The very last cell visited in this cube.
4
32
Returns: {0, 2, 3 }
1000000
1000000000
Returns: {113, 391, 1312 }
Submissions are judged against all 120 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class IterateOverACube with a public method vector<int> findCell(int N, long long index) · 120 test cases · 2 s / 256 MB per case