luogu 模板: https://www.luogu.com.cn/problem/P3390
矩阵快速幂
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 110, mod = 1e9 + 7;
LL n, k, a[N][N], b[N][N], t[N][N];
void matrixQp(LL y){
while (y){
if (y & 1){
memset(t, 0, sizeof t);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
for (int k = 1; k <= n; k ++ )
t[i][j] = ( t[i][j] + (a[i][k] * b[k][j]) % mod ) % mod;
memcpy(b, t, sizeof t);
}
y >>= 1;
memset(t, 0, sizeof t);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
for (int k = 1; k <= n; k ++ )
t[i][j] = ( t[i][j] + (a[i][k] * a[k][j]) % mod ) % mod;
memcpy(a, t, sizeof t);
}
}
int main(){
cin >> n >> k;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ ){
cin >> b[i][j];
a[i][j] = b[i][j];
}
matrixQp(k - 1);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
cout << b[i][j] << " \n"[j == n];
return 0;
}
https://www.luogu.com.cn/problem/P1939
矩阵加速
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int mod = 1e9 + 7;
LL T, n, t[5][5], a[5][5], b[5][5];
void matrixQp(LL y){
while (y){
if (y & 1){
memset(t, 0, sizeof t);
for (int i = 1; i <= 3; i ++ )
for (int j = 1; j <= 1; j ++ )
for (int k = 1; k <= 3; k ++ )
t[i][j] = ( t[i][j] + (a[i][k] * b[k][j]) % mod ) % mod;
memcpy(b, t, sizeof t);
}
y >>= 1;
memset(t, 0, sizeof t);
for (int i = 1; i <= 3; i ++ )
for (int j = 1; j <= 3; j ++ )
for (int k = 1; k <= 3; k ++ )
t[i][j] = ( t[i][j] + (a[i][k] * a[k][j]) % mod ) % mod;
memcpy(a, t, sizeof t);
}
}
void init(){
b[1][1] = b[2][1] = b[3][1] = 1;
memset(a, 0, sizeof a);
a[1][1] = a[2][1] = a[1][3] = a[3][2] = 1;
}
void solve(){
cin >> n;
if (n <= 3) cout << "1\n";
else{
init();
matrixQp(n - 3);
cout << b[1][1] << "\n";
}
}
int main(){
cin >> T;
while ( T -- )
solve();
return 0;
}