1142 - Summing up Powers (II)

   PDF (English) Statistics Forum
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 }
时间: 2024-07-31 04:35:45

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

LightOJ 1132 - Summing up Powers 矩阵快速幂+排列组合

链接:http://lightoj.com/volume_showproblem.php?problem=1132 1132 - Summing up Powers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given N and K, you have to find (1K + 2K + 3K + ... + NK) % 232 Input Input starts with an i

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

题目链接:http://lightoj.com/volume_showproblem.php?problem=1132 题意: 给定n.k,求(1K + 2K + 3K + ... + NK) % 232. 题解: 设sum(i) = 1K + 2K + 3K + ... + iK 所以要从sum(1)一直推到sum(n). 所以要找出sum(i)和sum(i+1)之间的关系: 好了可以造矩阵了. (n = 6时) 矩阵表示(大小为 1 * (k+2)): 初始矩阵start: 也就是: 特殊矩

LightOJ 1132 Summing up Powers(矩阵快速幂+二项式定理)

LightOJ 1132 题意: 给出N(1≤N≤1015),K(0≤K≤50): 计算:(1K+2K+3K+...+NK)%232. 思路: 根据二项式定理,我们可以得到: (n+1)k=C0knk+C1knk?1+C2knk?2+...+Ck?1kn+1 于是可以构造变化矩阵: ???????????C0k00...01C1kC0k?10...00C2kC1k?1C0k?2...00C3kC2k?1C1k?2...00..................111...11???????????

LightOJ - 1132 Summing up Powers 矩阵快速幂

题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 可以先打表,求出Cnm,用杨辉三角可以快速得到 #include<cstdio> typedef unsigned long long ll; const int N = 55; const ll mod = (1LL << 32); struct Matrix{ ll mat[N][N]; }A, B, tmp; ll n, num[N]; ll C[N][N]; int K

[伯努利数] poj 1707 Sum of powers

题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 735   Accepted: 354 Description A young schoolboy would like to calculate the sum for some fixed natural k and different

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

使用华邦的SPI FLASH作为EPCS时固化NIOS II软件报错及解决方案

Altera器件有EPCS系列配置器件,其实,这些配置器件就是我们平时通用的SPIFlash,据AlteraFAE描述:"EPCS器件也是选用某家公司的SPIFlash,只是中间经过Altera公司的严格测试,所以稳定性及耐用性都超过通用的SPIFlash".就本人看来,半导体的稳定性问题绝大部分都是由本身设计缺陷造成的,而成熟的制造工艺不会造成产品的不稳定:并且,现在Altera的器件在读入配置数据发生错误时,可以重新读取SPIFlash里面的数据,所以在工艺的稳定性以及设计的可靠性

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

AC日记——小A和uim之大逃离 II 洛谷七月月赛

小A和uim之大逃离 II 思路: spfa: 代码: #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f struct NodeType { int x,y,k; NodeType(int x_,int y_,int k_):x(x_),y(y_),k(k_){} NodeType(){} }; const int dx[5]={0,-1,0,1,0}; const int dy[5]={0,0,1,0,-