Number of subsets with sum divisible by M | Set 2 | C ++ implementation of the approach ; To store the states of DP ; Function to find the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxN  20 NEW_LINE #define maxM  10 NEW_LINE int dp [ maxN ] [ maxM ] ; bool v [ maxN ] [ maxM ] ; int findCnt ( int * arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = 1 ; return dp [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ; } int main ( ) { int arr [ ] = { 3 , 3 , 3 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int m = 6 ; cout << findCnt ( arr , 0 , 0 , n , m ) - 1 ; return 0 ; }