解题思路:
注意事项:矩阵的乘法
参考代码:
#include <stdio.h>
int b, m;
int a[2][2], ans[2][2], temp[2][2] = {1,1,1,1};
void play()
{
int cnt, cnt2;
for(cnt = 0; cnt < 2; ++cnt)
{
for(cnt2 = 0; cnt2 < 2; ++cnt2)
{
printf("%d ", ans[cnt][cnt2]);
}
printf("\n");
}
}
void cp(int arr1[][2], int arr2[][2])
{
int cnt, cnt2;
for(cnt = 0; cnt < 2 ;++cnt)
for(cnt2 = 0; cnt2 < 2; ++cnt2)
arr1[cnt][cnt2] = arr2[cnt][cnt2];
}
void mod(int arr[][2])
{
int cnt, cnt2;
for(cnt = 0; cnt < 2; ++cnt)
for(cnt2 = 0; cnt2 < 2; ++cnt2)
arr[cnt][cnt2] %= m;
}
void fun2(int a[][2], int b[][2])
{
int cnt, cnt2;
temp[0][0] = a[0][0]*b[0][0]+a[0][1]*b[1][0];
temp[0][1] = a[0][0]*b[0][1]+a[0][1]*b[1][1];
temp[1][0] = a[1][0]*b[0][0]+a[1][1]*b[1][0];
temp[1][1] = a[1][0]*b[0][1]+a[1][1]*b[1][1];
}
void fun(int arr[][2], int k)
{
int cnt;
if(k == 0)
{
mod(temp);
cp(ans, temp);
return;
}
if(k == 1)
{
mod(ans);
return;
}
if(k == 2)
{
fun2(a, a);
cp(ans, temp);
// printf("2\n");
// play();
mod(ans);
return;
}
if(k%2 == 0)
{
fun(arr, k/2);
fun2(ans, ans);
cp(ans, temp);
//printf("=0\n");
//play();
mod(ans);
return;
}
if(k%2 != 0)
{
fun(arr, k-1);
fun2(ans, arr);
cp(ans, temp);
//printf("!=0\n");
// play();
mod(ans);
return;
}
}
int main()
{
int cnt, cnt2;
scanf("%d%d", &b, &m);
for(cnt = 0; cnt < 2; ++cnt)
for(cnt2 = 0; cnt2 < 2; ++cnt2)
{
scanf("%d", &a[cnt][cnt2]);
ans[cnt][cnt2] = a[cnt][cnt2];
}
fun(a, b);
play();
return 0;
}
0.0分
0 人评分
大神老白 (C语言代码)浏览:713 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:525 |
【排队买票】 (C语言代码)浏览:899 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:543 |
大家好,我是验题君浏览:576 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:343 |
整除问题 (C语言代码)浏览:523 |
C语言程序设计教程(第三版)课后习题12.3 (C语言代码)浏览:542 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:672 |
上车人数 (C语言代码)浏览:710 |