public int WaysToShare(Stack2 coins) { #if DEBUG if (coins.Sum() % 2 != 0) { throw new Exception($"Unexpected sum of coins - should be even number - received {coins.Sum()}"); } #endif // even, with only one set of coins ... one way to share - split down middle. if (coins.Count() == 1) { return(1); } // extract even amounts from odd var(even, odd) = coins.SplitEvenOdd(); if (odd.Count() % 2 == 1) { throw new Exception("Count of odd items is odd - which should not be the case"); } return(MergeBlocks(MergeBlocks(even), MergeBlocks(odd))); }
public void Process(int value, ref Int64 total, int remainder, Stack2 state) { if (remainder != 0) { if (parent == null) { if (remainder > 0) { if (remainder % Head == 0) { var tc = remainder / Head + state.Sum(); if (tc % 2 == 0) { state.Push(remainder / Head); total += new ShareCoinsEvenly(100).WaysToShare(state); state.Pop(); } } } } else { if (SupportsCache()) { if (cache[remainder] == null) { cache[remainder] = new int[maxValue]; } cache[remainder][state.Sum()]++; } else { for (int i = 1; i <= remainder / Head; i++) { state.Push(i); parent.Process(value, ref total, remainder - i * Head, state); state.Pop(); } } } } }