当a和b都很大时(按10 ** 10到10 ** 18的顺序),如何计算a ** b?

时间:2019-01-16 15:44:29

标签: python

我需要计算((2**a)*(a))%1000000007,其中a的顺序为10 ^ 10。据我所知,python可以处理小于2 ^ 1400的int。那么有什么方法可以计算出来吗?或任何数学规则来解决?

1 个答案:

答案 0 :(得分:0)

这非常容易,因为python的math.pow具有用于模数的可选参数。

from math import pow
(pow(2, a, 1000000007)*a)%1000000007

这让您不必对所有数字都使用2**a,而只返回模数。