Power of Matrix(uva11149+矩阵快速幂)
Power of Matrix
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
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+27 = ?= (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+矩阵快速幂)的更多相关文章
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- 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 ...
- ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)
Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...
- HDU 4965 Fast Matrix Calculation 矩阵快速幂
题意: 给出一个\(n \times k\)的矩阵\(A\)和一个\(k \times n\)的矩阵\(B\),其中\(4 \leq N \leq 1000, \, 2 \leq K \leq 6\) ...
- bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希
题目 给定矩阵A, B和模数p,求最小的正整数x满足 A^x = B(mod p). 分析 与整数的离散对数类似,只不过普通乘法换乘了矩阵乘法. 由于矩阵的求逆麻烦,使用 $A^{km-t} = B( ...
- 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) ...
- Fast Matrix Calculation 矩阵快速幂
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learni ...
- 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 ...
- uva11149矩阵快速幂
求A+A^1+...+A^n 转换一下变成|A E|,的n+1次方就是|A^(n+1) A^n+...+A+E| |0 E| | 0 ...
随机推荐
- AJPFX:外汇的爆仓和追加保证金
在外汇交易中,当可用保证金变成0时,账户即会爆仓.而为了防止爆仓,您可以在可用保证金不足时追加保证金以防止爆仓. 例如,您在AJPFX的账户是100倍的杠杆,一手欧美货币对合约为10万美金(1 LOT ...
- 伪装为 吃鸡账号获取器 的QQ木马分析
本文作者:i春秋作家坏猫叔叔 0×01 起因随着吃鸡热潮的来临,各种各样的吃鸡辅助和账号交易也在互联网的灰色地带迅速繁殖滋生.其中有真有假,也不乏心怀鬼胎的“放马人”.吃过晚饭后在一个论坛看到了这样一 ...
- 脚本中 if 判断细节
if [[ $1 == "fedora" ]];then echo "redhat" elif [[ $1 == "redhat" ]];t ...
- ssh免密码登录Permission denied (publickey,gssapi-keyex,gssapi-with-mic) 的解决方案!
当出现Permission denied (publickey,gssapi-keyex,gssapi-with-mic) 警告的时候,恭喜你,你已经离成功很近了. 远程主机这里设为slave2,用户 ...
- nginx官方文档 之 http负载均衡 学习笔记
一.负载均衡 算法 大致可以分两类: (1)不能保证用户的每一次请求都通过负载均衡到达同一服务器. (2)可保证用户的每一次请求都通过负载均衡到达同一服务器. 第二类的应用场景: 1.如果服务器有缓存 ...
- linux shell使用别名,切换当前目录
别名alias 别名就是一种快捷方式,以省去用户输入一长串命令序列的麻烦. 别名使用alias命令 比如 alias cp='cp -i' alias l.='ls -d .* --color=tty ...
- POJ 2501
#include<iostream> #include<iomanip> #include<stdio.h> #include<string> #inc ...
- Java实现二分法排序
二分法:(二分法不是只能做数组,这里的数组只是为了举例) 在给出的有序排列的数组中,把目标值和数组中间值进行比较,如果相等,则返回中间值下标,如果目标值小于中间值,就从数组的前半段再次执行二分法查找, ...
- (转)Python自动化运维之13、异常处理及反射(__import__,getattr,hasattr,setattr)
原文:http://www.cnblogs.com/xiaozhiqi/p/5778856.html https://blog.csdn.net/zong596568821xp/article/det ...
- WebStorm project 打开多个项目的方法
File ---> Setting ---> Project:xxx ---> Sirectories 点击右侧 + Add content root,选择目录后即可显示该项目. ...