optimize problem 64, caching nth_expansion result