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.
21
24
22
25
Verbally it can be stated as following:
@@ -80,8 +83,14 @@ for (int i = 1; i < VALMAX; i++) {
80
83
81
84
<FocusProblem problem="SQFREE" />
82
85
86
+
## Explanation
87
+
83
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$.
84
89
90
+
## Implementation
91
+
92
+
**Time Complexity:**$\mathcal{O}(V \logV+T \cdot \sqrt{n})$, where $V= 1e7$
93
+
85
94
<LanguageSection>
86
95
<CPPSection>
87
96
@@ -126,11 +135,17 @@ int main() {
126
135
127
136
<FocusProblem problem="cow" />
128
137
138
+
## Explanation
139
+
129
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:
$dp[i][mask] = \text{the number of strings of length i that match all the patterns in set, but none other patterns. } $Therecurrenceis:
@@ -215,6 +232,10 @@ dp[i][mask \& j]=dp[i-1][j]\text{ where j is a set of patterns that match charac
215
232
$$
216
233
Thefollowingcodeillustratesthis:
217
234
235
+
## Implementation
236
+
237
+
**TimeComplexity:**$\mathcal{O}(M \cdotN \cdot2^N)$, where $M$ is size of each strings in patterns and $N$ is the size of patterns.
238
+
218
239
<LanguageSection>
219
240
<CPPSection>
220
241
@@ -253,6 +274,8 @@ int howMany(vector<string> patterns, int k) {
253
274
</CPPSection>
254
275
</LanguageSection>
255
276
277
+
## Explanation 2
278
+
256
279
The problem can also be solved using the inclusion exclusion principle.
257
280
258
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