既然选择了python,就要承担超时的风险,过了三个点
#快速幂算法+高精度乘法
import math
ans=[0]*501
bot=[0]*501
ans[0]=1
bot[0]=2
#计算位数
def count(n):
return int(math.log10(2)*n)+1
#快速幂算法
def fast_pow(a,b):
ans=1
while b:
if b%2==1:
mult1()
mult2()
b=b//2
#对于高精度乘法 只在乎后500位
#高精度乘法 equal to ans*=a
def mult1():
global ans
global bot
res=[0]*501
for i in range(500):
for j in range(500-i):
res[i+j]+=ans[i]*bot[j]
res[i+j+1]+=res[i+j]//10
res[i+j]=res[i+j]%10
ans=res.copy()
#equal to a*=a
def mult2():
global bot
res=[0]*501
for i in range(500):
for j in range(500-i):
res[i+j]+=bot[i]*bot[j]
res[i+j+1]+=res[i+j]//10
res[i+j]=res[i+j]%10
bot=res.copy()
if __name__=="__main__":
n=int(input())
print(count(n))
fast_pow(2,n)
ans[0]-=1
for i in range(499,-1,-1):
print(ans[i],end="")
if i%50==0:
print()
洛谷示例
编译器结果
尝试用C++重构,直接过了