关于矩阵乘法的一个小问题
查看原帖
关于矩阵乘法的一个小问题
541916
RiceFruit楼主2022/8/20 11:18

这是本人的代码:

#include<bits/stdc++.h>
using namespace std;
#define R register
#define ri register int
#define int long long
#define ull unsigned long long
#define lid (id<<1)
#define rid (id<<1|1)
void swap(int &x,int &y){int t=x;x=y;y=t;}
inline int max(int x,int y){return x>y?x:y;}
inline int min(int x,int y){return x<y?x:y;}
inline int read();
inline void write(int ans);
inline void put(int x,char c);
const int N=2e5,mod=1e9+7;
struct Matrix{
	int a[3][3];
	void file(){
		memset(a,0,sizeof a);
	}
	Matrix operator*(const Matrix &b) {
		Matrix res;
		res.file();
		for(int i=1;i<=2;i++){
			for(int j=1;j<=2;j++){
				for(int k=1;k<=2;k++)
					(res.a[i][j]+=a[i][k]*b.a[k][j])%=mod;
			}
		}
		return res;
	}
};
Matrix base,ans;
Matrix qow(int n){
	ans.file();
	base.file();
	ans.a[1][1]=1,ans.a[1][2]=1;
	base.a[1][1]=base.a[1][2]=base.a[2][1]=1;
	while(n){
		if(n&1)
			ans=ans*base;
		n>>=1,base=base*base;
	}
	return ans;
}
signed main(){
	int n=read();
	if(n<3)
		cout<<1;
	else{
		cout<<qow(n-2).a[1][1];
	}
	return 0;
}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if(x<0){putchar('-');x=-x;}if(x>9){write(x/10);}putchar(x % 10+'0');return;}
inline void put(int x,char c){write(x);putchar(c);return;}

在第 2121 行中的 operator*(const Matrix &b) 后面是否需要加 const?

也就是这样:operator*(const Matrix &b) const

加和不加都没有编译错误,有大佬能解释一下加 const 之后的作用吗?

2022/8/20 11:18
加载中...