This problem would have been a classical problem if the memory limits would not have been that tight. In the classical version we can use DP or the LCS with O(n^2) memory but this problem requires O(n) memory.
How to go about it?
Is there a similar problem on SPOJ/Codechef/IARCS?
I would prefer not to keep creating accounts on new websites :|
In Q.17, I think there is an error.
(14^12345678987654321) % 41 is actually equal to 14, and not 31.