Python:A的B次方

题目描述:
Python:A的B次方_第1张图片
错误代码:(c++)
Python:A的B次方_第2张图片

错误代码2:(python)

c = 1e9+7
a,b = list(map(int,input().split()))
print(round((a**b)%c))

以上两份错误代码之所以错误的原因:没考虑相乘过程中会出现大于1e9+7的情况,举个例子:a = 1e8-1, b=1e8-1
这样输入会直接导致程序数量级太大而跑崩,也理所当然在测评机上通过不了。

正确代码:

mod = 1e9+7
a,b = list(map(int,input().split()))
c =a
for i in range(b-1):
    c = (c*a)%mod
print(round(c))

你可能感兴趣的:(python)