From 19f694d1c153f4f7a3db3b8cb6cf128fbc224468 Mon Sep 17 00:00:00 2001 From: Sachet Abeysinghe <155912536+sachet-abey@users.noreply.github.com> Date: Sat, 6 Dec 2025 18:22:15 -0500 Subject: [PATCH 1/6] Create documentation for USACO 1135 problem Added explanation and implementation for USACO problem 1135. --- solutions/silver/usaco-1135.mdx | 84 +++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 solutions/silver/usaco-1135.mdx diff --git a/solutions/silver/usaco-1135.mdx b/solutions/silver/usaco-1135.mdx new file mode 100644 index 0000000000..f0b64a14bb --- /dev/null +++ b/solutions/silver/usaco-1135.mdx @@ -0,0 +1,84 @@ +--- +id: usaco-1135 +source: USACO Silver 2021 US Open +title: Do You Know Your ABCs? +author: Sachet Abeysinghe +--- + +## Explanation + +Due to the low bounds, we can use a brute-force approach. There are seven labels $A$, $B$, $C$, $A + B$, $B + C$, $A + C$, and $A + B + C$. We need exactly $N$ of them, so we can iterate over all combinations of $N$ labels to decide which labels appear in this test case. + +For a chosen set of labels, we need to decide which number corresponds to which label. We iterate over all permutations of the chosen labels, mapping them to the input array in order. Finally, we need to deduce the unique triple $(A, B, C)$ consistent with the mappings. To do this, we track candidate values for $A$, $B$, and $C$ using sets. If each of $A$, $B$, and $C$ has one consistent value, we record the triple as valid. + +## Implementation + +**Time Complexity:** $\mathcal{O}(T\cdot\frac{7!}{\left(7-N\right)!})$ + + + + + +```py +import itertools + +for i in range(int(input())): + N = int(input()) + arr = [int(x) for x in input().split()] + + def deduce_ABC(tags): + a = b = c = ab = bc = ac = abc = -1 + for i in range(N): + value = arr[i] + tag = tags[i] + if tag == "A": a = value + if tag == "B": b = value + if tag == "C": c = value + if tag == "A+B": ab = value + if tag == "B+C": bc = value + if tag == "A+C": ac = value + if tag == "A+B+C": abc = value + + possible_a, possible_b, possible_c = set(), set(), set() + if a != -1: + possible_a.add(a) + if ab != -1: possible_b.add(ab - a) + if ac != -1: possible_c.add(ac - a) + if b != -1: + possible_b.add(b) + if ab != -1: possible_a.add(ab - b) + if bc != -1: possible_c.add(bc - b) + if c != -1: + possible_c.add(c) + if ac != -1: possible_a.add(ac - c) + if bc != -1: possible_b.add(bc - c) + if abc != -1: + if bc != -1: possible_a.add(abc - bc) + if ac != -1: possible_b.add(abc - ac) + if ab != -1: possible_c.add(abc - ab) + + if len(possible_a) != 1 or len(possible_b) != 1 or len(possible_c) != 1: + return + + final_a, final_b, final_c = list(possible_a)[0], list(possible_b)[0], list(possible_c)[0] + if not 1 <= final_a <= final_b <= final_c: + return + if (ac != -1 and final_a + final_c != ac) or (ab != -1 and final_a + final_b != ab) or (bc != -1 and final_b + final_c != bc) or (abc != -1 and final_a + final_b + final_c != abc): + return + return (final_a, final_b, final_c) + + valid_triples = set() + combinations = itertools.combinations(["A", "B", "C", "A+B", "B+C", "A+C", "A+B+C"], N) + for combination in combinations: + permutations = itertools.permutations(combination) + for permutation in permutations: + triple = deduce_ABC(permutation) + if triple != None: + valid_triples.add(triple) + + print(len(valid_triples)) +``` + + + + From 3c6ba1f166192d371b4864fc78b1fcd50125fdb5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 6 Dec 2025 23:27:39 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/usaco-1135.mdx | 135 +++++++++++++++++++------------- 1 file changed, 81 insertions(+), 54 deletions(-) diff --git a/solutions/silver/usaco-1135.mdx b/solutions/silver/usaco-1135.mdx index f0b64a14bb..0fd38e373e 100644 --- a/solutions/silver/usaco-1135.mdx +++ b/solutions/silver/usaco-1135.mdx @@ -23,60 +23,87 @@ For a chosen set of labels, we need to decide which number corresponds to which import itertools for i in range(int(input())): - N = int(input()) - arr = [int(x) for x in input().split()] - - def deduce_ABC(tags): - a = b = c = ab = bc = ac = abc = -1 - for i in range(N): - value = arr[i] - tag = tags[i] - if tag == "A": a = value - if tag == "B": b = value - if tag == "C": c = value - if tag == "A+B": ab = value - if tag == "B+C": bc = value - if tag == "A+C": ac = value - if tag == "A+B+C": abc = value - - possible_a, possible_b, possible_c = set(), set(), set() - if a != -1: - possible_a.add(a) - if ab != -1: possible_b.add(ab - a) - if ac != -1: possible_c.add(ac - a) - if b != -1: - possible_b.add(b) - if ab != -1: possible_a.add(ab - b) - if bc != -1: possible_c.add(bc - b) - if c != -1: - possible_c.add(c) - if ac != -1: possible_a.add(ac - c) - if bc != -1: possible_b.add(bc - c) - if abc != -1: - if bc != -1: possible_a.add(abc - bc) - if ac != -1: possible_b.add(abc - ac) - if ab != -1: possible_c.add(abc - ab) - - if len(possible_a) != 1 or len(possible_b) != 1 or len(possible_c) != 1: - return - - final_a, final_b, final_c = list(possible_a)[0], list(possible_b)[0], list(possible_c)[0] - if not 1 <= final_a <= final_b <= final_c: - return - if (ac != -1 and final_a + final_c != ac) or (ab != -1 and final_a + final_b != ab) or (bc != -1 and final_b + final_c != bc) or (abc != -1 and final_a + final_b + final_c != abc): - return - return (final_a, final_b, final_c) - - valid_triples = set() - combinations = itertools.combinations(["A", "B", "C", "A+B", "B+C", "A+C", "A+B+C"], N) - for combination in combinations: - permutations = itertools.permutations(combination) - for permutation in permutations: - triple = deduce_ABC(permutation) - if triple != None: - valid_triples.add(triple) - - print(len(valid_triples)) + N = int(input()) + arr = [int(x) for x in input().split()] + + def deduce_ABC(tags): + a = b = c = ab = bc = ac = abc = -1 + for i in range(N): + value = arr[i] + tag = tags[i] + if tag == "A": + a = value + if tag == "B": + b = value + if tag == "C": + c = value + if tag == "A+B": + ab = value + if tag == "B+C": + bc = value + if tag == "A+C": + ac = value + if tag == "A+B+C": + abc = value + + possible_a, possible_b, possible_c = set(), set(), set() + if a != -1: + possible_a.add(a) + if ab != -1: + possible_b.add(ab - a) + if ac != -1: + possible_c.add(ac - a) + if b != -1: + possible_b.add(b) + if ab != -1: + possible_a.add(ab - b) + if bc != -1: + possible_c.add(bc - b) + if c != -1: + possible_c.add(c) + if ac != -1: + possible_a.add(ac - c) + if bc != -1: + possible_b.add(bc - c) + if abc != -1: + if bc != -1: + possible_a.add(abc - bc) + if ac != -1: + possible_b.add(abc - ac) + if ab != -1: + possible_c.add(abc - ab) + + if len(possible_a) != 1 or len(possible_b) != 1 or len(possible_c) != 1: + return + + final_a, final_b, final_c = ( + list(possible_a)[0], + list(possible_b)[0], + list(possible_c)[0], + ) + if not 1 <= final_a <= final_b <= final_c: + return + if ( + (ac != -1 and final_a + final_c != ac) + or (ab != -1 and final_a + final_b != ab) + or (bc != -1 and final_b + final_c != bc) + or (abc != -1 and final_a + final_b + final_c != abc) + ): + return + return (final_a, final_b, final_c) + + valid_triples = set() + combinations = itertools.combinations( + ["A", "B", "C", "A+B", "B+C", "A+C", "A+B+C"], N + ) + for combination in combinations: + permutations = itertools.permutations(combination) + for permutation in permutations: + triple = deduce_ABC(permutation) + if triple != None: + valid_triples.add(triple) + + print(len(valid_triples)) ``` From 6086aa18a78c3f70d6452b1cf0f5514613f23dca Mon Sep 17 00:00:00 2001 From: Sachet Abeysinghe <155912536+sachet-abey@users.noreply.github.com> Date: Sat, 6 Dec 2025 18:33:39 -0500 Subject: [PATCH 3/6] Update problem name and solution metadata --- content/extraProblems.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/content/extraProblems.json b/content/extraProblems.json index d06be79bf4..b58d83c22f 100644 --- a/content/extraProblems.json +++ b/content/extraProblems.json @@ -371,15 +371,14 @@ }, { "uniqueId": "usaco-1135", - "name": "ABC", + "name": "Do You Know Your ABCs?", "url": "http://www.usaco.org/index.php?page=viewproblem2&cpid=1135", "source": "Silver", "difficulty": "Normal", "isStarred": false, "tags": ["Set"], "solutionMetadata": { - "kind": "USACO", - "usacoId": "1135" + "kind": "internal" } }, { From 48005b182f731f9a4e8c1e1d929b19bc9bc5338d Mon Sep 17 00:00:00 2001 From: Sachet Abeysinghe <155912536+sachet-abey@users.noreply.github.com> Date: Tue, 9 Dec 2025 17:20:54 -0500 Subject: [PATCH 4/6] Add official analysis link to usaco-1135.mdx Added a link to the official analysis for the problem. --- solutions/silver/usaco-1135.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/solutions/silver/usaco-1135.mdx b/solutions/silver/usaco-1135.mdx index 0fd38e373e..7a0e79617c 100644 --- a/solutions/silver/usaco-1135.mdx +++ b/solutions/silver/usaco-1135.mdx @@ -5,6 +5,8 @@ title: Do You Know Your ABCs? author: Sachet Abeysinghe --- +[Official Analysis (C++, Java)](https://usaco.org/current/data/sol_prob2_silver_open21.html) + ## Explanation Due to the low bounds, we can use a brute-force approach. There are seven labels $A$, $B$, $C$, $A + B$, $B + C$, $A + C$, and $A + B + C$. We need exactly $N$ of them, so we can iterate over all combinations of $N$ labels to decide which labels appear in this test case. From a016a892f25c1f190c5d5a4c894c9b80db1b0c3c Mon Sep 17 00:00:00 2001 From: Sachet Abeysinghe <155912536+sachet-abey@users.noreply.github.com> Date: Tue, 9 Dec 2025 17:33:23 -0500 Subject: [PATCH 5/6] Update usaco-1135.mdx --- solutions/silver/usaco-1135.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/solutions/silver/usaco-1135.mdx b/solutions/silver/usaco-1135.mdx index 7a0e79617c..a74b8e56ab 100644 --- a/solutions/silver/usaco-1135.mdx +++ b/solutions/silver/usaco-1135.mdx @@ -13,6 +13,8 @@ Due to the low bounds, we can use a brute-force approach. There are seven labels For a chosen set of labels, we need to decide which number corresponds to which label. We iterate over all permutations of the chosen labels, mapping them to the input array in order. Finally, we need to deduce the unique triple $(A, B, C)$ consistent with the mappings. To do this, we track candidate values for $A$, $B$, and $C$ using sets. If each of $A$, $B$, and $C$ has one consistent value, we record the triple as valid. +We can always deduce a possible value for each of $A$, $B$, and $C$, because each variable appears in four of the seven labels. At most three of these labels can be missing, so we can use the remaining label to determine the variable's value. + ## Implementation **Time Complexity:** $\mathcal{O}(T\cdot\frac{7!}{\left(7-N\right)!})$ From 92e7a3545bafbb151048d9eaab84569b68ced2c7 Mon Sep 17 00:00:00 2001 From: David Guo Date: Thu, 11 Dec 2025 19:05:12 -0800 Subject: [PATCH 6/6] change TC to O(T) since n is so small --- solutions/silver/usaco-1135.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/usaco-1135.mdx b/solutions/silver/usaco-1135.mdx index a74b8e56ab..3785c45c60 100644 --- a/solutions/silver/usaco-1135.mdx +++ b/solutions/silver/usaco-1135.mdx @@ -17,7 +17,7 @@ We can always deduce a possible value for each of $A$, $B$, and $C$, because eac ## Implementation -**Time Complexity:** $\mathcal{O}(T\cdot\frac{7!}{\left(7-N\right)!})$ +**Time Complexity:** $\mathcal{O}(T)$