Problem 31: solved, probably not as optimized as it could be, omitted many print statements after successful solve