I have an array A of length N of negative as well as positive integers. I need to count the number of subsets in this array which add up to a multiple of a number M (or 0 (mod M))
For example:
Let A = {1,2,8,4,5}, M = 9,
Then, there are 4 such subsets:
- {}: Empty set, corresponding to the multiple 0,
- {1,8}: corresponding to the multiple 9,
- {4,5}: corresponding to the multiple 9
- {1,8,4,5}: corresponding to the multiple 18.
I thought of generating all possible multiples and then applying dynamic programming subset sum, but the constraints won't allow me that.
Constraints:
1 =< N <= 10^5,
1 =< M <= 100,
-10^9 =< each entry of array <=10^9
What should be my approach for this sort of problem?