Part of 2010 Canadian Mathematical Olympiad Qualification Repechage
Source: Canadian RepĂȘchage 2010: Problem 3