Power of Matrix

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-03-15)

Description

 

Problem B : Power of Matrix

Time limit: 10 seconds

Consider an n-by-n matrix A. 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.

Example

Suppose A = . Then A2 =  = , thus:

Such computation has various applications. For instance, the above example actually counts all the paths in the following graph:

Input

Input consists of no more than 20 test cases. The first line for each case contains two positive integers n (≤ 40) and k (≤ 1000000). This is followed by n lines, each containing n non-negative integers, giving the matrix A.

Input is terminated by a case where n = 0. This case need NOT be processed.

Output

For each case, your program should compute the matrix A + A2 + A3 + ... + Ak. Since the values may be very large, you only need to print their last digit. Print a blank line after each case.

Sample Input

3 2
0 2 0
0 0 2
0 0 0
0 0

Sample Output

0 2 4
0 0 2
0 0 0

首先我们来想一下计算A+A^2+A^3...+A^k。

如果A=2,k=6。那你怎么算

2+22+23+24+25+26 = ?= (2+22+23)*(1+23)

如果A=2,k=7。那你怎么算

2+22+23+24+25+26+2= ?= (2+22+23)*(1+23)+27

so....同理:

当k是偶数,A+A^2+A^3...+A^k=(E+A^(k/2))*(A+A^2...+A^(k/2))。

当k是奇数,A+A^2+A^3...+A^k=(E+A^(k/2))*(A+A^2...+A^(k/2))+A^k。

转载请注明出处:寻找&星空の孩子

题目链接:UVA 11149

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL __int64
#define mmax 45 struct matrix
{
int mat[mmax][mmax];
}; int N; matrix multiply(matrix a,matrix b)
{
matrix c;
memset(c.mat,,sizeof(c.mat));
for(int i=; i<N; i++)
{
for(int j=; j<N; j++)
{
if(a.mat[i][j]==)continue;
for(int k=; k<N; k++)
{
if(b.mat[j][k]==)continue;
c.mat[i][k]=(c.mat[i][k]+a.mat[i][j]*b.mat[j][k])%; }
}
}
return c;
} matrix quickmod(matrix a,int n)
{
matrix res;
for(int i=; i<N; i++) //单位阵
for(int j=; j<N; j++)
res.mat[i][j]=(i==j);
while(n)
{
if(n&)
res=multiply(a,res);
a=multiply(a,a);
n>>=;
}
return res;
}
matrix add (matrix a,matrix b)
{
matrix ret;
for(int i=; i<N; i++)
for(int j=; j<N; j++)
ret.mat[i][j]=(a.mat[i][j]+b.mat[i][j])%;
return ret;
}
matrix solve(matrix a,int k)
{
if(k==) return a;
matrix ans;
for(int i=; i<N; i++)
for(int j=; j<N; j++)
ans.mat[i][j]=(i==j);
if(k==) return ans;
ans=multiply((add(quickmod(a,(k>>)),ans)),solve(a,(k>>)));
if(k%) ans=add(quickmod(a,k),ans);
return ans;
} int main()
{
int k;
while(scanf("%d%d",&N,&k)!=EOF)
{
if(!N)break;
matrix ans;
for(int i=;i<N;i++)
{
for(int j=;j<N;j++)
{
int temp;
scanf("%d",&temp);
ans.mat[i][j]=temp%;
}
} ans=solve(ans,k); for(int i=;i<N;i++)
{
for(int j=;j<N-;j++)
{
printf("%d ",ans.mat[i][j]);
}
printf("%d\n",ans.mat[i][N-]);
}
printf("\n");
}
return ;
}

Power of Matrix(uva11149+矩阵快速幂)的更多相关文章

  1. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  2. hdu4965 Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  3. ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

    Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...

  4. HDU 4965 Fast Matrix Calculation 矩阵快速幂

    题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...

  5. bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希

    题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...

  6. UVA-11149 Power of Matrix(矩阵二分幂)

    题目大意:给一个n阶方阵,求A1+A2+A3+......Ak. 题目分析:令F(k)=A1+A2+A3+......Ak.当k为偶数时,F(k)=F(k/2)*(E+Ak/2),k为奇数时,F(k) ...

  7. Fast Matrix Calculation 矩阵快速幂

    One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...

  8. UVA11149 矩阵快速幂

    首先我们来想一下计算A+A^2+A^3...+A^k. 如果A=2,k=6.那你怎么算 2+22+23+24+25+26 = ?= (2+22+23)*(1+23) 如果A=2,k=7.那你怎么算 2 ...

  9. uva11149矩阵快速幂

    求A+A^1+...+A^n 转换一下变成|A  E|,的n+1次方就是|A^(n+1)  A^n+...+A+E| |0  E|                       |    0       ...

随机推荐

  1. 关于css3中的flex

    参考几篇文章: Flex 布局语法教程 IE10中的Flexible Box("Flexbox")布局 “老”的Flexbox和“新”的Flexbox 一个可以练习的地方: NEW ...

  2. java变量初始化顺序

    第一次实例化一个类时,初始化优先顺序为: 1.父类中的静态成员变量和静态代码块初始化 2.本类中的静态成员变量和静态代码块初始化 3.父类中的实例成员初始化 4.父类中的构造方法 5.本类中的实例成员 ...

  3. 抓取分析网页批量下载评书(3)之批量下载mp3

         本系列目录:    <1.搜索有声小说>    <2.分析详细页地址>     <3.批量下载mp3>      本篇是大结局,看过前两篇的放心吧,不会有 ...

  4. PHP 中 var_export、print_r、var_dump 调试中的区别

    1.output basic type 代码 $n = "test"; var_export($n); print_r($n); var_dump($n); echo '----- ...

  5. Upgrade Win10

    Internal deployment of Windows 10 Enterprise is currently underway as a phased deployment. Watch you ...

  6. iOS开发之Todo List for Swift项目

    一直从事Windows Phone开发,但对iOS开发一直有所好奇,于是在MBP到手之际,顺手安装了Xcode.移动互联网开发的相似性,使得我能快速地了解和认识了iOS的开发框架体系,在看完了Appl ...

  7. flask-Datatables

    我先给大家推荐一个jQuery开源库网址  http://www.jq22.com/ Datatables 是一款jquery表格插件.他是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能 ...

  8. Jenkins配置项目

    前提:服务器上部署了jenkins+Tomcat,并且安装了所需插件 1.新建项目 -- 项目配置 2.配置git地址 出现上述错误是因为该git地址,在jenkins服务器上无权限访问.在git上开 ...

  9. vue教程1-03 v-for循环

    vue教程1-03 v-for循环 v-for循环: v-for="name in arr" {{value}} {{$index}} v-for="name in js ...

  10. [Umbraco] 熟悉管理页面

    登录到umbraco管理界面后,发现其后台管理页很简洁 首页看到左侧部分是一个Content和Sections,右侧是管理区域 介绍各个Sections代表的含义: "Content&quo ...