Algorithm for counting n-element subsets of m-element set divisible by k -


suppose {1, 2, 3, ..., m} set. choose n distinct elements set. can write algorithm counts number of such subsets sum divisible k (ordering not mattering)?

this problem have been easier if ordering mattered, doesn't , don't have clue how approach. can please help?

this can done in time o(n·k·m) , space o(n·k) method similar outlined below. let s set m elements. definition of set , subset, elements of s distinct, elements of s-subset.

first, consider simpler problem count s-subsets number of elements instead of n elements. let n(w,r) number of w-subsets u such Σu (the sum of elements of u) equal r mod k. if w subset of s, let w' w + z, z ∈ s\w; is, z element of s not in w. n(w', (r+z)%k) = n(w, (r+z)%k) + n(w, r) because n(w, (r+z)%k) number of w'-subsets u Σu≡(r+z)%k) don't contain z , n(w, r) number of w'-subsets u Σu≡(r+z)%k) contain z. repeat construction, treating each element of s in turn until w' = s, @ point desired answer n(s,0). time o(k·m), space o(k).

to adapt above process exact subset sizes, change n(w,r) n(w,h,r), h subset size, , adapt equations n(w',r) n(w',h,r) in obvious way. time o(k·n·m), space o(k·n).


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

java - More than one row with the given identifier was found: 1, for class: com.model.Diagnosis -