现在水平不够,挖个坑…以后再看
非常nice的博客
https://blog.csdn.net/u012061345/article/details/52224623
7.13 upd:
用矩阵快速幂的题目,一般有两个难点
- 寻找递推式
- 根据递推式建立系数矩阵
这俩肯定有一个难搞的
一个是思维,一个要经验…
其他也没啥了
放个板子 结构体用的挺舒服的
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define pb push_back
#define es erase
#define in insert
#define ll long long
#define pii pair<ll, ll>
#define lb long double
#define ioss \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define m_p(i, j) make_pair(i, j)
#define mem(a, x) memset(a, x, sizeof(a))
#define fcout(i)
#define endl "\n"
#define am ans.mat
const int N = 1e5 + 7;
const int INF = 0x3f3f3f3f;
struct Martix
{
static const int mod = 1000000009;
static const int N = 10;
ll mat[N][N];
int row, col; //一般为方阵
Martix(int row, int col) : row(row), col(col)
{
memset(mat, 0, sizeof(mat));
}
Martix(int n) : row(n), col(n)
{ //返回一个n*n的单位矩阵
memset(mat, 0, sizeof(mat));
for (int i = 1; i <= n; i++)
mat[i][i] = 1;
}
void output()
{
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
Martix operator*(Martix t)
{
Martix ans(row, t.col);
for (int i = 1; i <= row; i++)
for (int j = 1; j <= t.col; j++)
for (int k = 1; k <= col; k++)
ans.mat[i][j] = (ans.mat[i][j] + mat[i][k] * t.mat[k][j]) % mod;
return ans;
}
};
Martix mtx_pow(Martix n, ll m)
{
Martix tmp(n.row);
while (m > 0)
{
if (m & 1)
tmp = tmp * n;
m >>= 1;
n = n * n;
}
return tmp;
}
int main()
{
ll n;
while (cin >> n)
{
Martix ans(5);
mem(am, 0);
am[1][1] = 2;
am[1][2] = 0;
am[1][3] = 1;
am[1][4] = 2;
am[1][5] = 1;
am[2][1] = am[3][3] = am[3][5] = 1;
am[4][4] = am[4][5] = am[5][5] = 1;
am[3][4] = 2;
ans = mtx_pow(ans, n - 1);
//ans.output();
ll res = am[1][1] + am[1][3] + am[1][4] + am[1][5];
if (n)
cout << res % 1000000009 << endl;
else
cout << 0 << endl;
}
return 0;
}