algorithm - dynamic programming technique with inclusion-exclusion -
there n soldiers (numbered 1 n). each soldier has subset of skills out of m different skills (numbered 1 m). skill-set of army union of skill-sets of constituent soldiers. how many different subsets of soldiers satisfy there have specific skill-set requirement problem
according explanation problem reduces finding number of subsets of these numbers or equal required value, req
let f(i) number of numbers j such j or = i.then answer is
∑i(−1)^popcount(i xor req)(2^f(i)−1) such or req req
how formula comes , how popcount describe added or subtracted.
here take @ it. way solve it: instead of reasoning on groups have set of skills, let's reason on groups of soldiers do not have set of skills.
because second property propagates group members.
before start, little simplification:
without loss of generality, can filter out soldiers @ least 1 skill outside required set. because skills of subset must required set of skills, smart soldiers know never needed.
from on, assume required skill set {1, ... , r}
, skills of soldiers between 1
, r
.
we going need definition of a_i
(where i
belongs {1,..,r}
):
a_i
: set of groups of soldiers have skill i
in group skill set.
in other words, group in a_i
(inter
means intersection) group @ least 1 soldier has skill number i
.
now if i<j
,
a_i inter a_j
= groups of soldiers have skills i
, j
in group skill set.
in other words, group in a_i inter a_j
group @ least 1 of properties
- one soldier both skills number
i
,j
. - two soldiers, 1 skill
i
, 1 skillj
.
and
a_1 inter a_2 ... inter a_r
is set of groups of soldiers have skills 1,.., r
in group skill set.
the question problem asking can expressed a_i
:
what size of a_1 inter a_2 ... inter a_r
?
it equal to
2^2^r - size( comp(a_1) union comp(a_2) ... union comp(a_r) )
where comp(a)
complementary set.
now, can apply inclusion-exclusion principle.
there 1 more trick make computable:
if i
s subset of {1,..,r}
, groups in
intersection(comp(a_j) j in i)
are groups of soldiers no skill (as group) in i
. note soldier in such group soldier not have skill in i
. let define g(i)
number of soldier not have skill in set i
. that, have
size( intersection(comp(a_j) j in i) ) = 2^g(i)
in end, result have is:
2^2^r - sum( (-1)^(size(i)+1) * 2^g(i) , each subset of {1,..,r} , non-empty)
i hope helps. let me know if still unclear.
Comments
Post a Comment