Somewhere within the discussion threads on the

Yahoo! Groups for AZSPCS is

a link to a code that solves the

`D=3`

problem by brute force. After careful inspection of the code, I can safely say that I've fully understood it now.

The algorithm generates all admissible basis to find one with the best score. At each level of the recursion, it tries in increasing order all numbers strictly larger than the previous number, but less than or equal to the score of the numbers we have so far. Score is implicit in the global data structure that it maintains throughout the search. The common leave-it-as-you-found-it principle is enforced, and thus changes made to the data structure needs to be undone upon backing out of the recursion.

Conceptually, the data structure can be described as follows. Let

`count[x][d]`

be the number of ways the number

`x`

can be represented as a sum of exactly

`d`

numbers (

`d = 1, 2,`

or

`3`

) in the basis we have so far. The current score, then, will be the lowest value

`s`

such that

`count[s][1]`

,

`count[s][2]`

, and

`count[s][3]`

are all zeroes.

We start with no numbers in the basis, so

`count`

is initialized to all zeroes. Then, when a number b is introduced to the basis, we must update

`count`

as follows:

count[b][1]++
for j = b+1 to 3*b
count[j][2] += count[j-b][1]
count[j][3] += count[j-b][2]

It should be noted here that

`count`

needs to be updated left to right because of the directional data dependency. Conversely, to undo this update, we would need to go subtracting right to left, and decrementing

`count[b][1]`

after the loop.

The code essentially implements the above algorithm, spiced up with various low-level optimizations. The most important one here is the packing of

`count[x][3], count[x][2], count[x][1]`

into one number. This is quite ingenious because the two

`O(D)`

operations (checking if all are zeroes, and the shifted addition/subtraction) are now

`O(1)`

.

Another obvious optimization is the use of what I later found out is called the

Duff's device. I'm familiar with the concept of loop unrolling, but was nonetheless previously unaware of this construct (or even that a switch statement can be (ab)used in this manner!).

And that pretty much describes the code in a nutshell.