HDU 4565 So Easy(矩阵解公式) So Easy

【题目链接】So Easy

【题目类型】矩阵解公式

&题解:

感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的.
HDU 4565 So Easy(矩阵解公式)
 So Easy
HDU 4565 So Easy(矩阵解公式)
 So Easy
所以最后2Xn就是答案

【时间复杂度】(O(logn))

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int si=2;
int M,a,b,n;
struct mat
{
	int m[si][si];
}A;
mat Mul(mat a,mat b)
{
	mat c;
	for(int i=0;i<si;i++)
	for(int j=0;j<si;j++){
		c.m[i][j]=0;
		for(int k=0;k<si;k++){
			c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
		}
	}
	return c;
}
mat bPow(mat a,ll z)
{
	mat b;
	for(int i=0;i<si;i++)for(int j=0;j<si;j++)
		b.m[i][j]=(i==j);
	while(z){
		if(z&1)
			b=Mul(b,a);
		a=Mul(a,a);
		z>>=1;
	}
	return b;
}
int tb[si];
void Init()
{
	tb[0]=a,tb[1]=1;
	A.m[0][0]=a,A.m[0][1]=b;
	A.m[1][0]=1,A.m[1][1]=a;
}
void DF(mat A)
{
	for(int i=0;i<si;i++){
		for(int j=0;j<si;j++)
			cout<<A.m[i][j]<<" ";
		cout<<endl;
	}
}
int main()
{
	freopen("E:1.txt","r",stdin);
	while(cin>>a>>b>>n>>M){
		//输入必须要取模,在这wa了1次
		a%=M,b%=M;
		Init();
		A=bPow(A,n-1);
//		DF(A);
		ll ans=0;
		for(int i=0;i<si;i++)
			ans=(ans+A.m[0][i]*tb[i])%M;
		cout<<2*ans%M<<endl;
	}
	return 0;
}