• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ACM s1124yy
守りたいものが 強くさせること
博客园    首页    新随笔    联系   管理     

HDU 4565 So Easy(矩阵解公式)

So Easy

【题目链接】So Easy

【题目类型】矩阵解公式

&题解:

感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的.


所以最后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;
}
posted @ 2017-04-07 00:16  s1124yy  阅读(354)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3