My solutions for task I solved on http://www.spoj.com/