Part of 2009 Canadian Mathematical Olympiad Qualification Repechage
Source: Canadian Repêchage 2009: Problem 9