多测清空问题求助
查看原帖
多测清空问题求助
275989
LingHusama楼主2023/10/20 15:32

rt,不知道哪里没清空,就算ans和base开局部变量还是G

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct Mat{
	int c[105][105];
	int lenth;
	int wide;
}; 
const int mod=1e9+7;
Mat operator *(const Mat &a,const Mat &b){
	Mat ret;
	int n=a.wide;
	for(int i=1;i<=a.lenth;i++){
		for(int j=1;j<=b.wide;j++){
			int nowans=0;
			for(int k=1;k<=n;k++){
				nowans+=(a.c[i][k]*b.c[k][j]%mod);
				nowans%=mod;
			}
			ret.c[i][j]=nowans;
		}
	}
	ret.lenth=a.lenth;
	ret.wide=b.wide;
	return ret;
}
Mat qpow(Mat a,int b){
	Mat ret;
	ret.lenth=ret.wide=101;
	for(int i=1;i<=101;i++){
		ret.c[i][i]=1;
	}
	while(b){
		if(b%2==1){
			ret=ret*a;
		}
		b/=2;
		a=a*a;
	}
	return ret;
}
Mat ans;
Mat base;
signed main(){
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while(t--){
		ans.lenth=1;
		ans.wide=3;
		ans.c[1][1]=ans.c[1][2]=ans.c[1][3]=1;
		base.lenth=base.wide=3;
		base.c[1][1]=1;
		base.c[1][2]=1;
		base.c[1][3]=0;
		base.c[2][1]=0;
		base.c[2][2]=0;
		base.c[2][3]=1;
		base.c[3][1]=1;
		base.c[3][2]=0;
		base.c[3][3]=0;
		int n;
		cin >> n;
		ans=ans*qpow(base,n-3);
		cout<<ans.c[1][1]<<endl;		
	}

}
2023/10/20 15:32
加载中...