矩阵加速板子求调
查看原帖
矩阵加速板子求调
817681
HZHDCM楼主2024/12/26 21:28
#include<bits/stdc++.h>
#define int long long
using namespace std;
int Mod;
int n;
struct Martix{
	int c[5][5];
	Martix(){memset(c,0,sizeof(c));}
}a;
Martix operator*(const Martix &x,const Martix &y){
	Martix z;
	for(int i=1;i<=2;i++)
		for(int j=1;j<=2;j++)
			z.c[i][j]=0;
	for(int i=1;i<=2;i++)
		for(int j=1;j<=2;j++)
			for(int k=1;k<=2;k++)
				(z.c[i][j]+=(x.c[i][k]*y.c[k][j])%Mod)%Mod;
	return z;
}
signed main() {
	int p,q,a1,a2;
	cin>>p>>q>>a1>>a2>>n>>Mod;
	if(n==1)return cout<<a1,0;
	if(n==2)return cout<<a2,0;
	int power=n-2;
	Martix ans;
	ans.c[1][1]=a2;ans.c[1][2]=a1;
	a.c[1][1]=p;a.c[1][2]=1;a.c[2][1]=q;
	a.c[2][2]=0;
	while(power){
		if(power&1)
			ans=ans*a;
		power>>=1;
		a=a*a;
	}
	cout<<ans.c[1][1]%Mod<<endl;
	return 0;
}
2024/12/26 21:28
加载中...