HDU 4549 M斐波那契数列(矩阵快速幂)

题目链接:M斐波那契数列

枚举几项会发现$ F[n]=a^{fib(n-1)} * b^{fib(n)} $

斐波那契数列用矩阵快速幂求即可。但是因为n很大,fib会爆掉。这时候可以引入费马小定理。

证明:$a^x \% p = a^{x \%(p-1)} \%p$

1.$a^x \% p = a^{x \% (p-1) + x/(p-1)*(p-1)} \% p$

2.$a^x \% p = a^{x \% (p-1)} * a^{x/(p-1)*(p-1)} \%p$

3.$a^{x/(p-1)*(p-1)} \% p= ({a^{p-1}}) ^ {(x/(p-1))} \%p$

猜你喜欢

转载自www.cnblogs.com/ehanla/p/9811333.html