标签:矩阵快速幂 邀请赛
链接 :click here~~
题意:
A sequence Sn is defined as:
【解题思路】
给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下来就是矩阵的快速幂了。
神图:click here~~
代码:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> using namespace std; #define LL long long LL MOD; struct Matrix { long long mapp[2][2]; }; Matrix unin= {1,0,0,1};//单位矩阵 Matrix multi(Matrix a,Matrix b) { Matrix c; for(int i=0; i<2; i++) for(int j=0; j<2; j++) { c.mapp[i][j]=0; for(int k=0; k<2; k++) c.mapp[i][j]+=(a.mapp[i][k]*b.mapp[k][j])%MOD; c.mapp[i][j]%=MOD; } return c; } Matrix power(Matrix a,long long n)//矩阵快速幂 { Matrix ans=unin;//单位矩阵 Matrix res=a; while(n) { if(n&1) ans=multi(ans,res); res=multi(res,res); n>>=1; } return ans; } int main() { LL A,B,N,M,ret; Matrix a,b; while(cin>>A>>B>>N>>MOD) { a.mapp[0][0] = 2*A%MOD; //递推式 a.mapp[0][1] = ((B%MOD-A*A%MOD)%MOD+MOD)%MOD; a.mapp[1][0] = 1; a.mapp[1][1] = 0; if(N==1) { printf("%lld\n",2*A%MOD); continue; } b=power(a,N-1); ret=(b.mapp[0][0]%MOD*2*A%MOD+b.mapp[0][1]%MOD*2%MOD)%MOD; ret%=MOD; printf("%lld\n",ret); } return 0; }
【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
标签:矩阵快速幂 邀请赛
原文地址:http://blog.csdn.net/u013050857/article/details/44936565