Let's just focus on subtask 2, since subtask 1's solution is essentially the same.

Hint 1

Hint 2

Hint 3

Solution

We're essentially just creating a Hamming code.

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!