Help solving Mock CCDSAP problems

I was trying to solve mock problems for CCDSAP.

For this problem, I could not think of any approach other than brute force. I can’t figure out how DP can be applied to solve the problem.

I tried using Union find but got TLE on subtask 2.

1 Like

For CodeChef: Practical coding for everyone
problem you should use Path compression optimization…
https://cp-algorithms.com/data_structures/disjoint_set_union.html

1 Like

Did you figure out BADMATH? if yes can you provide the logic?

1 Like

@hetp111 My Code : wgIJnV - Online C++0x Compiler & Debugging Tool - Ideone.com
I too used Recursion. But, it was partially accepted. (40%), Please see what’s wrong with my approach.

Length of string is 100. makes number to be around 2^100. How do you check divisibility with M?