I've been working through some algorithm programming problems and have a question about one. The problem is the same one as the one referenced in this question: USACO: Subsets (Inefficient)
I was able to code some (non-dynamic) solutions that were too slow for high N. Had to cheat a bit and look up some solutions online. It turns out the fast algorithm is trivially simple, but even knowing the answer I still can't see how to get from the problem to the answer. I can see patterns in the way subsets of equal sums form, but I don't see the link between those patterns and the algorithmic solution.
The problem (link above) is:
Given a set of consecutive integers from 1 through N (1 <= N <= 39), how many ways can the set be partitioned into two subsets whose sums are identical? E.g., {1,2,3} can be partitioned one way: {1,2} {3}.
For larger sets the answer is either 0 (when N*(N+1)/2 is odd) or is given by this simple algorithm:
arr = array of int with (N*(N+1)/4)+1 elements
arr[0]=1 // all other elements initialized to 0
for i = 1 to N
for j = N*(N+1) / 4 downto i
add arr[j-i] to arr[j]
subsetpaircount = arr[N*(N+1)/4] / 2
Again, I can see how the algorithm works, I've even inserted print statements so I can "watch" how it works. I just can't see how the operation of the algorithm links up to the pattern of different ways the two-set partitions are generated.
A response in the linked question may be relevant, but I also can't connect up how it works: "This is the same thing as finding the coefficient x^0 term in the polynomial (x^1+1/x)(x^2+1/x^2)...(x^n+1/x^n). . . ."
Can anyone clarify the connection for me, or point me to some reference materials that explain this specific problem? Thanks.