2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)

时间:2023-03-08 15:44:12

So Easy!

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

Problem Description
  A sequence Sn is defined as:
2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
  You, a top coder, say: So easy! 
2013长沙邀请赛A 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 < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
Output
  For each the case, output an integer Sn.
Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
Sample Output
4
14
4
挑战程序设计竞赛P268,关键是求初始矩阵:
an+1 = a*an+b*bn;
bn+1 = an+a*bn;
a[0][0],a[0][1]分别是公式1里面的系数,a和b;
a[1][0],a[1][1]分别是公式2里面的系数,1和a;
然后就是矩阵快速幂了:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#define M(a,b) memset(a,b,sizeof(a))
typedef long long LL; using namespace std; long long a,b,n,m; struct matrix
{
LL mat[][];
void init()
{
mat[][] = a;
mat[][] = b;
mat[][] = ;
mat[][] = a;
}
}; matrix mamul(matrix aa,matrix bb)
{
matrix c;
for(int i = ;i<;i++)
{
for(int j = ;j<;j++)
{
c.mat[i][j] = ;
for(int k = ;k<;k++)
c.mat[i][j]+=(aa.mat[i][k]*bb.mat[k][j]);
c.mat[i][j]%=m;
}
}
return c;
} matrix mul(matrix s, int k)
{
matrix ans;
ans.init();
while(k>=)
{
if(k&)
ans = mamul(ans,s);
k = k>>;
s = mamul(s,s);
}
return ans;
} int main()
{
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&m)==)
{
matrix ans;
ans.init();
ans = mul(ans,n-);
printf("%I64d\n",(ans.mat[][]*+m)%m);
}
return ;
}