Here is a hint. think of it this way:
Suppose the number you want is X. X mod N = 0.
So, you need to save only N states, i.e. 0-n-1. Start with 1. and run bfs. You need to drop branches that have the same remainder as before. I will leave you a proof.
sukunrt
source share