First lets solve more simple task of counting number of elements not present in your sets. This task can be reworded in more simple form - instead of 100,000 sets you can think about 1 set which contains all your numbers. Then number of elements not present in this set is x = 1000000 - len(set)
. Now you can use this number x
to count number of combinations. With repetitions: x * x
, without repetitions: x * (x - 1)
. So bottom line of my answer is to put all your numbers in one big set and use it's length to find number of combinations using combinatorics.
Update
So above we have a way to find number of combinations where each element in combination is not in any of the sets. But question was to find number of combinations where each combination is not present in any of the sets.
Lets try to solve simpler problem first:
- your sets have all numbers in them, none missing
- each number is present exactly in one set, no duplicates across sets
How you would construct such combinations over such sets? You would simply pick two elements from different sets and resulting combination would not be in any of the sets. Number of such combinations could be counted using following code (it accepts sizes of the sets):
int count_combinations(vector<int>& buckets) {
int result = 0;
for (int i=0; i < buckets.size(); ++i) {
for (int j=i+1; j < buckets.size(); ++j) {
result += buckets[i] * buckets[j];
}
}
return result;
}
Now let's imagine that some numbers are missing. Then we can just add additional set with those missing numbers to our sets (as a separate set). But we also need to account that given there were n
missing numbers there would be n * (n-1)
combinations constructed using only these missing numbers. So following code will produce total number of combinations with account to missing numbers:
int missing_numbers = upper_bound - all_numbers.size() - 1;
int missing_combinations = missing_numbers * (missing_numbers - 1);
return missing_combinations + count_combinations(sets, missing_numbers);
Now lets imagine we have a duplicate across two sets: {a, b, c}
, {a, d}
.
What types of errors they will introduce? Following pairs: {a, a}
- repetition, {a, d}
- combination which is present in second set.
So how to treat such duplicates? We need to eliminate them completely from all sets. Even single instance of a duplicate will produce combination present in some set. Because we can just pick any element from the set where duplicate was removed and produce such combination (in my example - if we will keep a
in first set, then pick d
from the second to produce {a, d}
, if we will keep a
in second set, then pick b
or c
from the first to produce {a, b}
and {a, c}
). So duplicates shall be removed.
Update
However we can't simply remove all duplicates, consider this counterexample:
{a, b}
{a, c}
{d}
. If we simply remove a
we will acquire {b}
{c}
{d}
and lost information about not-existing combination {a, d}
. Consider another counterexample:
{a, b}
{a, b, c}
{b, d}
. If we simply remove duplicates we will acquire {c}
{d}
and lost information about {a, d}
.
Also we can't simply apply such logic to pairs of sets, a simple counter example for numbers < 3: {1, 2}
{1}
{2}
. Here number of missing combinations is 0, but we will incorrectly count in {1, 2}
if we will apply duplicates removal to pair of sets. Bottom line is that I can't come up with good technique which will help to correctly handle duplicate elements across sets.
{m, n}
. I'm under an impression that you're looking for two numbers that don't exist in any sets. – Leben Asa