题目地址:http://www.51cpc.com/web/problem.php?id=4267
Knowledge Point:https://www.cnblogs.com/liubilan/p/9450568.html
Summarize:
1. 矩阵乘法 a*b 与 b*a 不同,注意顺序;
2. 初始其中一个是乘数应该为对角矩阵,且对角线上元素全部为1,作用类似于整数1;
3. 关于函数返回值,由于试了很久我也没能直接返回二维数组,所以在这里用结构体封装二维数组,函数返回结构体;
附代码:
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 #define LL long long 6 #define MOD 20181225 7 LL x0, y0, a, b, c, d, n; 8 struct Matrix { 9 LL w[2][2]; 10 }matrix; 11 12 Matrix calculate(Matrix x, Matrix y) 13 { 14 Matrix tmp; 15 memset(tmp.w, 0, sizeof(tmp.w)); 16 for(int i=0; i<2; i++) 17 for(int j=0; j<2; j++) { 18 for(int k=0; k<2; k++) 19 tmp.w[i][j] += x.w[i][k]*y.w[k][j]; 20 tmp.w[i][j]%=MOD; 21 } 22 return tmp; 23 } 24 25 void Quick_Pow() 26 { 27 Matrix tmp; 28 tmp.w[0][0]=tmp.w[1][1]=1; 29 tmp.w[0][1]=tmp.w[1][0]=0; 30 31 while(n) { 32 if(n&1) { 33 tmp = calculate(matrix, tmp); 34 } 35 matrix = calculate(matrix, matrix); 36 n>>=1; 37 } 38 39 LL xn = (tmp.w[0][0]*x0%MOD + tmp.w[0][1]*y0%MOD)%MOD; 40 LL yn = (tmp.w[1][0]*x0%MOD + tmp.w[1][1]*y0%MOD)%MOD; 41 cout<<xn<<' '<<yn<<endl; 42 } 43 44 int main() 45 { 46 ios::sync_with_stdio(false); 47 while(cin>>x0>>y0>>a>>b>>c>>d>>n) { 48 matrix.w[0][0]=a, matrix.w[0][1]=b; 49 matrix.w[1][0]=c, matrix.w[1][1]=d; 50 Quick_Pow(); 51 } 52 return 0; 53 }