解题报告:poj 3070 - 矩阵快速幂简单应用

时间:2025-03-04 12:04:44

2017-09-13 19:22:01

writer:pprp

题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数

解题报告:poj 3070 - 矩阵快速幂简单应用.

这是原理,实现部分就是矩阵的快速幂,也就是二分来做

矩阵快速幂可以用来解决线性递推方程,难点在于矩阵的构造

代码如下:

/*
@theme:用矩阵快速幂解决线性递推公式-斐波那契数列
@writer:pprp
@begin:21:17
@end:19:10
@error:注意mod的位置,不能连用,要加括号来用
@date:2017/9/13
*/ #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std;
typedef long long ll;
const int mod=; struct Mat
{
ll a[][];
}; Mat mat_mul(Mat x, Mat y)
{
Mat res;
memset(res.a,,sizeof(res.a));
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; k++)
{
res.a[i][j] += x.a[i][k] * y.a[k][j];
res.a[i][j] %= mod;
}
return res;
} void quick_pow(ll n)
{
Mat E,res;
E.a[][] = E.a[][] = E.a[][] = ;
E.a[][] = ;
memset(res.a,,sizeof(res.a)); for(int i = ; i < ; i++)//二阶单位矩阵
res.a[i][i] = ; while(n)
{
if(n&)
res = mat_mul(res,E);
E = mat_mul(E,E);
n >>= ;
}
cout << res.a[][] << endl;
} int main()
{
ios::sync_with_stdio(false);
ll n;
while(cin >> n && n != -)
{
quick_pow(n);
}
return ;
}
/*
@theme:用矩阵快速幂解决线性递推公式-斐波那契数列
@writer:pprp
@begin:21:17
@end:19:10
@error:注意mod的位置,不能连用,要加括号来用
@date:2017/9/13
*/ #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std;
typedef long long ll;
const int mod=; struct Mat
{
ll a[][];
}; Mat mat_mul(Mat x, Mat y)
{
Mat res;
memset(res.a,,sizeof(res.a));
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; k++)
{
res.a[i][j] += x.a[i][k] * y.a[k][j];
res.a[i][j] %= mod;
}
return res;
} void quick_pow(ll n)
{
Mat E,res;
E.a[][] = E.a[][] = E.a[][] = ;
E.a[][] = ;
memset(res.a,,sizeof(res.a)); for(int i = ; i < ; i++)//二阶单位矩阵
res.a[i][i] = ; while(n)
{
if(n&)
res = mat_mul(res,E);
E = mat_mul(E,E);
n >>= ;
}
cout << res.a[][] << endl;
} int main()
{
ios::sync_with_stdio(false);
ll n;
while(cin >> n && n != -)
{
quick_pow(n);
}
return ;
}