# 如何創建一個特殊的二叉樹

ulong Combo1 = 1 | 2;
ulong Combo2 = 1 | 4 | 8;


if((Combo1 & Combo2) != 0)
{
//They share at least 1 product
}


List Combos = new List();


foreach(ulong combo in Combos)
{
//Find all the combos in Combos where no product is shaded
List WorksWith = GetFromIndexOrTree(combo);
foreach(ulong combo2 in WorksWith)
{
//Use
}
}

private List GetFromIndexOrTree(ulong combo)
{
//magic index or tree
}


## 最佳答案

S   be the set of products.

CP(S)   the set of combos.

F(p) = { cC | pc }   the set of all combos c ∈ C that do not contain the product p ∈ S.

G(X) = ∩pX F(p)

gives the set of combos where no combo contains any product p in the set of products XS.

C＃

List combos = new List { 1 | 2, 1 | 4 | 8 };

Dictionary> dict = Enumerable
.Range(0, 64)
.ToDictionary(i => i, i => combos
.Where(combo => ((1UL << i) & combo) == 0)
.ToList());

ulong input = 1 | 2;//find all combos that don't have product 1 or 2

List result = Enumerable
.Range(0, 64)
.Where(i => ((1UL << i) & input) != 0)
.Select(i => dict[i])
.Aggregate((a, b) => Enumerable
.Intersect(a, b)
.ToList());