So Easy!(矩阵快速幂)

171 篇文章 0 订阅
79 篇文章 0 订阅


Link:http://acm.hdu.edu.cn/showproblem.php?pid=4565


So Easy!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3370    Accepted Submission(s): 1080


Problem Description
  A sequence S n is defined as:

Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S n.
  You, a top coder, say: So easy! 
 

Input
  There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 2 15, (a-1) 2< b < a 2, 0 < b, n < 2 31.The input will finish with the end of file.
 

Output
  For each the case, output an integer S n.
 

Sample Input
  
  
2 3 1 2013 2 3 2 2013 2 2 1 2013
 

Sample Output
  
  
4 14 4
 

Source
 



AC code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define LL long long
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
//----以下为矩阵快速幂模板-----//
int mod=1024;
const int NUM=11;//定义矩阵能表示的最大维数
int N;//N表示矩阵的维数,以下的矩阵加法、乘法、快速幂都是按N维矩阵运算的
struct Mat{//矩阵的类
	LL a[NUM][NUM];
	void init()//将其初始化为单位矩阵
	{
		memset(a,0,sizeof(a));
		for(int i=0;i<NUM;i++)
		{
			a[i][i]=1;
		}
	}
};
Mat add(Mat a,Mat b)//(a+b)%mod  矩阵加法
{
	Mat ans;
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)
		{
			ans.a[i][j]=a.a[i][j]+b.a[i][j];
			ans.a[i][j]%=mod;
		}
	}
	return ans;
}
Mat mul(Mat a,Mat b) //(a*b)%mod  矩阵乘法
{
	Mat ans;
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)
		{
			ans.a[i][j]=0;
			for(int k=0;k<N;k++)
			{
				ans.a[i][j]+=a.a[i][k]*b.a[k][j];
			}
			ans.a[i][j]%=mod;
		}
	}
	return ans;
}
Mat power(Mat a,int num)//(a^n)%mod  矩阵快速幂
{
	Mat ans;
	ans.init();
	while(num)
	{
		if(num&1)
		{
			ans=mul(ans,a);
		}
		num>>=1;
		a=mul(a,a);
	}
	return ans;
}
Mat pow_sum(Mat a,int num)//(a+a^2+a^3....+a^n)%mod 矩阵的幂和
{
	int m;
	Mat ans,pre;
	if(num==1)
		return a;
	m=num/2;
	pre=pow_sum(a,m);
	ans=add(pre,mul(pre,power(a,m)));
	if(num&1)
		ans=add(ans,power(a,num));
	return ans;
}
void output(Mat a)//输出矩阵
{
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)
		{
			printf("%d%c",a.a[i][j],j==N-1?'\n':' ');
		}
	}
}
//----以上为矩阵快速幂模板-----//
int main()
{
	int t,k,ans,i,j,A,B,n,m;
	int T;
	while(scanf("%d%d%d%d",&A,&B,&n,&m)!=EOF)
	{
	    if(n==1){
            cout << (2*A)%m << endl;
            continue;
        }
		Mat a,f;
		N=2;
	    a.a[0][0]=A;
	    a.a[0][1]=B;
	    a.a[1][0]=1;
	    a.a[1][1]=A;
	    /*k=n%m;//k为循环节
	    if(k==0)
            k=m;*/
        mod=m;
		f=power(a,n-1);
		int an=f.a[0][0]*A+f.a[0][1]*1;
		printf("%d\n",(2*an)%mod);
	 }
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值