You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The inclusion-exclusion principle relates to finding the size of the union of some sets.
20
24
21
25
Verbally it can be stated as following:
@@ -79,8 +83,14 @@ for (int i = 1; i < VALMAX; i++) {
79
83
80
84
<FocusProblem problem="SQFREE" />
81
85
86
+
## Explanation
87
+
82
88
A perfect application for inclusion-exclusion principle and mobius function. In this particular case the set $A_i$ - previously mentioned in the tutorial section - denotes how many numbers are divisible with $i^2$ and we're asked to find out $\bigg| \bigcup_{i=1}^{\sqrt{n}} A_i \bigg|$. The precomputed mobius array tells whether to add or subtract $A_i$.
83
89
90
+
## Implementation
91
+
92
+
**Time Complexity:**$\mathcal{O}(V \logV+T \cdot \sqrt{n})$, where $V= 1e7$
93
+
84
94
<LanguageSection>
85
95
<CPPSection>
86
96
@@ -125,11 +135,17 @@ int main() {
125
135
126
136
<FocusProblem problem="cow" />
127
137
138
+
## Explanation
139
+
128
140
In this particular case the set $A_i$ - previously mentioned in the tutorial section - denotes how many pairs of cows have at least $i$ ice cream flavors in common. From the total number of pairs subtract the union of $A_i$. The global answer is:
Adynamicprogrammingapproachwithbitmaskingwouldlooklikethis: $dp[i][mask] = \text{the number of strings of length i that match all the patterns in set, but none other patterns. } $Therecurrenceis:
if (__builtin_popcount(mask) == k) { ans = (ans + dp[m - 1][mask]) % MOD; }
242
268
}
243
269
244
270
return ans;
@@ -248,6 +274,8 @@ int howMany(vector<string> patterns, int k) {
248
274
</CPPSection>
249
275
</LanguageSection>
250
276
277
+
## Explanation 2
278
+
251
279
The problem can also be solved using the inclusion exclusion principle.
252
280
253
281
An important observation is that we can easily count the strings that satisfy some specific patterns. Simply iterate through the positions of all patterns. If all the patterns contain $?$ then we can use any letter from $a$ to $z$ giving us $26$ solution, otherwise we can only put the fixed letter contained by a pattern. The answer is the product.
0 commit comments