求助
  • 板块灌水区
  • 楼主WD2c0mP
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/9/23 13:29
  • 上次更新2023/11/2 18:32:57
查看原帖
求助
780641
WD2c0mP楼主2023/9/23 13:29
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int P = 1e9 + 7;
struct mat{
    int n,m;
    int a[5][5];
    mat() {
        memset(a,0,sizeof(a));
    }
}; 
mat operator *(mat A,mat B) {
    mat c;
    c.n = A.n,c.m = B.m;
    for (int i = 1;i <= c.n;i ++) {
        for (int j = 1;j <= c.m;j ++) {
            for (int k = 1;k <= A.m;k ++) {
                c.a[i][j] += A.a[i][k] * B.a[k][j] % P;
                c.a[i][j] %= P;
            }
        }
    }
    return c;
}
mat beg,One;
inline mat ksm(mat a,int b) {
    mat ret = beg;
    while (b) {
        if (b & 1) ret = ret * a;
        a = a * a;
        b >>= 1;
    }
    return ret;
}
signed main() {
    One.n = One.m = 3;
    One.a[2][1] = One.a[1][3] = One.a[3][2] = One.a[3][3] = 1;
    beg.n = 1,beg.m = 3;
    beg.a[1][1] = beg.a[1][2] = beg.a[1][3] = 1;
    int T;
    cin >> T;
    while (T --) {
        int r;
        cin >> r;
        mat t = ksm(One,r - 3);
        cout << t.a[1][3] % P << endl;
    }
    return 0;
}
/*3

a[i - 3] a[i - 2] a[i - 1]
0 0 1
1 0 0
0 1 1
a[i - 2] a[i - 1] a[i]

1 1 1
1 1 2
1 2 3
2 3 
*/

P1939矩阵快速幂求助
请问这样写TLE 2个点怎么优化?

2023/9/23 13:29
加载中...