矩阵快速幂 为什么我的是矩阵龟速幂
查看原帖
矩阵快速幂 为什么我的是矩阵龟速幂
527629
iqer楼主2022/3/17 19:25

想问一下我的矩阵快速幂有什么问题吗???
想知道哪里导致我的快速幂变成了了龟速幂。。。。
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
ll n, pr, nf, p;
ll dp[105];
int tmp[105], q[105];

typedef vector<ll> vec;
typedef vector<vec> mat;
inline mat mul(mat &x, mat &y){
    mat ans(100, vec(100));
    // for(int i = 0; i < 100; ++i)for(int j = 0; j < 100; ++j)ans[i][j] = 0;
    for(int i = 0; i < 100; ++i){
        for(int j = 0; j < 100; ++j){
            for(int k = 0; k < 100; ++k){
                ans[i][j] = (ans[i][j] + x[i][k] * y[k][j] % mod) % mod;
            }
        }
    }
    return ans;
}
mat qpow(mat &x, ll y){
    mat ans(100, vec(100));
    for(int i = 0; i < 100; ++i)ans[i][i] = 1;
    while(y){
        if(y & 1)ans = mul(ans, x);
        x = mul(x, x); y >>= 1;
    }
    return ans;
}

int main(){
    cin >> n;
    cin >> pr;
    for(int i = 1; i <= pr; ++i)cin >> p, tmp[p] = 1;
    cin >> nf;
    for(int i = 1; i <= nf; ++i){
        cin >> p;
        if(tmp[p])q[p] = 1;
    }
    //for(int i = 0; i <= 100; ++i)if(q[i])cout << i << " "; cout << endl;
    dp[0] = 1;
    for(int i = 1; i <= 100; ++i){
        for(int j = 0; j <= 100; ++j){
            if(q[j] && j <= i)dp[i] = (dp[i] + dp[i - j]) % mod;
        }
    }
    //cout << dp[100] << endl;
    if(n <= 100)return cout << dp[n] << endl, 0;
    mat orz(100, vec(100));
    // for(int i = 0; i < 100; ++i)
    //     for(int j = 0; j < 100; ++j)
    //         orz[i][j] = 0;
    for(int i = 1; i <= 100; ++i)if(q[i])orz[0][i - 1] = 1;
    for(int i = 1; i < 100; ++i)orz[i][i - 1] = 1;

    // for(int i = 0; i < 100; ++i){
    //     for(int j = 0; j < 100; ++j){
    //         cout << orz[i][j] << " "; 
    //     }cout << endl;
    // }
    mat matrix = qpow(orz, n - 100);
    ll ans = 0;
    for(int i = 0; i < 100; ++i)ans = (ans + matrix[0][i] * dp[100 - i] % mod) % mod;
    cout << ans << endl; return 0;
}
2022/3/17 19:25
加载中...