Feb-28-2017, 08:00 PM
Another way could be using numpy (if your t and n fit into int32) - you are doing exactly same operation on list of numbers, if you transform it to 1-D array, you can manipulate them all at once. I am not sure if there is modular exponentiation in numpy - if not, to avoid overflow (and to speed it) you will need to implement it. With binary exponentiation it could by quite fast.