Is it okay if author solution is shit?


Just simple test case - prime number order of 10^7 = 10000019


Nope, the author’s solution is perfetly elegantly. If you don’t understand it then it doesn’t mean it is shit, it just means that you are dumb.


I find editorial solution elegant and optimized.
How are you planning to handle when, answer exceeds integer/long long range. Using string modular divison is one path. But, that is not required in this problem.
Also, if you plan to do normal bfs, this solution is more space optimized than simple bfs. As we are not keeping duplicates.
Third using states, is an excellent approach. To avoid string modulation. And to form answer from parent, it is required to keep one ans in one state and that too the minimum/optimized/best/first seen/shortest one. That’s why visited is required to clearly form answer from parent array of the states.