http://www.codechef.com/APRIL14/problems/ANUCBC

Input:
There are 'n' bags, and each bag has a number, a[i]. Now there are q queries, each query is a number 'm' and the answer is the number of subsets such that the sum is exactly divisible by m.

Solution:
Step 1: We make an array mod_num[m], such that mod_num[i] stores the number of a[j]'s (numbers in the bags) such that a[j]%m = i. In essence, for each value of remainder, we store the number of numbers that would give us that remainder. For example, suppose,
        n  =  10
        m  =  5
        The numbers are 2, 3, -1, 8, 10
        So, our mod_num array would be,
        mod_num[0] = 1 {10}
        mod_num[1] = 0
        mod_num[2] = 1 {2}
        mod_num[3] = 2 {3, 8}
        mod_num[4] = 1 {-1}

Step 2: Now, for each set that would give us a remainder i, we calculate the number of ways, we could select numbers from that set to get remainders 0 to m-1. For example, for the set of three remainder from the above example, i.e {3, 8} we could get remainders 3 {{3}, {8}} and a remainder 1 {3, 8}. Thus we construct this set which stores the number od sets formed from the set of each remainder to get the desired remainders.

Step 3: Now we use dynamic programming. We define a state dp[i][j] such as,
dp[i][j] = the number of subsets formed from the numbers with remainders less than i mod m (using the array constructed in the first step), such that the remainder is j. Now to achieve this, we can do it as,
dp[i][j] = dp[i-1][(j-i*0)%m]*ncr(mod_num[i], 0) + dp[i-1][(j-i*1)%m]*ncr(mod_num[i], 1) + dp[i-1][(j-i*mod_num[i])%m]*ncr(mod_num[i], mod_num[i])
where, ncr(n,r) means the number of ways to choose r objects from n objects.

But this can be reduced by precomputation we did in step 2.

The loop in the code can be understood as follows:
for each i (the remainder set in step 1):
    for each j (the remainder we want to get including the current set i)
        for each k (using the set of numbers for remainder i, that can yield a remainder k)
            to the set of dp[i][j] we add, the new sets.