求调 80pts 矩阵+快速幂 特判领域大神
查看原帖
求调 80pts 矩阵+快速幂 特判领域大神
465029
Specter_LiZN楼主2025/1/19 16:47

说来好笑,在加结尾那两行的特判以前80pts
加一行变90pts 再加一行都AC

请求各位dalao帮我看一下,为什么大数据啥事没有,小数据却错误吗?感激!
私货注意 封装完美的屎山注意

#include <bits/stdc++.h>
#define F(i,a,b) for (int i=a;i<=b;++i)
#define ll long long
#define int long long
#define Marisa cout<<"Matrix QPow da*ze"<<'\n'
const signed N=12345;
const signed Miku=INT_MAX;
using namespace std;

const signed mod=1e9+7;
const signed len=4;
int T;
int n;

struct Matrix{
  ll num[len][len];
  Matrix() { // 初始化
    memset(num, 0, sizeof(num));
  }
  Matrix(int x){
    memset(num, 0, sizeof(num));
    if(x==1){// 建造单位矩阵
      F(i,1,n)
        num[i][i]=1;
    }
    else if(x==2){// 建造求解矩阵
      num[1][3]=num[2][1]=num[3][2]=num[3][3]=1;
      /*
      0 0 1
      1 0 0
      0 1 1
      */
    }
    else if(x==3){// 建造初始矩阵
      num[1][1]=num[1][2]=num[1][3]=1;
      // 1 1 1
    }
    else
      Marisa;
  }
};

Matrix operator *(const Matrix &x,const Matrix &y){
  Matrix z;
  F(k,1,3)
    F(i,1,3)
      F(j,1,3)
        z.num[i][j]=(z.num[i][j]+x.num[i][k]*y.num[k][j]%mod)%mod;
  return z;
}
// void PrintMatrix(Matrix x){
//   F(i,1,4)
//     F(j,1,4)
//       cout<<x.num[i][j]<<" \n"[j==4];
// }
ll Answer(int mul){// ans (*maker)^mul
  Matrix res(1),maker(2),ans(3);
  while(mul){
    if(mul&1)
      res=res*maker;
    maker=maker*maker;
    mul>>=1;
  }
  ans=ans*res;
  return ans.num[1][3];
}


signed main(){
  ios::sync_with_stdio(0);cin.tie(0);
  cin>>T;
  while(T--){
    cin>>n;
    if(n<=3)
      cout<<1<<'\n';
    else if(n==4) cout<<"2\n"; <==看这行
    else if(n==5) cout<<"3\n"; <==和这行
    else{
      n-=3;
      cout<<Answer(n)%mod<<'\n';
    }
  }
  return 0;
  Marisa;
}
2025/1/19 16:47
加载中...