One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learning something about matrix, so he decided to make a crazy problem for her.

Bob has a six-faced dice which has numbers 0, 1, 2, 3, 4 and 5 on
each face. At first, he will choose a number N (4 <= N <= 1000),
and for N times, he keeps throwing his dice for K times (2 <=K <=
6) and writes down its number on the top face to make an N*K matrix A,
in which each element is not less than 0 and not greater than 5. Then he
does similar thing again with a bit difference: he keeps throwing his
dice for N times and each time repeat it for K times to write down a K*N
matrix B, in which each element is not less than 0 and not greater than
5. With the two matrix A and B formed, Alice’s task is to perform the
following 4-step calculation.

Step 1: Calculate a new N*N matrix C = A*B.

Step 2: Calculate M = C^(N*N).

Step 3: For each element x in M, calculate x % 6. All the remainders form a new matrix M’.

Step 4: Calculate the sum of all the elements in M’.

Bob just made this problem for kidding but he sees Alice taking it
serious, so he also wonders what the answer is. And then Bob turn to
you for help because he is not good at math.

InputThe input contains several test cases. Each test case starts
with two integer N and K, indicating the numbers N and K described
above. Then N lines follow, and each line has K integers between 0 and
5, representing matrix A. Then K lines follow, and each line has N
integers between 0 and 5, representing matrix B.

The end of input is indicated by N = K = 0.OutputFor each case, output the sum of all the elements in M’ in a line.Sample Input

4 2
5 5
4 4
5 4
0 0
4 2 5 5
1 3 1 5
6 3
1 2 3
0 3 0
2 3 4
4 3 2
2 5 5
0 5 0
3 4 5 1 1 0
5 3 2 3 3 2
3 1 5 4 5 2
0 0

Sample Output

14
56 题意 :按照题目所给的要求,求最终答案
思路 : 在一个结构体中去存一个二维矩阵时,最多可以开到 f[800][800],在往大就会直接停止运行了。
    解决此问题有一个关键的地方就是 A*B^(n*n), 这样求的话 A*B 就是1000*1000的矩阵,指定是 超时,如何展开 A*B*A*B*A*B……A*B,等于 A*(B*A)^(n*n-1),转变成为了一个 6 * 6 的矩阵。 代码 :
int a[1005][10];
int b[10][1005];
struct mat
{
int a[6][6];
};
int k;
int c[1005][10]; mat mul(mat a, mat b){
mat r;
memset(r.a, 0, sizeof(r.a)); for(int i = 0; i < k; i++){
for(int f = 0; f < k; f++){
if(a.a[i][f]){
for(int j = 0; j < k; j++){
if(b.a[f][j]){
r.a[i][j] += a.a[i][f]*b.a[f][j];
r.a[i][j] %= 6;
}
}
}
}
}
return r;
} mat pow(mat a, int n){
mat b;
memset(b.a, 0, sizeof(b.a));
for(int i = 0; i < k; i++) b.a[i][i] = 1; while(n){
if(1 & n) b = mul(b, a);
a = mul(a, a);
n >>= 1;
}
return b;
} int main() {
int n; while(~scanf("%d%d", &n, &k)&& n+k ){
for(int i = 0; i < n; i++){
for(int j = 0; j < k; j++){
scanf("%d", &a[i][j]);
}
}
for(int i = 0; i <k; i++){
for(int j = 0; j < n; j++){
scanf("%d", &b[i][j]);
}
}
mat A;
memset(A.a, 0, sizeof(A.a));
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
for(int f = 0; f < n; f++){
A.a[i][j] += b[i][f]*a[f][j];
A.a[i][j] %= 6;
}
}
}
A = pow(A, n*n-1);
memset(c, 0, sizeof(c));
for(int i = 0; i < n; i++){
for(int j = 0; j < k; j++){
for(int f = 0; f < k; f++){
c[i][j] += a[i][f]*A.a[f][j];
c[i][j] %= 6;
}
}
}
int sum = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int t = 0;
for(int f = 0; f < k; f++){
t += c[i][f]*b[f][j];
t %= 6;
}
sum += t;
}
}
printf("%d\n", sum);
} return 0;
}

最新文章

  1. EasyPR--开发详解(6)SVM开发详解
  2. Log4J的入门简介学习【转】
  3. Start_Learning_Python 03 条件、循环
  4. 菜鸟译文(三)——JDK6和JDK7中substring()方法的对比
  5. 专注网格剖分 - TetGen
  6. 新学Linux时遇到的一些问题
  7. Python开发过程中17个坑
  8. Android 使用网络ADB调试.
  9. C# 关闭窗体立即停止进程
  10. Yii的Relational Active Record三张表连接查询
  11. 解决asp.net MVC中 当前上下文中不存在名称“model” 的问题
  12. 学习Vue.js之vue移动端框架到底哪家强
  13. Java_设计模式之享元模式
  14. Windows服务器修改网站上传文件的大小限制
  15. 【Appium自学】Android studio安装与配置(转)
  16. C++中字符串换行(如何拆分为多行)
  17. css 动态线条制作方案
  18. (水题)P1424 小鱼的航程(改进版) 洛谷
  19. ASP.NET MVC+BUI实现表格的操作
  20. pip安装第三方库镜像源选择

热门文章

  1. 关于 vue 生命周期 钩子函数 事件
  2. servicemix-4.5.3 启动日志
  3. Springboot-webscoket with sockjs
  4. H3C 寻找邻居
  5. npm install 报错(npm ERR! errno -4048,Error: EPERM: operation not permitted)
  6. printk函数 用查询来调试
  7. win10 uwp 如何使用DataTemplate
  8. 【t081】序列长度
  9. git无密码push
  10. Python9_类