20pts 求调 玄关
查看原帖
20pts 求调 玄关
970132
Hero_Broom楼主2024/7/18 10:41

我这代码是哪里有UB吗,为什么本地正确洛谷TLE

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
int n,k;
struct Matrix{
	int n,a[3][3];
};
Matrix A,ans;
Matrix operator * (Matrix x,Matrix y){
	Matrix z;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			z.a[i][j]=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			for(int k=1;k<=n;k++){
				z.a[i][j]+=x.a[i][k]*y.a[k][j]%mod;
				z.a[i][j]%=mod;
			}
	return z;
}
Matrix pow(int t){
	while(t){
		if(t&1) ans=ans*A;
		A=A*A;
		t>>=1;
	}
}
void init(){
	A.a[1][1]=A.a[1][2]=A.a[2][1]=1;
	ans.a[1][1]=ans.a[1][2]=1;
}
signed main(){
	scanf("%lld",&n);
	if(n<=2) printf("1\n"),exit(0);
	init();
	pow(n-2);
	printf("%lld\n",ans.a[1][1]%mod);
	return 0;
}
2024/7/18 10:41
加载中...