Part of 2004 239 Open Mathematical Olympiad
Source: 239MO 2004, grade 8-9, problem 5, grade 10-11, problem 4