I tried solving problem magic numbers with iterative digit dp. But it runs slower than my recursive solution by more than 200ms.
iterative : link
recursive : link
I also avoided using modulo operations in my iterative code, but am surprised to see it running slower.
Any idea why this happened?
There are only $$$O(1)$$$ states which don't have the last 2 values equal to 1, meaning that the recursive solution visits about 4 times less states than the iterative solution(the recursive solution visits about $$$nm$$$ states, while the iterative solution visits about $$$4nm$$$ states).127938045 just adds an if to make sure states that are unreachable don't waste time and that speeds up the solution a lot.