Dynamic programming/Number theory