and this: http://acm.uva.es/problemset/v3/332.html

~Yogi.