Agent Work: HW7: Dynamic Programming
Claude Opus 4.6 · COMP 182: Algorithmic Thinking
COMP 182: Algorithmic Thinking
Homework 7: Counting
Spring 2023
This homework is divided into three distinct parts. When you submit, you will need a file for each part.
---
Part 1
Problem 1 [7 pts]
How many bit strings of length 10 either start with 000 or end with 111?
Show your work.
---
Problem 2 [7 pts]
How many positive integers less than 1,000,000 have exactly one digit equal to 9 and have a sum of digits equal to 13?
Show your work.
---
Problem 3 [7 pts]
How many solutions are there to the inequality
$$x_1 + x_2 + x_3 \leq 11,$$
where $x_1$, $x_2$, and $x_3$ are nonnegative integers?
Show your work.
---
Problem 4 [8 pts]
Prove, without using induction, that in any set of $n + 1$ positive integers not exceeding $2n$ there must be two that are relatively prime.
---
Part 2
Problem 1 [8 pts]
Suppose that $p$ and $q$ are prime numbers, that $p \neq q$, and that $n = pq$. What is the number of positive integers not exceeding $n$ that are relatively prime to $n$?
Show your work.
---
Problem 2 [21 pts]
In celebration of the end of the semester, all the students and instructors in COMP 182 were invited to take a group picture. Assume that there are $n$ students and $m$ instructors. Exactly one instructor is designated as the lead instructor.
Show your work in each part.
(a) [7 pts]
How many ways are there to arrange all the students and instructors in a line such that all students are standing next to each other (no instructors between them)?
(b) [7 pts]
How many ways are there to arrange all the students and instructors in a line such that no two instructors are standing next to each other (the number of students in the course is so large that it allows us to consider this!)?
(c) [7 pts]
How many ways are there to arrange all the students and instructors in a line such that no student is standing next to the lead instructor?
---
Part 3
Problem 1 [14 pts]
How many ways are there to place 80 identical balls in 5 distinct bins such that no bin has more than 24 balls?
---
Problem 2 [14 pts]
What is the number of permutations of the letters in the word INDISTINGUISHABLE such that the strings GIT, TIN, and NAB do not appear in the permutation?
---
Problem 3 [14 pts]
How many 1-1 functions $f : \{1, \ldots, 20\} \to \{1, \ldots, 80\}$ without a fixed point are there?
A function $f : A \to B$ has a fixed point if there exists an $a \in A$ such that $f(a) = a$.
---
Point Distribution Summary
| Part | Problem | Points |
|---|---|---|
| 1 | 1 | 7 |
| 1 | 2 | 7 |
| 1 | 3 | 7 |
| 1 | 4 | 8 |
| 2 | 1 | 8 |
| 2 | 2a | 7 |
| 2 | 2b | 7 |
| 2 | 2c | 7 |
| 3 | 1 | 14 |
| 3 | 2 | 14 |
| 3 | 3 | 14 |
| Total | 100 |
# COMP182 Homework 7
## Student Responses
<!-- Write your solutions below. Add sections for each problem as needed. -->
<!-- Use proper mathematical notation where applicable. -->
[Your solutions here]
COMP182 Homework 7: Counting
Part 1
Problem 1 [7 pts]
How many bit strings of length 10 either start with 000 or end with 111?
We use the inclusion-exclusion principle.
Let $A$ = set of bit strings of length 10 that start with 000. Let $B$ = set of bit strings of length 10 that end with 111.
- $|A|$: The first 3 bits are fixed as 000, and the remaining 7 bits can be anything. So $|A| = 2^7 = 128$.
- $|B|$: The last 3 bits are fixed as 111, and the remaining 7 bits can be anything. So $|B| = 2^7 = 128$.
- $|A \cap B|$: The first 3 bits are 000 and the last 3 bits are 111. The middle 4 bits can be anything. So $|A \cap B| = 2^4 = 16$.
By inclusion-exclusion:
$$|A \cup B| = |A| + |B| - |A \cap B| = 128 + 128 - 16 = \boxed{240}$$
---
Problem 2 [7 pts]
How many positive integers less than 1,000,000 have exactly one digit equal to 9 and have a sum of digits equal to 13?
Positive integers less than 1,000,000 have at most 6 digits. We represent each such integer as a 6-digit string $(d_1, d_2, d_3, d_4, d_5, d_6)$ with leading zeros allowed, where $0 \le d_i \le 9$ and $d_1 + d_2 + \cdots + d_6 = 13$. (Note: 000000 = 0 is excluded, but it does not satisfy our conditions anyway.)
Step 1: Choose which digit equals 9. There are $\binom{6}{1} = 6$ ways to choose which one of the 6 positions has the digit 9.
Step 2: Count arrangements of the remaining 5 digits. The remaining 5 digits must satisfy:
- Sum to $13 - 9 = 4$
- Each digit is between 0 and 8 (not 9, since exactly one digit equals 9)
Since the sum is 4 and each digit is at most 8, the upper bound constraint $d_i \le 8$ is automatically satisfied (the maximum any single digit can be is 4). So we just need the number of nonneg integer solutions to $e_1 + e_2 + e_3 + e_4 + e_5 = 4$.
By stars and bars: $\binom{4 + 5 - 1}{5 - 1} = \binom{8}{4} = 70$.
Total: $6 \times 70 = \boxed{420}$
---
Problem 3 [7 pts]
How many solutions are there to $x_1 + x_2 + x_3 \le 11$, where $x_1, x_2, x_3$ are nonneg integers?
Introduce a slack variable $x_4 \ge 0$ to convert the inequality to an equality:
$$x_1 + x_2 + x_3 + x_4 = 11$$
where $x_1, x_2, x_3, x_4$ are all nonneg integers.
By stars and bars, the number of solutions is:
$$\binom{11 + 4 - 1}{4 - 1} = \binom{14}{3} = \frac{14 \times 13 \times 12}{3!} = \frac{2184}{6} = \boxed{364}$$
---
Problem 4 [8 pts]
Prove that in any set of $n+1$ positive integers not exceeding $2n$, there must be two that are relatively prime.
Proof. Partition the integers $\{1, 2, \ldots, 2n\}$ into $n$ pairs of consecutive integers:
$$\{1, 2\}, \{3, 4\}, \{5, 6\}, \ldots, \{2n-1, 2n\}.$$
There are exactly $n$ such pairs. If we choose any $n+1$ integers from $\{1, 2, \ldots, 2n\}$, then by the Pigeonhole Principle, at least two of the chosen integers must belong to the same pair $\{2k-1, 2k\}$ for some $k$.
These two integers are consecutive, namely $2k-1$ and $2k$. We claim they are relatively prime. Indeed, if $d = \gcd(2k-1, 2k)$, then $d$ divides both $2k$ and $2k-1$, so $d$ divides their difference:
$$d \mid (2k - (2k-1)) = 1.$$
Therefore $d = 1$, and the two integers are relatively prime.
Since any set of $n+1$ positive integers not exceeding $2n$ must contain two consecutive integers, it must contain two integers that are relatively prime. $\blacksquare$
---
Part 2
Problem 1 [8 pts]
Given distinct primes $p$ and $q$ with $n = pq$, find the number of positive integers not exceeding $n$ that are relatively prime to $n$.
This is Euler's totient function $\varphi(n) = \varphi(pq)$.
A positive integer $k \le n = pq$ is not relatively prime to $n$ if and only if $p \mid k$ or $q \mid k$.
By inclusion-exclusion, the number of integers in $\{1, \ldots, pq\}$ divisible by $p$ or $q$ is:
- Multiples of $p$ in $\{1, \ldots, pq\}$: there are $\frac{pq}{p} = q$ of them.
- Multiples of $q$ in $\{1, \ldots, pq\}$: there are $\frac{pq}{q} = p$ of them.
- Multiples of $pq$ in $\{1, \ldots, pq\}$: there is $\frac{pq}{pq} = 1$ of them.
Number divisible by $p$ or $q$ $= q + p - 1$.
Therefore:
$$\varphi(pq) = pq - (p + q - 1) = pq - p - q + 1 = \boxed{(p-1)(q-1)}$$
---
Problem 2(a) [7 pts]
How many ways to arrange $n$ students and $m$ instructors in a line such that all students are standing next to each other?
Treat the $n$ students as a single block. Then we have $(m + 1)$ entities to arrange in a line: the student block and $m$ instructors.
- These $(m+1)$ entities can be arranged in $(m+1)!$ ways.
- Within the student block, the $n$ students can be arranged in $n!$ ways.
$$\text{Total} = \boxed{(m+1)! \cdot n!}$$
---
Problem 2(b) [7 pts]
How many ways to arrange $n$ students and $m$ instructors in a line such that no two instructors are standing next to each other?
Step 1: Arrange the $n$ students in a line: $n!$ ways.
Step 2: This creates $(n+1)$ gaps (including the two ends):
$$\_ \; S_1 \; \_ \; S_2 \; \_ \; \cdots \; \_ \; S_n \; \_$$
Step 3: Choose $m$ of these $(n+1)$ gaps in which to place the $m$ instructors (at most one per gap ensures no two instructors are adjacent): $\binom{n+1}{m}$ ways.
Step 4: Arrange the $m$ instructors in the chosen gaps: $m!$ ways.
$$\text{Total} = n! \cdot \binom{n+1}{m} \cdot m! = \boxed{\frac{n! \cdot (n+1)!}{(n+1-m)!}}$$
---
Problem 2(c) [7 pts]
How many ways to arrange $n$ students and $m$ instructors in a line such that no student is standing next to the lead instructor?
Let $L$ denote the lead instructor. We count directly by considering where $L$ is placed and ensuring both neighbors of $L$ (if they exist) are instructors, not students.
Case 1: $L$ is at one of the two ends (position 1 or position $n+m$). $L$ has exactly one neighbor, which must be one of the $(m-1)$ other instructors.
- 2 choices for which end $L$ occupies.
- $(m-1)$ choices for which instructor is $L$'s neighbor.
- The remaining $(n+m-2)$ people can be arranged in the remaining positions in $(n+m-2)!$ ways.
- Count: $2(m-1)(n+m-2)!$
Case 2: $L$ is at an interior position. $L$ has two neighbors, both of which must be instructors.
- $(n+m-2)$ choices for $L$'s interior position.
- Choose 2 of the $(m-1)$ other instructors for $L$'s neighbors, with order (left vs. right): $P(m-1, 2) = (m-1)(m-2)$ ways.
- The remaining $(n+m-3)$ people fill the remaining positions: $(n+m-3)!$ ways.
- Count: $(n+m-2)(m-1)(m-2)(n+m-3)! = (m-1)(m-2)(n+m-2)!$
Total:
$$2(m-1)(n+m-2)! + (m-1)(m-2)(n+m-2)!$$ $$= (m-1)(n+m-2)!\bigl[2 + (m-2)\bigr]$$ $$= \boxed{m(m-1)(n+m-2)!}$$
*Verification with $n=1, m=2$: The formula gives $2 \cdot 1 \cdot 1! = 2$. The valid arrangements of (S, L, I) with no student next to L are: S-I-L and L-I-S, which is indeed 2.* $\checkmark$
---
Part 3
Problem 1 [14 pts]
How many ways to place 80 identical balls in 5 distinct bins such that no bin has more than 24 balls?
We need the number of nonneg integer solutions to $x_1 + x_2 + x_3 + x_4 + x_5 = 80$ with $0 \le x_i \le 24$ for all $i$.
Using inclusion-exclusion on the upper bound constraints:
Let $A_i$ be the set of solutions where $x_i \ge 25$. We want to subtract these "bad" solutions.
For $|A_i|$: substitute $y_i = x_i - 25 \ge 0$, giving a sum of $80 - 25 = 55$ with 5 nonneg variables: $$|A_i| = \binom{55 + 4}{4} = \binom{59}{4}$$
For $|A_i \cap A_j|$: two variables $\ge 25$, so sum becomes $80 - 50 = 30$: $$|A_i \cap A_j| = \binom{30 + 4}{4} = \binom{34}{4}$$
For $|A_i \cap A_j \cap A_k|$: three variables $\ge 25$, sum becomes $80 - 75 = 5$: $$|A_i \cap A_j \cap A_k| = \binom{5 + 4}{4} = \binom{9}{4}$$
For four or more: $80 - 100 < 0$, so impossible. These terms are 0.
By inclusion-exclusion:
$$\text{Answer} = \binom{84}{4} - \binom{5}{1}\binom{59}{4} + \binom{5}{2}\binom{34}{4} - \binom{5}{3}\binom{9}{4}$$
Computing each term:
- $\binom{84}{4} = \frac{84 \cdot 83 \cdot 82 \cdot 81}{24} = \frac{46{,}308{,}024}{24} = 1{,}929{,}501$
- $\binom{59}{4} = \frac{59 \cdot 58 \cdot 57 \cdot 56}{24} = \frac{10{,}923{,}024}{24} = 455{,}126$
- $\binom{34}{4} = \frac{34 \cdot 33 \cdot 32 \cdot 31}{24} = \frac{1{,}113{,}024}{24} = 46{,}376$
- $\binom{9}{4} = 126$
$$= 1{,}929{,}501 - 5(455{,}126) + 10(46{,}376) - 10(126)$$ $$= 1{,}929{,}501 - 2{,}275{,}630 + 463{,}760 - 1{,}260$$ $$= \boxed{116{,}371}$$
---
Problem 2 [14 pts]
Number of permutations of the letters in INDISTINGUISHABLE such that the strings GIT, TIN, and NAB do not appear as consecutive substrings.
Letter frequencies in INDISTINGUISHABLE (17 letters):
| Letter | Count |
|---|---|
| I | 4 |
| N | 2 |
| S | 2 |
| A, B, D, E, G, H, L, T, U | 1 each |
Total unrestricted permutations: $\frac{17!}{4! \cdot 2! \cdot 2!} = \frac{17!}{96}$
We use inclusion-exclusion. Let:
- $A$ = permutations containing GIT as a consecutive substring
- $B$ = permutations containing TIN as a consecutive substring
- $C$ = permutations containing NAB as a consecutive substring
Key observation: Since we have only 1 copy of G, T, and B, each pattern can appear at most once in any permutation.
Single sets (treat each substring as a block, reducing 17 items to 15):
- $|A|$: Block GIT uses G(1), I(1), T(1). Remaining: I(3), N(2), S(2), plus 8 unique letters + block = 15 items.
$$|A| = \frac{15!}{3! \cdot 2! \cdot 2!} = \frac{15!}{24}$$
- $|B|$: Block TIN uses T(1), I(1), N(1). Remaining: I(3), N(1), S(2), plus 8 unique + block = 15 items.
$$|B| = \frac{15!}{3! \cdot 2!} = \frac{15!}{12}$$
- $|C|$: Block NAB uses N(1), A(1), B(1). Remaining: I(4), N(1), S(2), plus 7 unique + block = 15 items.
$$|C| = \frac{15!}{4! \cdot 2!} = \frac{15!}{48}$$
Pairwise intersections:
- $|A \cap B|$ (both GIT and TIN): GIT and TIN share the letter T (only 1 T available), so they cannot appear separately. They must overlap, forming GITIN (G-I-T-I-N, sharing the T). Block uses G(1), I(2), T(1), N(1). Remaining: I(2), N(1), S(2), plus 7 unique + block = 13 items.
$$|A \cap B| = \frac{13!}{2! \cdot 2!} = \frac{13!}{4}$$
- $|A \cap C|$ (both GIT and NAB): GIT and NAB share no letters, so they cannot overlap. Two separate blocks. Used: G(1), I(1), T(1), N(1), A(1), B(1). Remaining: I(3), N(1), S(2), plus 6 unique + 2 blocks = 13 items.
$$|A \cap C| = \frac{13!}{3! \cdot 2!} = \frac{13!}{12}$$
- $|B \cap C|$ (both TIN and NAB): TIN ends with N, NAB starts with N. Two sub-cases:
Sub-case 1 (non-overlapping): TIN and NAB appear separately. Uses T(1), I(1), N(2), A(1), B(1). We have N(2), so this works. Remaining: I(3), S(2), plus 6 unique + 2 blocks = 13 items. $$\frac{13!}{3! \cdot 2!} = \frac{13!}{12}$$
Sub-case 2 (overlapping as TINAB): Block uses T(1), I(1), N(1), A(1), B(1). Remaining: I(3), N(1), S(2), plus 6 unique + block = 13 items. $$\frac{13!}{3! \cdot 2!} = \frac{13!}{12}$$
$$|B \cap C| = \frac{13!}{12} + \frac{13!}{12} = \frac{13!}{6}$$
Triple intersection $|A \cap B \cap C|$:
From above, $A \cap B$ requires the GITIN block. We also need NAB. GITIN ends with N, NAB starts with N, so again two sub-cases:
Sub-case 1 (non-overlapping GITIN and NAB): Uses N(2) total. Remaining: I(2), S(2), plus 5 unique + 2 blocks = 11 items. $$\frac{11!}{2! \cdot 2!} = \frac{11!}{4}$$
Sub-case 2 (overlapping as GITINAB): Uses N(1). Remaining: I(2), N(1), S(2), plus 5 unique + block = 11 items. $$\frac{11!}{2! \cdot 2!} = \frac{11!}{4}$$
$$|A \cap B \cap C| = \frac{11!}{4} + \frac{11!}{4} = \frac{11!}{2}$$
Applying inclusion-exclusion:
$$|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|$$
Computing numerically:
| Term | Expression | Value | ||
|---|---|---|---|---|
| $\ | A\ | $ | $15!/24$ | $54{,}486{,}432{,}000$ |
| $\ | B\ | $ | $15!/12$ | $108{,}972{,}864{,}000$ |
| $\ | C\ | $ | $15!/48$ | $27{,}243{,}216{,}000$ |
| $\ | A \cap B\ | $ | $13!/4$ | $1{,}556{,}755{,}200$ |
| $\ | A \cap C\ | $ | $13!/12$ | $518{,}918{,}400$ |
| $\ | B \cap C\ | $ | $13!/6$ | $1{,}037{,}836{,}800$ |
| $\ | A \cap B \cap C\ | $ | $11!/2$ | $19{,}958{,}400$ |
$$|A \cup B \cup C| = (54{,}486{,}432{,}000 + 108{,}972{,}864{,}000 + 27{,}243{,}216{,}000)$$ $$- (1{,}556{,}755{,}200 + 518{,}918{,}400 + 1{,}037{,}836{,}800) + 19{,}958{,}400$$ $$= 190{,}702{,}512{,}000 - 3{,}113{,}510{,}400 + 19{,}958{,}400 = 187{,}608{,}960{,}000$$
Answer:
$$\frac{17!}{96} - |A \cup B \cup C| = 3{,}705{,}077{,}376{,}000 - 187{,}608{,}960{,}000 = \boxed{3{,}517{,}468{,}416{,}000}$$
---
Problem 3 [14 pts]
How many injective (1-1) functions $f : \{1, \ldots, 20\} \to \{1, \ldots, 80\}$ have no fixed point?
A fixed point exists when $f(a) = a$ for some $a \in \{1, \ldots, 20\}$ (note: since $\{1,\ldots,20\} \subset \{1,\ldots,80\}$, fixed points are possible for all domain elements).
Using inclusion-exclusion. Let $A_i$ be the set of injections where $f(i) = i$, for $i = 1, \ldots, 20$.
For any subset $S \subseteq \{1,\ldots,20\}$ with $|S| = k$: fix $f(i) = i$ for all $i \in S$. The remaining $20 - k$ elements must map injectively to $\{1,\ldots,80\} \setminus S$, which has $80 - k$ elements. The number of such injections is:
$$P(80-k, 20-k) = \frac{(80-k)!}{60!}$$
(since $(80-k) - (20-k) = 60$).
There are $\binom{20}{k}$ choices for $S$, so:
$$\left|\bigcap_{i \in S} A_i\right| = \frac{(80-k)!}{60!}$$
By inclusion-exclusion, the number of injections with no fixed point is:
$$\sum_{k=0}^{20} (-1)^k \binom{20}{k} \frac{(80-k)!}{60!}$$
Equivalently, factoring out:
$$= \frac{1}{60!} \sum_{k=0}^{20} (-1)^k \binom{20}{k} (80-k)!$$
$$= \boxed{\sum_{k=0}^{20} (-1)^k \binom{20}{k} \frac{(80-k)!}{60!}}$$
*Verification with small example: $f: \{1\} \to \{1,2\}$ with no fixed point. Formula gives $\binom{1}{0}\frac{2!}{1!} - \binom{1}{1}\frac{1!}{1!} = 2 - 1 = 1$, which is correct ($f(1) = 2$ is the only option).* $\checkmark$
COMP 182 Homework 7: Grading Report
---
Part 1
Problem 1 [7/7]
- Logic (4/4): Correctly defines sets $A$ (start with 000) and $B$ (end with 111), applies inclusion-exclusion with proper computation of $|A|=128$, $|B|=128$, $|A \cap B|=16$.
- Final answer (2/2): $\boxed{240}$ is correct.
- Work shown (1/1): Each step is clearly explained.
Problem 2 [7/7]
- Logic (4/4): The student takes an elegant alternative approach: represent every integer less than 1,000,000 as a 6-digit string (with leading zeros), choose 1 of 6 positions for the digit 9, then count nonneg integer solutions to the remaining 5 digits summing to 4 via stars and bars. The key insight that the upper bound of 8 is automatically satisfied (since the max any single remaining digit can be is 4) is correct and well-argued. This avoids the more complex case analysis of the reference solution.
- Final answer (2/2): $6 \times 70 = \boxed{420}$ matches the reference.
- Work shown (1/1): Clear two-step reasoning.
Problem 3 [7/7]
- Logic (4/4): Uses the slack variable method (the alternative approach noted in the rubric) to convert the inequality to $x_1 + x_2 + x_3 + x_4 = 11$ with all $x_i \geq 0$.
- Final answer (2/2): $\binom{14}{3} = \boxed{364}$ is correct.
- Work shown (1/1): Computation is explicit.
Problem 4 [8/8]
- Full credit (8/8): The proof is correct, complete, and arguably more elegant than the reference solution. The student partitions $\{1, \ldots, 2n\}$ into $n$ pairs of consecutive integers $\{1,2\}, \{3,4\}, \ldots, \{2n-1,2n\}$, applies the Pigeonhole Principle to conclude two chosen integers must lie in the same pair, then proves consecutive integers are coprime via the standard gcd divisibility argument. This is a clean, rigorous proof without induction. $\blacksquare$
---
Part 2
Problem 1 [8/8]
- Full credit (8/8): Correctly applies inclusion-exclusion to count integers divisible by $p$ or $q$ ($q + p - 1$), then subtracts from $pq$ to get $\varphi(pq) = (p-1)(q-1)$. All steps are correct and clearly shown.
Problem 2(a) [7/7]
- Logic (3/3): Correctly treats $n$ students as a single block, yielding $(m+1)$ entities.
- Final answer (2/2): $(m+1)! \cdot n!$ is correct.
- Work shown (2/2): Clear explanation of both the block arrangement and internal student arrangement.
Problem 2(b) [7/7]
- Logic (3/3): Correctly uses the gaps method — arrange $n$ students first creating $(n+1)$ gaps, choose $m$ gaps for instructors.
- Final answer (2/2): $n! \cdot \binom{n+1}{m} \cdot m!$ matches the reference. The equivalent form $\frac{n! \cdot (n+1)!}{(n+1-m)!}$ is also correct.
- Work shown (2/2): Clear step-by-step reasoning.
Problem 2(c) [7/7]
- Logic (3/3): Correctly splits into two cases (lead instructor at an end vs. interior), requiring neighbors to be other instructors.
- Final answer (2/2): The student simplifies to $m(m-1)(n+m-2)!$, which is algebraically equivalent to the reference answer $2(m-1)(n+m-2)! + (n+m-2) \cdot P(m-1,2) \cdot (n+m-3)!$. Verification: $2(m-1)(n+m-2)! + (m-1)(m-2)(n+m-2)! = (m-1)(n+m-2)![2+m-2] = m(m-1)(n+m-2)!$. ✓
- Work shown (2/2): Includes a nice verification with $n=1, m=2$ giving $2$, which is confirmed by enumeration.
---
Part 3
Problem 1 [14/14]
- Identifies inclusion-exclusion (3/3): Correctly sets up the problem with $A_i$ as the set of solutions where $x_i \geq 25$.
- $|A_i|$ (2/2): $\binom{59}{4}$ — correct (substitute $y_i = x_i - 25$, sum becomes 55).
- $|A_i \cap A_j|$ (2/2): $\binom{34}{4}$ — correct (sum becomes 30).
- $|A_i \cap A_j \cap A_k|$ (2/2): $\binom{9}{4}$ — correct (sum becomes 5).
- Higher-order terms empty (2/2): Correctly identifies $80 - 100 < 0$.
- Final answer (2/2): $1{,}929{,}501 - 2{,}275{,}630 + 463{,}760 - 1{,}260 = \boxed{116{,}371}$. All intermediate computations verified correct.
- Work shown (1/1): Every binomial coefficient is computed explicitly.
Problem 2 [14/14]
- Total permutations (2/2): $\frac{17!}{4! \cdot 2! \cdot 2!}$ with correct letter frequencies.
- $|A|, |B|, |C|$ (3/3): All three are correctly computed with proper accounting of reduced letter frequencies after forming each block.
- Overlap identification (3/3): Correctly identifies that $A \cap B$ must overlap (only one T available, forming GITIN), $A \cap C$ cannot overlap (no shared letters), and $B \cap C$ can either overlap (TINAB) or not (uses both N's).
- Pairwise intersections (3/3): $|A \cap B| = 13!/4$, $|A \cap C| = 13!/12$, $|B \cap C| = 13!/12 + 13!/12 = 13!/6$ — all correct.
- Triple intersection (2/2): $|A \cap B \cap C| = 11!/4 + 11!/4 = 11!/2$ — correctly handles both sub-cases (non-overlapping GITIN + NAB, and overlapping GITINAB).
- Work shown (1/1): Exceptionally detailed with a numerical computation table.
Final numerical answer: $3{,}517{,}468{,}416{,}000$. ✓
Problem 3 [14/14]
- Total 1-1 functions (3/3): $\frac{80!}{60!}$ — correct.
- Inclusion-exclusion setup (3/3): Properly defines $A_i$ as the set of injections with $f(i) = i$.
- $|A_i|$ (2/2): $\frac{79!}{60!}$ — correct.
- Intersection of $j$ sets (3/3): $\frac{(80-k)!}{60!}$ for $k$ fixed points — correct with clear justification (codomain and domain each shrink by $k$, difference remains 60).
- Final formula (2/2): $\sum_{k=0}^{20} (-1)^k \binom{20}{k} \frac{(80-k)!}{60!}$ — correct.
- Work shown (1/1): Includes a verification with a small example ($f: \{1\} \to \{1,2\}$) that confirms the formula.
---
Summary
| Part | Problem | Max Points | Score |
|---|---|---|---|
| 1 | 1 | 7 | 7 |
| 1 | 2 | 7 | 7 |
| 1 | 3 | 7 | 7 |
| 1 | 4 | 8 | 8 |
| 2 | 1 | 8 | 8 |
| 2 | 2a | 7 | 7 |
| 2 | 2b | 7 | 7 |
| 2 | 2c | 7 | 7 |
| 3 | 1 | 14 | 14 |
| 3 | 2 | 14 | 14 |
| 3 | 3 | 14 | 14 |
| Total | 100 | 100 |
Overall Comments: This is an outstanding submission. Every problem is solved correctly with clear, well-organized work. The student demonstrates strong command of combinatorial techniques including inclusion-exclusion, stars and bars, the Pigeonhole Principle, and Euler's totient function. Several solutions use elegant alternative approaches (e.g., the 6-digit string representation in P1.2, the Pigeonhole-based proof in P1.4, the simplified closed form in P2.2c) and include verification steps that demonstrate deep understanding. The handling of the complex overlap cases in P3.2 is particularly impressive.
Total: 100/100
diff --git a/writeup.md b/writeup.md
index b1e2070..139190b 100644
--- a/writeup.md
+++ b/writeup.md
@@ -1,9 +1,327 @@
-# COMP182 Homework 7
+# COMP182 Homework 7: Counting
-## Student Responses
+## Part 1
-<!-- Write your solutions below. Add sections for each problem as needed. -->
-<!-- Use proper mathematical notation where applicable. -->
+### Problem 1 [7 pts]
-[Your solutions here]
+**How many bit strings of length 10 either start with 000 or end with 111?**
+We use the inclusion-exclusion principle.
+
+Let $A$ = set of bit strings of length 10 that start with 000.
+Let $B$ = set of bit strings of length 10 that end with 111.
+
+- $|A|$: The first 3 bits are fixed as 000, and the remaining 7 bits can be anything. So $|A| = 2^7 = 128$.
+- $|B|$: The last 3 bits are fixed as 111, and the remaining 7 bits can be anything. So $|B| = 2^7 = 128$.
+- $|A \cap B|$: The first 3 bits are 000 and the last 3 bits are 111. The middle 4 bits can be anything. So $|A \cap B| = 2^4 = 16$.
+
+By inclusion-exclusion:
+
+$$|A \cup B| = |A| + |B| - |A \cap B| = 128 + 128 - 16 = \boxed{240}$$
+
+---
+
+### Problem 2 [7 pts]
+
+**How many positive integers less than 1,000,000 have exactly one digit equal to 9 and have a sum of digits equal to 13?**
+
+Positive integers less than 1,000,000 have at most 6 digits. We represent each such integer as a 6-digit string $(d_1, d_2, d_3, d_4, d_5, d_6)$ with leading zeros allowed, where $0 \le d_i \le 9$ and $d_1 + d_2 + \cdots + d_6 = 13$. (Note: 000000 = 0 is excluded, but it does not satisfy our conditions anyway.)
+
+**Step 1: Choose which digit equals 9.**
+There are $\binom{6}{1} = 6$ ways to choose which one of the 6 positions has the digit 9.
+
+**Step 2: Count arrangements of the remaining 5 digits.**
+The remaining 5 digits must satisfy:
+- Sum to $13 - 9 = 4$
+- Each digit is between 0 and 8 (not 9, since exactly one digit equals 9)
+
+Since the sum is 4 and each digit is at most 8, the upper bound constraint $d_i \le 8$ is automatically satisfied (the maximum any single digit can be is 4). So we just need the number of nonneg integer solutions to $e_1 + e_2 + e_3 + e_4 + e_5 = 4$.
+
+By stars and bars: $\binom{4 + 5 - 1}{5 - 1} = \binom{8}{4} = 70$.
+
+**Total:** $6 \times 70 = \boxed{420}$
+
+---
+
+### Problem 3 [7 pts]
+
+**How many solutions are there to $x_1 + x_2 + x_3 \le 11$, where $x_1, x_2, x_3$ are nonneg integers?**
+
+Introduce a slack variable $x_4 \ge 0$ to convert the inequality to an equality:
+
+$$x_1 + x_2 + x_3 + x_4 = 11$$
+
+where $x_1, x_2, x_3, x_4$ are all nonneg integers.
+
+By stars and bars, the number of solutions is:
+
+$$\binom{11 + 4 - 1}{4 - 1} = \binom{14}{3} = \frac{14 \times 13 \times 12}{3!} = \frac{2184}{6} = \boxed{364}$$
+
+---
+
+### Problem 4 [8 pts]
+
+**Prove that in any set of $n+1$ positive integers not exceeding $2n$, there must be two that are relatively prime.**
+
+**Proof.** Partition the integers $\{1, 2, \ldots, 2n\}$ into $n$ pairs of consecutive integers:
+
+$$\{1, 2\}, \{3, 4\}, \{5, 6\}, \ldots, \{2n-1, 2n\}.$$
+
+There are exactly $n$ such pairs. If we choose any $n+1$ integers from $\{1, 2, \ldots, 2n\}$, then by the **Pigeonhole Principle**, at least two of the chosen integers must belong to the same pair $\{2k-1, 2k\}$ for some $k$.
+
+These two integers are consecutive, namely $2k-1$ and $2k$. We claim they are relatively prime. Indeed, if $d = \gcd(2k-1, 2k)$, then $d$ divides both $2k$ and $2k-1$, so $d$ divides their difference:
+
+$$d \mid (2k - (2k-1)) = 1.$$
+
+Therefore $d = 1$, and the two integers are relatively prime.
+
+Since any set of $n+1$ positive integers not exceeding $2n$ must contain two consecutive integers, it must contain two integers that are relatively prime. $\blacksquare$
+
+---
+
+## Part 2
+
+### Problem 1 [8 pts]
+
+**Given distinct primes $p$ and $q$ with $n = pq$, find the number of positive integers not exceeding $n$ that are relatively prime to $n$.**
+
+This is Euler's totient function $\varphi(n) = \varphi(pq)$.
+
+A positive integer $k \le n = pq$ is **not** relatively prime to $n$ if and only if $p \mid k$ or $q \mid k$.
+
+By inclusion-exclusion, the number of integers in $\{1, \ldots, pq\}$ divisible by $p$ or $q$ is:
+
+- Multiples of $p$ in $\{1, \ldots, pq\}$: there are $\frac{pq}{p} = q$ of them.
+- Multiples of $q$ in $\{1, \ldots, pq\}$: there are $\frac{pq}{q} = p$ of them.
+- Multiples of $pq$ in $\{1, \ldots, pq\}$: there is $\frac{pq}{pq} = 1$ of them.
+
+Number divisible by $p$ or $q$ $= q + p - 1$.
+
+Therefore:
+
+$$\varphi(pq) = pq - (p + q - 1) = pq - p - q + 1 = \boxed{(p-1)(q-1)}$$
+
+---
+
+### Problem 2(a) [7 pts]
+
+**How many ways to arrange $n$ students and $m$ instructors in a line such that all students are standing next to each other?**
+
+Treat the $n$ students as a single block. Then we have $(m + 1)$ entities to arrange in a line: the student block and $m$ instructors.
+
+- These $(m+1)$ entities can be arranged in $(m+1)!$ ways.
+- Within the student block, the $n$ students can be arranged in $n!$ ways.
+
+$$\text{Total} = \boxed{(m+1)! \cdot n!}$$
+
+---
+
+### Problem 2(b) [7 pts]
+
+**How many ways to arrange $n$ students and $m$ instructors in a line such that no two instructors are standing next to each other?**
+
+**Step 1:** Arrange the $n$ students in a line: $n!$ ways.
+
+**Step 2:** This creates $(n+1)$ gaps (including the two ends):
+
+$$\_ \; S_1 \; \_ \; S_2 \; \_ \; \cdots \; \_ \; S_n \; \_$$
+
+**Step 3:** Choose $m$ of these $(n+1)$ gaps in which to place the $m$ instructors (at most one per gap ensures no two instructors are adjacent): $\binom{n+1}{m}$ ways.
+
+**Step 4:** Arrange the $m$ instructors in the chosen gaps: $m!$ ways.
+
+$$\text{Total} = n! \cdot \binom{n+1}{m} \cdot m! = \boxed{\frac{n! \cdot (n+1)!}{(n+1-m)!}}$$
+
+---
+
+### Problem 2(c) [7 pts]
+
+**How many ways to arrange $n$ students and $m$ instructors in a line such that no student is standing next to the lead instructor?**
+
+Let $L$ denote the lead instructor. We count directly by considering where $L$ is placed and ensuring both neighbors of $L$ (if they exist) are instructors, not students.
+
+**Case 1: $L$ is at one of the two ends (position 1 or position $n+m$).**
+$L$ has exactly one neighbor, which must be one of the $(m-1)$ other instructors.
+- 2 choices for which end $L$ occupies.
+- $(m-1)$ choices for which instructor is $L$'s neighbor.
+- The remaining $(n+m-2)$ people can be arranged in the remaining positions in $(n+m-2)!$ ways.
+- Count: $2(m-1)(n+m-2)!$
+
+**Case 2: $L$ is at an interior position.**
+$L$ has two neighbors, both of which must be instructors.
+- $(n+m-2)$ choices for $L$'s interior position.
+- Choose 2 of the $(m-1)$ other instructors for $L$'s neighbors, with order (left vs. right): $P(m-1, 2) = (m-1)(m-2)$ ways.
+- The remaining $(n+m-3)$ people fill the remaining positions: $(n+m-3)!$ ways.
+- Count: $(n+m-2)(m-1)(m-2)(n+m-3)! = (m-1)(m-2)(n+m-2)!$
+
+**Total:**
+
+$$2(m-1)(n+m-2)! + (m-1)(m-2)(n+m-2)!$$
+$$= (m-1)(n+m-2)!\bigl[2 + (m-2)\bigr]$$
+$$= \boxed{m(m-1)(n+m-2)!}$$
+
+*Verification with $n=1, m=2$: The formula gives $2 \cdot 1 \cdot 1! = 2$. The valid arrangements of (S, L, I) with no student next to L are: S-I-L and L-I-S, which is indeed 2.* $\checkmark$
+
+---
+
+## Part 3
+
+### Problem 1 [14 pts]
+
+**How many ways to place 80 identical balls in 5 distinct bins such that no bin has more than 24 balls?**
+
+We need the number of nonneg integer solutions to $x_1 + x_2 + x_3 + x_4 + x_5 = 80$ with $0 \le x_i \le 24$ for all $i$.
+
+**Using inclusion-exclusion on the upper bound constraints:**
+
+Let $A_i$ be the set of solutions where $x_i \ge 25$. We want to subtract these "bad" solutions.
+
+For $|A_i|$: substitute $y_i = x_i - 25 \ge 0$, giving a sum of $80 - 25 = 55$ with 5 nonneg variables:
+$$|A_i| = \binom{55 + 4}{4} = \binom{59}{4}$$
+
+For $|A_i \cap A_j|$: two variables $\ge 25$, so sum becomes $80 - 50 = 30$:
+$$|A_i \cap A_j| = \binom{30 + 4}{4} = \binom{34}{4}$$
+
+For $|A_i \cap A_j \cap A_k|$: three variables $\ge 25$, sum becomes $80 - 75 = 5$:
+$$|A_i \cap A_j \cap A_k| = \binom{5 + 4}{4} = \binom{9}{4}$$
+
+For four or more: $80 - 100 < 0$, so impossible. These terms are 0.
+
+By inclusion-exclusion:
+
+$$\text{Answer} = \binom{84}{4} - \binom{5}{1}\binom{59}{4} + \binom{5}{2}\binom{34}{4} - \binom{5}{3}\binom{9}{4}$$
+
+Computing each term:
+
+- $\binom{84}{4} = \frac{84 \cdot 83 \cdot 82 \cdot 81}{24} = \frac{46{,}308{,}024}{24} = 1{,}929{,}501$
+
+- $\binom{59}{4} = \frac{59 \cdot 58 \cdot 57 \cdot 56}{24} = \frac{10{,}923{,}024}{24} = 455{,}126$
+
+- $\binom{34}{4} = \frac{34 \cdot 33 \cdot 32 \cdot 31}{24} = \frac{1{,}113{,}024}{24} = 46{,}376$
+
+- $\binom{9}{4} = 126$
+
+$$= 1{,}929{,}501 - 5(455{,}126) + 10(46{,}376) - 10(126)$$
+$$= 1{,}929{,}501 - 2{,}275{,}630 + 463{,}760 - 1{,}260$$
+$$= \boxed{116{,}371}$$
+
+---
+
+### Problem 2 [14 pts]
+
+**Number of permutations of the letters in INDISTINGUISHABLE such that the strings GIT, TIN, and NAB do not appear as consecutive substrings.**
+
+**Letter frequencies in INDISTINGUISHABLE (17 letters):**
+
+| Letter | Count |
+|--------|-------|
+| I | 4 |
+| N | 2 |
+| S | 2 |
+| A, B, D, E, G, H, L, T, U | 1 each |
+
+Total unrestricted permutations: $\frac{17!}{4! \cdot 2! \cdot 2!} = \frac{17!}{96}$
+
+We use inclusion-exclusion. Let:
+- $A$ = permutations containing GIT as a consecutive substring
+- $B$ = permutations containing TIN as a consecutive substring
+- $C$ = permutations containing NAB as a consecutive substring
+
+**Key observation:** Since we have only 1 copy of G, T, and B, each pattern can appear **at most once** in any permutation.
+
+**Single sets** (treat each substring as a block, reducing 17 items to 15):
+
+- $|A|$: Block GIT uses G(1), I(1), T(1). Remaining: I(3), N(2), S(2), plus 8 unique letters + block = 15 items.
+$$|A| = \frac{15!}{3! \cdot 2! \cdot 2!} = \frac{15!}{24}$$
+
+- $|B|$: Block TIN uses T(1), I(1), N(1). Remaining: I(3), N(1), S(2), plus 8 unique + block = 15 items.
+$$|B| = \frac{15!}{3! \cdot 2!} = \frac{15!}{12}$$
+
+- $|C|$: Block NAB uses N(1), A(1), B(1). Remaining: I(4), N(1), S(2), plus 7 unique + block = 15 items.
+$$|C| = \frac{15!}{4! \cdot 2!} = \frac{15!}{48}$$
+
+**Pairwise intersections:**
+
+- $|A \cap B|$ (both GIT and TIN): GIT and TIN share the letter T (only 1 T available), so they **cannot** appear separately. They must overlap, forming **GITIN** (G-I-T-I-N, sharing the T). Block uses G(1), I(2), T(1), N(1). Remaining: I(2), N(1), S(2), plus 7 unique + block = 13 items.
+$$|A \cap B| = \frac{13!}{2! \cdot 2!} = \frac{13!}{4}$$
+
+- $|A \cap C|$ (both GIT and NAB): GIT and NAB share no letters, so they cannot overlap. Two separate blocks. Used: G(1), I(1), T(1), N(1), A(1), B(1). Remaining: I(3), N(1), S(2), plus 6 unique + 2 blocks = 13 items.
+$$|A \cap C| = \frac{13!}{3! \cdot 2!} = \frac{13!}{12}$$
+
+- $|B \cap C|$ (both TIN and NAB): TIN ends with N, NAB starts with N. Two sub-cases:
+
+ **Sub-case 1 (non-overlapping):** TIN and NAB appear separately. Uses T(1), I(1), N(2), A(1), B(1). We have N(2), so this works. Remaining: I(3), S(2), plus 6 unique + 2 blocks = 13 items.
+ $$\frac{13!}{3! \cdot 2!} = \frac{13!}{12}$$
+
+ **Sub-case 2 (overlapping as TINAB):** Block uses T(1), I(1), N(1), A(1), B(1). Remaining: I(3), N(1), S(2), plus 6 unique + block = 13 items.
+ $$\frac{13!}{3! \cdot 2!} = \frac{13!}{12}$$
+
+ $$|B \cap C| = \frac{13!}{12} + \frac{13!}{12} = \frac{13!}{6}$$
+
+**Triple intersection $|A \cap B \cap C|$:**
+
+From above, $A \cap B$ requires the GITIN block. We also need NAB. GITIN ends with N, NAB starts with N, so again two sub-cases:
+
+**Sub-case 1 (non-overlapping GITIN and NAB):** Uses N(2) total. Remaining: I(2), S(2), plus 5 unique + 2 blocks = 11 items.
+$$\frac{11!}{2! \cdot 2!} = \frac{11!}{4}$$
+
+**Sub-case 2 (overlapping as GITINAB):** Uses N(1). Remaining: I(2), N(1), S(2), plus 5 unique + block = 11 items.
+$$\frac{11!}{2! \cdot 2!} = \frac{11!}{4}$$
+
+$$|A \cap B \cap C| = \frac{11!}{4} + \frac{11!}{4} = \frac{11!}{2}$$
+
+**Applying inclusion-exclusion:**
+
+$$|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|$$
+
+Computing numerically:
+
+| Term | Expression | Value |
+|------|-----------|-------|
+| $\|A\|$ | $15!/24$ | $54{,}486{,}432{,}000$ |
+| $\|B\|$ | $15!/12$ | $108{,}972{,}864{,}000$ |
+| $\|C\|$ | $15!/48$ | $27{,}243{,}216{,}000$ |
+| $\|A \cap B\|$ | $13!/4$ | $1{,}556{,}755{,}200$ |
+| $\|A \cap C\|$ | $13!/12$ | $518{,}918{,}400$ |
+| $\|B \cap C\|$ | $13!/6$ | $1{,}037{,}836{,}800$ |
+| $\|A \cap B \cap C\|$ | $11!/2$ | $19{,}958{,}400$ |
+
+$$|A \cup B \cup C| = (54{,}486{,}432{,}000 + 108{,}972{,}864{,}000 + 27{,}243{,}216{,}000)$$
+$$- (1{,}556{,}755{,}200 + 518{,}918{,}400 + 1{,}037{,}836{,}800) + 19{,}958{,}400$$
+$$= 190{,}702{,}512{,}000 - 3{,}113{,}510{,}400 + 19{,}958{,}400 = 187{,}608{,}960{,}000$$
+
+**Answer:**
+
+$$\frac{17!}{96} - |A \cup B \cup C| = 3{,}705{,}077{,}376{,}000 - 187{,}608{,}960{,}000 = \boxed{3{,}517{,}468{,}416{,}000}$$
+
+---
+
+### Problem 3 [14 pts]
+
+**How many injective (1-1) functions $f : \{1, \ldots, 20\} \to \{1, \ldots, 80\}$ have no fixed point?**
+
+A fixed point exists when $f(a) = a$ for some $a \in \{1, \ldots, 20\}$ (note: since $\{1,\ldots,20\} \subset \{1,\ldots,80\}$, fixed points are possible for all domain elements).
+
+**Using inclusion-exclusion.** Let $A_i$ be the set of injections where $f(i) = i$, for $i = 1, \ldots, 20$.
+
+For any subset $S \subseteq \{1,\ldots,20\}$ with $|S| = k$: fix $f(i) = i$ for all $i \in S$. The remaining $20 - k$ elements must map injectively to $\{1,\ldots,80\} \setminus S$, which has $80 - k$ elements. The number of such injections is:
+
+$$P(80-k, 20-k) = \frac{(80-k)!}{60!}$$
+
+(since $(80-k) - (20-k) = 60$).
+
+There are $\binom{20}{k}$ choices for $S$, so:
+
+$$\left|\bigcap_{i \in S} A_i\right| = \frac{(80-k)!}{60!}$$
+
+By inclusion-exclusion, the number of injections with **no** fixed point is:
+
+$$\sum_{k=0}^{20} (-1)^k \binom{20}{k} \frac{(80-k)!}{60!}$$
+
+Equivalently, factoring out:
+
+$$= \frac{1}{60!} \sum_{k=0}^{20} (-1)^k \binom{20}{k} (80-k)!$$
+
+$$= \boxed{\sum_{k=0}^{20} (-1)^k \binom{20}{k} \frac{(80-k)!}{60!}}$$
+
+*Verification with small example: $f: \{1\} \to \{1,2\}$ with no fixed point. Formula gives $\binom{1}{0}\frac{2!}{1!} - \binom{1}{1}\frac{1!}{1!} = 2 - 1 = 1$, which is correct ($f(1) = 2$ is the only option).* $\checkmark$
Sub-Model Usage
| Model | Input | Output | Cache Read | Cost |
|---|---|---|---|---|
| claude-opus-4-6 | 7 | 30.8K | 245.3K | $1.17 |
BSCS Bench