解题思路:
注意事项:
参考代码:
n,m,p = map(int,input().split())
# 定义快速幂,k为取模的时候使用
def fp(base, power:int, k=float('inf')):
res = 1
while power>0:
if power&1:
res *= base%k
power >>= 1
base *= base%k
return res
# 矩阵点乘
def matrix_m(n,m):
R = [[0]*len(m[0]) for i in range(len(n))]
for a in range(len(n)):
for b in range(len(m[0])):
total = 0
for c in range(len(n[0])):
total += n[a][c]*m[c][b]
R[a][b] = total
return R
# 矩阵快速幂
def m_fp(base,power):
def get(n,i):
v = [0]*n
v[i] = 1
return v
res = [get(len(base),i) for i in range(len(base))]
while power>0:
if power&1:
res = matrix_m(res,base)
power >>= 1
base = matrix_m(base,base)
return res
T = [[1,1],[1,0]]
f_m = matrix_m(m_fp(T,m-2),[[1],[1]])[0][0]
s_n = matrix_m(m_fp(T,n),[[1],[1]])[0][0]-1
#print(f_m,s_n)
print(s_n%f_m%p)
0.0分
2 人评分
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:591 |
printf基础练习2 (有点不明白)浏览:887 |
Hello, world! (C语言代码)浏览:916 |
矩形面积交 (C语言代码)浏览:1433 |
图形输出 (C语言代码)浏览:1019 |
A+B for Input-Output Practice (III) (C语言代码)浏览:455 |
简单的a+b (C语言代码)浏览:672 |
小O的数字 (C++代码)浏览:806 |
老王赛马 (C语言代码)浏览:1732 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:447 |