样例没过求调qwq
查看原帖
样例没过求调qwq
246331
mystic_qwq楼主2024/4/26 15:19

是一道矩阵乘法的题目,初始矩阵是 x0c\begin{matrix} x_0\\c \end{matrix},转移矩阵为a 10 1\begin{matrix} a~1\\0~ 1\end{matrix}。要求过 nn 次转移后的矩阵。但是我的程序(基本和第一篇题解一样)对于样例输出是 1010 。求大佬帮调一下qwq

#include<bits/stdc++.h>
using namespace std;
using ll=__int128;
auto GetChar=[](){
  static char buf[1<<20],*l=buf,*r=buf;
  if(l==r) r=(l=buf)+fread(buf,1,1<<20,stdin);
  return l==r?-1:*l++;
};
template<class T>
void Read(T&x){
  //un-negative
  #define G GetChar()
  char c; while((c=G)<48);
  for(;c>47;c=G) x=x*10+(c^48);
  #undef G
};
template<class T,class...Args>
void Read(T&x,Args&...y){
  Read(x);Read(y...);
}
void write(ll x){
  if(x>9) write(x/10);
  putchar(x%10^48);
}
ll M,a,c,x0,n,g;
struct Mat{
  ll a[4][4],n,m;
  Mat(){}
  Mat(int x,int y):n(x),m(y){
    //memset(a,0,sizeof a);
  }
  Mat(int x):n(x),m(x){
    for(int i=0;i<n;++i)
      for(int j=0;j<m;++j)
        a[i][j]=1;
  }
  ll*operator[](int x){
    return a[x];
  }
  Mat&operator*=(Mat x){
    Mat res(n,x.m);
    for(int i=0;i<n;++i)
      for(int j=0;j<x.m;++j)
        for(int k=0;k<m;++k)
          (res[i][j]+=a[i][k]*x[k][j])%=M;
    return *this=res;
  }
  Mat&operator%=(int unused){
    for(int i=0;i<n;++i)
      for(int j=0;j<n;++j)
        if(a[i][j]>M) a[i][j]%=M;
    return*this;
  }
  Mat&operator^=(ll x){
    Mat res(n),t=*this;
    for(;x;x>>=1,(t*=t)%=1)
      if(x&1) (res*=t)%=1;
    return *this=res;
  }
};
main(){
  Read(M,a,c,x0,n,g);
  if(!n){write(x0);exit(0);}
  Mat res(4,4),p(2,1);
  res[0][0]=a,res[0][1]=1,res[1][1]=1;
  p[0][0]=x0,p[1][0]=c;
  res^=n,res*=p,write(res[0][0]);
}

2024/4/26 15:19
加载中...