How can I optimize the solution for MULTHREE

Problem: https://www.codechef.com/LRNDSA01/problems/MULTHREE
Solution: https://ide.geeksforgeeks.org/Mvqc0RD70y

The 3rd case: 760399384224 5 1 gives status: time limit exceeded

You can use a simple trick to optimise your code.

Notice that after 4 or 5 numbers you end up in wither of the two cycles {2, 4, 8, 6} or {0}.
For {0}: Just check the sum of the first three digits and you are done!
For the other case: You can first check from where the cycle starts, see how many times the cycle is repeated and multiply that by 20 (2 + 4 + 6 + 8). And add the residual non - repeating bits.