可爱萌新,刚学OI,求救
查看原帖
可爱萌新,刚学OI,求救
28910
览遍千秋七海楼主2019/8/18 15:16

矩阵加速

WA成20分

#include<bits/stdc++.h>
using namespace std;

#define int long long
int T,a,b;
int n,p,q,mod;
struct Mat{
	int a[5][5];
	int n;
	Mat(){
		memset(a,0,sizeof(a));n=2;
	}
}base;

template<typename Tp>
void read(Tp &x){
	x=0;char ch=1;int fh;
	while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
	if(ch=='-'){
		ch=getchar();fh=-1;
	}
	else fh=1;
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	x*=fh;
}

Mat Mul(Mat a,Mat b){
	Mat ret;
	int n=a.n;ret.n=a.n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			for(int k=1;k<=n;k++){
				ret.a[i][j]+=(a.a[i][k]*b.a[k][j]%mod);
				ret.a[i][j]%=mod;
			}
		}
	}
	return ret;
}

Mat ksm(Mat x,int p){
	Mat ret;
	ret.a[1][1]=ret.a[1][2]=1;
	while(p){Z
		if(p&1) ret=Mul(ret,x);p>>=1;
		x=Mul(x,x);
	}
	return ret;
}

signed main(){
	read(p);read(q);read(a);read(b);read(n);read(mod);
	base.a[1][1]=p,base.a[2][1]=q,base.a[1][2]=1;
	if(n<=2){
		puts("1");return 0;
	}
	Mat ans;
	ans.a[1][1]=b,ans.a[1][2]=a;
	ans=Mul(ans,ksm(base,n-2));
	printf("%lld\n",ans.a[1][1]);
	return 0;
}
2019/8/18 15:16
加载中...