TZOJ 4954 矩阵游戏(矩阵乘法+费马小定理)

 

 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
typedef long long ll;
ll MOD=1000000007;
string N,M;
ll n,m,a,b,c,d;
ll A[2][2],B[2][2],C[2][2],D[2][2],u[2][2];
void J(ll x[2][2],ll y[2][2])
{
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++){
			ll uu=0;
			for(int k=0;k<2;k++)
				uu=(uu+x[i][k]*y[k][j]%MOD)%MOD;
			u[i][j]=uu%MOD;
		}
			
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++)
			x[i][j]=u[i][j];
}
ll Mod(string s,ll x)
{
	ll ans=0;
	for(int i=0;i<s.size();i++)
		ans=(ans*10+s[i]-'0')%x;
	return ans;	
}
void solve()
{
	cin>>N>>M;
	cin>>a>>b>>c>>d;
	//cout<<N<<" "<<M<<" "<<a<<b<<c<<d<<endl;
	if(c==1) n=Mod(N,MOD); else n=Mod(N,MOD-1);
	if(a==1) m=Mod(M,MOD); else m=Mod(M,MOD-1);
	//cout<<n<<m;
	n--;m-=2;
	A[0][0]=1,A[0][1]=1,A[1][0]=0,A[1][1]=0;
	B[0][0]=a,B[0][1]=0,B[1][0]=b,B[1][1]=1;
	C[0][0]=c,C[0][1]=0,C[1][0]=d,C[1][1]=1;
	D[0][0]=a,D[0][1]=0,D[1][0]=b,D[1][1]=1;
	while(m)
	{
		if(m&1)
			J(B,D);
		J(D,D);
		m>>=1;
	}
	D[0][0]=B[0][0],D[0][1]=B[0][1],D[1][0]=B[1][0],D[1][1]=B[1][1];
	J(B,C);
	while(n)
	{
		if(n&1)
			J(A,B);
		J(B,B);
		n>>=1;
	}
	J(A,D);
	cout<<A[0][0]<<endl;
}
int main()
{
 	IOS;
 	solve();
    return 0;
}

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值