Help solving Mock CCDSAP problems

I was trying to solve mock problems for CCDSAP.

https://www.codechef.com/ALPAST01/problems/BADMATH
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.

https://www.codechef.com/ALPAST01/problems/SMCMP
I tried using Union find but got TLE on subtask 2.

1 Like

For https://www.codechef.com/ALPAST01/problems/SMCMP
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 : https://ideone.com/wgIJnV
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?