Skip to content

11.3.2 Sets, logic, and counting

This part of Discrete math 1 problem set: sets, counting, proofs, induction, relations, bijections develops the worked mechanisms below.

Concepts from other sections (15)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Learning objectives

  • Work the set, quantified-logic, and counting problems before revealing solutions.

q1. Let U={1,2,…,10}U = \{1, 2, \dots, 10\}, A={1,2,3,4,5,6}A = \{1, 2, 3, 4, 5, 6\} and B={2,4,6,8}B = \{2, 4, 6, 8\}. (a) A∩BA \cap B. (b) A∖BA \setminus B. (c) The complement of A∪BA \cup B in UU. [set]

Answer

(a) {2,4,6}\{2, 4, 6\}, (b) {1,3,5}\{1, 3, 5\}, (c) {7,9,10}\{7, 9, 10\}. (a) The elements in both: 2,4,62, 4, 6. (b) The elements of AA that are not in BB: 1,3,51, 3, 5. (c) A∪B={1,2,3,4,5,6,8}A \cup B = \{1, 2, 3, 4, 5, 6, 8\}, and the rest of UU is {7,9,10}\{7, 9, 10\}. Common mistakes: the union {1,2,3,4,5,6,8}\{1, 2, 3, 4, 5, 6, 8\} for (a); B∖A={8}B \setminus A = \{8\} for (b), since difference is not symmetric; and {7,8,9,10}\{7, 8, 9, 10\} for (c), the complement of AA alone. These are the operations of the mask algebra in Masks: causal, padding, sliding window, additive in AI Engineering.

q2. How many subsets of {1,2,3,4,5,6}\{1, 2, 3, 4, 5, 6\} contain 11 but not 22? [number]

Answer

1616. The product rule: 1 must be in, 2 must be out, and each of the other 4 elements is independently in or out, so 1⋅1⋅24=161 \cdot 1 \cdot 2^4 = 16. A common mistake is 32, which fixes only one of the two elements.

q3. For propositions PP and QQ: (a) is P⇒QP \Rightarrow Q logically equivalent to ¬Q⇒¬P\lnot Q \Rightarrow \lnot P? (b) Is P⇒QP \Rightarrow Q logically equivalent to Q⇒PQ \Rightarrow P? [bool]

Answer

(a) True, (b) false. (a) The contrapositive has the same truth table as P⇒QP \Rightarrow Q: both are false only when PP is true and QQ is false. (b) The converse is not equivalent: with PP false and QQ true, P⇒QP \Rightarrow Q is true but Q⇒PQ \Rightarrow P is false. The contrapositive is the move behind q18; the converse is the classic slip.

q4. An attention mask is a 0/1 matrix MM whose row ii is a query position and column jj a key position; Mij=1M_{ij} = 1 means “query ii may attend to key jj”. With T=4T = 4 positions, the causal mask allows j≤ij \le i and the padding mask allows j<3j < 3 (position 3 is padding). (a) Give the combined mask (causal AND padding) as a 4×44 \times 4 matrix. [matrix] (b) How many (i,j)(i, j) pairs does it allow? [number]

Answer

(a) (1000110011101110)\begin{pmatrix} 1 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 \\ 1 & 1 & 1 & 0 \\ 1 & 1 & 1 & 0 \end{pmatrix}, (b) 99. (a) Entry (i,j)(i, j) is 1 when j≤ij \le i (causal) and j<3j < 3 (padding): the lower triangle with column 3 cleared. (b) Rows allow 1+2+3+3=91 + 2 + 3 + 3 = 9 pairs. Common mistakes: the causal mask alone, which keeps M33=1M_{33} = 1 (10 pairs) and lets the pad key receive attention; and the upper triangle j≥ij \ge i, which lets a query look at the future.

q5. Which statement is the negation of “for every query row ii there is a key jj with Mij=1M_{ij} = 1”? [choice] (a) There is a row ii such that Mij=0M_{ij} = 0 for every key jj. (b) Mij=0M_{ij} = 0 for every row ii and every key jj. (c) There is a row ii and a key jj with Mij=0M_{ij} = 0. (d) For every row ii there is a key jj with Mij=0M_{ij} = 0.

Answer

(a). The statement is ∀i ∃j Mij=1\forall i\ \exists j\ M_{ij} = 1. Negating swaps each quantifier and negates the predicate: ∃i ∀j Mij=0\exists i\ \forall j\ M_{ij} = 0, a row with no allowed key. That fully masked row is the NaN case of Masks: causal, padding, sliding window, additive and of Stable numerics: logsumexp, softmax, and compensated sums. A common mistake is (c), which negates only the inner predicate and describes any matrix with a single 0; (b) is much stronger than the negation.

q6. Prove De Morgan’s law (A∪B)c=Ac∩Bc(A \cup B)^c = A^c \cap B^c for subsets A,BA, B of a universe UU. [proof]

Answer

Claim: for subsets A,BA, B of a universe UU, (A∪B)c=Ac∩Bc(A \cup B)^c = A^c \cap B^c, where Xc=U∖XX^c = U \setminus X.

Method: show each side is a subset of the other (double inclusion).

(⊆\subseteq) Let x∈(A∪B)cx \in (A \cup B)^c. Then x∈Ux \in U and x∉A∪Bx \notin A \cup B. By the definition of union, x∈A∪Bx \in A \cup B means ”x∈Ax \in A or x∈Bx \in B”, so x∉A∪Bx \notin A \cup B means “not (x∈Ax \in A or x∈Bx \in B)”, which by De Morgan’s law for propositions is ”x∉Ax \notin A and x∉Bx \notin B”. Hence x∈Acx \in A^c and x∈Bcx \in B^c, so x∈Ac∩Bcx \in A^c \cap B^c.

(⊇\supseteq) Let x∈Ac∩Bcx \in A^c \cap B^c. Then x∈Ux \in U, x∉Ax \notin A, and x∉Bx \notin B. So ”x∈Ax \in A or x∈Bx \in B” is false, that is x∉A∪Bx \notin A \cup B, and x∈(A∪B)cx \in (A \cup B)^c.

Each set contains the other, so (A∪B)c=Ac∩Bc(A \cup B)^c = A^c \cap B^c.

Model proof also in q6.md.

q7. (a) In how many ways can you choose 3 of 10 attention heads to prune? (b) How many byte strings of length 4 are there? (c) How many entries does a byte bigram table have (ordered pairs of bytes, repeats allowed)? [number]

Answer

(a) 120120, (b) 4,294,967,2964{,}294{,}967{,}296, (c) 65,53665{,}536. (a) An unordered choice: (103)=10⋅9⋅83!=120\binom{10}{3} = \frac{10 \cdot 9 \cdot 8}{3!} = 120. (b) Four positions with 256 options each: 2564=232256^4 = 2^{32}. (c) Ordered pairs with repetition: 2562=65,536256^2 = 65{,}536. Common mistakes: 720 for (a) counts ordered choices; 1024 for (b) mixes up base and exponent; 32,640 =(2562)= \binom{256}{2} for (c) counts unordered pairs of distinct bytes, but a bigram table needs (a,b)(a, b) and (b,a)(b, a) and (a,a)(a, a).

q8. In how many ways can 5 identical tokens be placed into 3 distinct buckets, empty buckets allowed? [number]

Answer

2121. Stars and bars: 5 stars and 2 bars in a row, (5+3−13−1)=(72)=21\binom{5 + 3 - 1}{3 - 1} = \binom{7}{2} = 21. Common mistakes: 35=2433^5 = 243 treats the tokens as distinct, and (52)=10\binom{5}{2} = 10 forgets that the bars are placed among the stars.

q9. A linear layer y=Wx+by = Wx + b maps Rn\mathbb{R}^n to Rm\mathbb{R}^m. How many parameters does it have? [expr in m, n]

Answer

mn+mmn + m. WW is m×nm \times n, so mnmn weights, plus one bias per output: mm. This is how param_count in Counting: parameters, FLOPs, KV bytes, and memory plans adds biases. Common mistakes: mnmn forgets the bias, and mn+nmn + n sizes the bias by the input.

q10. A language model has an embedding table with VV rows of width dd and an untied output head that maps width dd to VV logits without a bias. How many parameters do the two hold together? [expr in V, d]

Answer

2Vd2Vd. The embedding is a V×dV \times d table, and the untied head is a d→Vd \to V matrix: VdVd each. Tying the two (tie_embeddings in ModelConfig) halves this to VdVd, which is the common wrong answer here.

q11. A decoder block of width dd has four d×dd \times d attention projections (Q,K,V,OQ, K, V, O) without biases, an MLP d→4d→dd \to 4d \to d (two matrices, no biases), and two RMSNorm gain vectors of length dd. How many parameters does the block have? [expr in d]

Answer

12d2+2d12d^2 + 2d. Attention: 4⋅d24 \cdot d^2. MLP: d⋅4d+4d⋅d=8d2d \cdot 4d + 4d \cdot d = 8d^2. Norms: 2d2d. This is where the rule "N≈12Ld2N \approx 12 L d^2" comes from. Common mistakes: 12d212d^2 forgets the norm gains, and 8d2+2d8d^2 + 2d counts only one MLP matrix.

q12. Counting one multiplication and one addition as two floating-point operations, how many FLOPs does the product of an m×km \times k matrix and a k×nk \times n matrix take? [expr in m, k, n]

Answer

2mkn2mkn. CC has mnmn entries, and each is a dot product of length kk: kk multiplications and kk additions into the accumulator, 2k2k FLOPs. That is the 2MKN2MKN of Vectors, matrices, row-major layout, and matmul and of the roofline in the matmul kernel. A common mistake is mknmkn, which counts multiply-adds, not FLOPs.

q13. The training-compute rule of thumb is C≈6NDC \approx 6ND FLOPs for NN parameters and DD tokens. Give CC for N=107N = 10^7 and D=2×108D = 2 \times 10^8. [number]

Answer

1.2×10161.2 \times 10^{16}. 6⋅107⋅2⋅108=12⋅10156 \cdot 10^7 \cdot 2 \cdot 10^8 = 12 \cdot 10^{15}. The 6 is 2 for the forward pass and 4 for the backward pass. A common mistake is 2⋅10152 \cdot 10^{15}, which uses 2ND2ND, the forward pass only.

q14. SmolLM2-135M has 30 layers, 3 key/value heads, and head dimension 64, and caches keys and values in bf16 (2 bytes per number). How many bytes of KV cache does one token take? [number]

Answer

23,04023{,}040. 2⋅L⋅Hkv⋅dh⋅b=2⋅30⋅3⋅64⋅2=23,0402 \cdot L \cdot H_{kv} \cdot d_h \cdot b = 2 \cdot 30 \cdot 3 \cdot 64 \cdot 2 = 23{,}040 bytes. Common mistakes: 11,520 caches keys only (no factor 2), and 69,120 sizes the cache by the 9 query heads instead of the 3 key/value heads, which is exactly what GQA saves.

q15. How many bytes does that cache take for a context of 2048 tokens? [number]

Answer

47,185,92047{,}185{,}920. 23,040⋅2048=47,185,92023{,}040 \cdot 2048 = 47{,}185{,}920 bytes, exactly 45 MiB. Keys only would give half, 23,592,960.

q16. How many integers in {1,2,…,100}\{1, 2, \dots, 100\} are divisible by 3 or by 5? [number]

Answer

4747. Inclusion-exclusion: ⌊100/3⌋+⌊100/5⌋−⌊100/15⌋=33+20−6=47\lfloor 100/3 \rfloor + \lfloor 100/5 \rfloor - \lfloor 100/15 \rfloor = 33 + 20 - 6 = 47. A common mistake is 53, which counts the multiples of 15 twice.

  1. How can a quantified claim be disproved?

    Answer

    Give a counterexample with the required domain membership and show it violates the predicate.

Contents · Next: Proofs, induction, and invariants