# 题目传送门

sol：

c[n][0] = f[0] * f[n] = f[0] * f[n - 1] + f[0] * f[n - 2] = c[n - 1][0] + c[n - 2][0];

c[n][1] = f[1] * f[n - 1] = f[1] * f[n - 2] + f[1] * f[n - 3] = c[n - 1][1] + c[n - 2][1];

c[n][n] = f[n] * f[0] = f[n] * f[-1] + f[n] * f[-2],这里就不能用c[n - 1][n] 和 c[n - 2][n]来表示了，但是f[0] = 0，所以c[n][n] = 0; 可以忽略；

c[n - 1][n - 1] 也等于0;

• 矩阵快速幂
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
const int MOD = 998244353;
struct Mat {
int mat[5][5];
Mat() {memset(mat, 0, sizeof(mat));}
friend Mat operator * (Mat a, Mat b) {
Mat c;
for (int k = 1; k <= 4; k++)
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++)
c.mat[i][j] = (c.mat[i][j] + 1LL * a.mat[i][k] * b.mat[k][j]) % MOD;
return c;
}
};
Mat mat_pow(Mat n, LL k) {
Mat ans;
for (int i = 1; i <= 4; i++)
ans.mat[i][i] = 1;
while (k) {
if (k & 1) ans = ans * n;
n = n * n;
k >>= 1;
}
return ans;
}
int main() {
LL n;
scanf("%lld", &n);
Mat m;
m.mat[1][1] = m.mat[1][2] = m.mat[1][3] = 1;
m.mat[2][1] = m.mat[4][3] = 1;
m.mat[3][3] = m.mat[3][4] = 1;
m = mat_pow(m, n - 1);
printf("%d\n", m.mat[1][3]);
return 0;
}

posted @ 2019-09-24 18:14  Angel_Demon  阅读(188)  评论(0编辑  收藏