1142 - Summing up Powers (II)
Time Limit: 2 second(s) Memory Limit: 32 MB

Shanto is learning how to power up numbers and he found an efficient way to find kth power of a matrix. He was quite happy with his discovery. Suddenly his sister Natasha came to him and asked him to find the summation of the powers. To be specific his sister gave the following problem.

Let A be an n x n matrix. We define Ak = A * A * ... * A (k times). Here, * denotes the usual matrix multiplication. You are to write a program that computes the matrix A + A2 + A3 + ... + Ak.

Shanto smiled and thought that it would be an easy one. But after a while he found that it's tough for him. Can you help him?

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with two integers n (1 ≤ n ≤ 30) and k (1 ≤ k ≤ 109). Each of the next n lines will contain n non-negative integers (not greater than 10).

Output

For each case, print the case number and the result matrix. For each cell, just print the last digit. See the samples for more details.

Sample Input

Output for Sample Input

2

3 2

1 4 6

6 5 2

1 2 3

3 10

1 4 6

6 5 2

1 2 3

Case 1:

208

484

722

Case 2:

868

620

546


PROBLEM SETTER: JANE ALAM JAN
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<math.h>
7 #include<queue>
8 using namespace std;
9 typedef long long LL;
10 typedef struct node
11 {
12 int m[70][70];
13 node()
14 {
15 memset(m,0,sizeof(m));
16 }
17 } maxtr;
18 int ans[70][70];
19 void E(node *nn,int n);
20 maxtr ju(int n);
21 maxtr quick(node ju,int n,int m);
22 int main(void)
23 {
24 int i,j,k;
25 int n,m;
26 int s;
27 cin>>k;
28 for(s=1; s<=k; s++)
29 {
30 scanf("%d %d",&n,&m);
31 for(i=0; i<n; i++)
32 {
33 for(j=0; j<n; j++)
34 {
35 scanf("%d",&ans[i][j]);
36 ans[i][j]%=10;
37 }
38 }node aa;aa=ju(n);
39 aa=quick(aa,n,m);printf("Case %d:\n",s);
40 for(i=0;i<n;i++)
41 {
42 for(j=n;j<2*n;j++)
43 {
44 printf("%d",aa.m[i][j]);
45 }printf("\n");
46 }
47 }return 0;
48 }
49 void E(node *nn,int n)
50 {
51 int i,j,k;
52 for(i=0; i<n; i++)
53 {
54 for(j=0; j<n; j++)
55 {
56 if(i==j)
57 nn->m[i][j]=1;
58 else nn->m[i][j]=0;
59 }
60 }
61 }
62 maxtr ju(int n)
63 {
64 int i,j,k;
65 maxtr nn;
66 for(i=0; i<n; i++)
67 {
68 for(j=0; j<n; j++)
69 {
70 nn.m[i][j]=ans[i][j];
71 }
72 }
73 for(i=0; i<n; i++)
74 {
75 for(j=n; j<2*n; j++)
76 {
77 nn.m[i][j]=ans[i][j-n];
78 }
79 }
80 node cc;
81 E(&cc,n);
82 for(i=n; i<2*n; i++)
83 {
84 for(j=n; j<2*n; j++)
85 {
86 nn.m[i][j]=cc.m[i-n][j-n];
87 }
88 }return nn;
89 }
90 maxtr quick(node ju,int n,int m)
91 { node ee;
92
93 E(&ee,2*n);
94 int i,j,k;
95 int s;
96 while(m)
97 {
98 if(m&1)
99 {
100 node cc;
101 for(i=0; i<2*n; i++)
102 {
103 for(j=0; j<2*n; j++)
104 {
105 for(s=0; s<2*n; s++)
106 {
107 cc.m[i][j]=(ju.m[i][s]*ee.m[s][j]+cc.m[i][j])%10;
108 }
109 }
110 }
111 ee=cc;
112 }
113 node cc;
114 for(i=0; i<2*n; i++)
115 {
116 for(j=0; j<2*n; j++)
117 {
118 for(s=0; s<2*n; s++)
119 {
120 cc.m[i][j]=(ju.m[i][s]*ju.m[s][j]+cc.m[i][j])%10;
121 }
122 }
123 }
124 ju=cc;
125 m/=2;
126 }
127 return ee;
128 }

1142 - Summing up Powers (II)的更多相关文章

  1. lightOJ 1132 Summing up Powers(矩阵 二分)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意:给出n和m.求sum(i^m)%2^32.(1<=i<=n) ...

  2. lightoj1132—Summing up Powers (取膜技巧&&组合数应用)

    题目链接:https://vjudge.net/problem/LightOJ-1132 题目意思:(1K + 2K + 3K + ... + NK) % 232 矩阵快速幂的题目一般都很短,这道题也 ...

  3. LightOJ 1132 Summing up Powers:矩阵快速幂 + 二项式定理

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意: 给定n.k,求(1K + 2K + 3K + ... + NK) % 2 ...

  4. LightOJ - 1132 Summing up Powers 矩阵高速幂

    题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...

  5. [伯努利数] poj 1707 Sum of powers

    题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Lim ...

  6. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  7. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  8. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

  9. 统计分析中Type I Error与Type II Error的区别

    统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...

随机推荐

  1. 使用 Skywalking 对 Kubernetes(K8s)中的微服务进行监控

    1. 概述 老话说的好:任何成功都不是轻易得来的,是不断地坚持与面对的结果. 言归正传,之前我们聊了 SpringCloud 开发的微服务是如何部署在  Kubernetes(K8s)集群中的,今天我 ...

  2. JavaSE中级篇1 — 核心思想:面向对象 — 更新完毕

    1.面向对象编程思想(重点中的重点) 题外话: 其他都还可以是技术,但这里是走自己的路--面向对象编程,即:OOP,养成的思想就是:万物皆对象,懂得把东西抽离出来 这一部分记的理论知识很多,而且需要自 ...

  3. absorb

    absorb 物理的absorb比较直观.被书本/知识absorb也好理解.涉及到money/time时有点抽象,但汉语也有"吸金"的说法,consume, use up.可以吸收 ...

  4. 【swift】Xcode未响应(卡死、卡住、CPU满载、忙碌、转圈圈)

    在尝试了网上的方法,依然没能解决问题,尝试如下: 1.去自己项目的路径,找到<你的项目名.xcodeproj>,点击[显示包内容],删除xcuserdata文件夹 2.去Library,把 ...

  5. VIM多标签页

    :tabnew 增加一个标签 :tabc       关闭当前的tab :tabo       关闭所有其他的tab :tabp 或gT 前一个 :tabn 或gt  后一个 :tabs     显示 ...

  6. APK 反编译以及遇到的问题

    APK反编译: https://www.cnblogs.com/geeksongs/p/10864200.html 遇到的问题 https://www.jianshu.com/p/55bf5f688e ...

  7. Linux学习 - 文件特殊权限

    一.SUID权限(只针对文件) 只有可执行的二进制程序才能设定SUID权限 命令执行者要对该程序拥有x(执行)权限 1 拥有SUID的文件 /usr/bin/passwd 2 功能: 命令执行者(其他 ...

  8. linux 加密安全之AWK

    密钥 密钥一般是一串字符串或数字,在加密或者解密时传递给加密或者解密算法,以使算法能够正确对明文加密或密文解密. 加密算法 已知的加密算法有对称和非对称加密,也就是说你想进行加解密操作的时候需要具备密 ...

  9. 通过spring-data-redis操作Redis

    一.操作String类型数据 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/ ...

  10. 12.Vue.js 表单

    这节我们为大家介绍 Vue.js 表单上的应用. 你可以用 v-model 指令在表单控件元素上创建双向数据绑定. <div id="app"> <p>in ...