solution to SPOJ problems in C++/Python