题目链接:https://www.luogu.com.cn/problem/P2233
解题思路:
矩阵快速幂。
思路完全来自 ghj1222大佬的博客
示例代码:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000;
struct Matrix {
int a[8][8];
Matrix operator * (Matrix b) const {
Matrix c;
memset(c.a, 0, sizeof(c.a));
for (int i = 0; i < 8; i ++)
for (int j = 0; j < 8; j ++)
for (int k = 0; k < 8; k ++)
c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % MOD;
return c;
}
} a, c;
int r[8][8] = {
0, 1, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, // E没有出边
0, 0, 0, 0, 1, 0, 1, 0,
0, 0, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 0, 0, 1, 0,
};
int n;
int main() {
cin >> n;
memset(c.a, 0, sizeof(c.a));
for (int i = 0; i < 8; i ++) c.a[i][i] = 1;
memcpy(a.a, r, sizeof(r));
while (n > 0) {
if (n % 2) c = c * a;
a = a * a;
n /= 2;
}
cout << c.a[0][4] << endl;
return 0;
}