#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll n, sum;
struct Node
{
ll a[2][2];
};
Node mut(Node x, Node y)
{
Node ans;
memset(ans.a, 0, sizeof(ans.a));
for (ll i = 0; i < 2; i++)
for (ll j = 0; j < 2; j++)
for (ll k = 0; k < 2; k++)
ans.a[i][j] = (ans.a[i][j] + x.a[i][k] * y.a[k][j]) % mod;
return ans;
}
void qp(ll n)
{
Node now, ans;
now.a[0][0] = 1;
now.a[0][1] = 1;
now.a[1][0] = 1;
now.a[1][1] = 0;
memset(ans.a, 0, sizeof(ans.a));
for (ll i = 0; i < 2; i++)
ans.a[i][i] = 1;
while (n)
{
if (n & 1)
ans = mut(ans, now);
now = mut(now, now);
n = n >> 1;
}
cout <<(ans.a[0][1] % mod)<< '\n';
}
int main()
{
while(cin>>n){
qp(n);}
return 0;
}
矩阵求斐波那契数列
最新推荐文章于 2024-11-15 17:20:35 发布
本文深入探讨了矩阵快速幂算法的实现,通过具体的代码示例,详细解释了如何使用矩阵乘法和快速幂技巧来高效地计算大指数的矩阵幂。此算法在处理递归序列和线性代数问题中具有广泛应用。
1万+

被折叠的 条评论
为什么被折叠?



