矩阵快速幂奇奇怪怪的模板增加了!

在找矩阵快速幂的模板的时候,始终没有找到类似f(n)=a1f(n-1)+a2f(n-2)+…ax*f(n-x)的模板,于是总结一下这种奇奇怪怪的形式

//用于解决f(n)=a*f(n-1)+b*f(n-2)+c*f(n-3).....xjb*f(xiajiba)//共N项
#include<bits/stdc++.h>
using namespace std;
 typedef long long ll;
 int mod=1e8+7;
 const int maxn=1e5+10;
 ll pre[maxn];
 const int N=2;
 struct mat{
    ll m[N+1][N+1];
    mat(){memset(m, 0, sizeof(m));}
    mat(int a[]){memset(m, 0, sizeof(m));
    for(int i=1;i<=N;i++)m[1][i]=a[i];
    }
    mat operator*(const mat other) const{
     mat ans;
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
            for(int k = 1; k <= N; k++){
                ans.m[i][j] = (ans.m[i][j] + this->m[i][k] * other.m[k][j]) % mod;
            }
        }
    }
    return ans;
    }
    mat matqpow(ll p){
    mat ans;
    int i, j;
    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++){
            if(i == j) ans.m[i][j] = 1;
            else ans.m[i][j] = 0;
        }
    }
    while(p){
        if(p & 1) ans = (ans**this);
        *this = *this**this;
        p >>= 1;
    }
    return ans;
}
};
int main(){
  ll num;
  //对于f(n)=4*f(n-1)+255*f(n-2)+f(n-3)  f(1)=2,f(2)=4,f(3)=114514
  int p,q,a1,a2;
  cin>>num;
  int re[]={0,4,255,1},xishu[]={0,2,4,114514};
  mat res(re);
  for(int i=2;i<=N;i++)res.m[i][i-1]=1;
  mat ans;
  for(int i=1;i<=N;i++)ans.m[i][1]=xishu[N+1-i];
  if(num <=N){
      cout<<xishu[num]%mod;
  }
  else {
    num -= N;
    res = res.matqpow(num);
    res = res*ans;
    cout << res.m[1][1] % mod ;
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值