UVa 11149 Power of Matrix(倍增法、矩阵快速幂)
题目链接: 传送门
Power of Matrix
Time Limit: 3000MS
Description
给一个n阶方阵,求A1+A2+A3+......Ak。
思路
A1+A2+...+An = (A1+A2+...+An/2)+(A1+A2+...+An/2) * An/2 = (1 + An/2 ) * (A1+A2+...+An/2)那么对于(A1+A2+...+An/2)也能用同样的方法去求,不断对半下去计算,最后总体复杂度为log(n)^2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 50;
const int mod = 10;
struct Matrix
{
int mat[50][50];
int r,c;
Matrix(int r1 = 0,int c1 = 0):r(r1),c(c1)
{
memset(mat,0,sizeof(mat));
}
void E()
{
memset(mat,0,sizeof(mat));
for (int i = 0;i < r;i++)
{
for (int j = 0;j < c;j++)
{
mat[i][j] = (i == j);
}
}
}
Matrix operator+(const Matrix & m)
{
Matrix res(r,c);
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
res.mat[i][j] = (mat[i][j] + m.mat[i][j])%mod;
}
}
return res;
}
Matrix operator * (const Matrix & m)
{
Matrix res(r,m.c);
for (int i = 0; i < r; i++)
{
for (int j = 0; j < m.c; j++)
{
for (int k = 0; k < c; k++)
{
res.mat[i][j] = (res.mat[i][j] + mat[i][k] * m.mat[k][j])%mod;
}
}
}
return res;
}
void show()
{
for (int i = 0;i < r;i++)
{
bool first = true;
for (int j = 0;j < c;j++)
{
first?printf("%d",mat[i][j]):printf(" %d",mat[i][j]);
first = false;
}
printf("\n");
}
}
};
Matrix pow(Matrix x,int n)
{
Matrix res(x.r,x.c);
res.E();
while (n > 0)
{
if (n&1)
{
res = res * x;
}
x = x * x;
n >>= 1;
}
return res;
}
Matrix sum(Matrix mat,int k)
{
if (k == 1)
{
return mat;
}
Matrix E(mat.r,mat.c);
E.E();
if (k&1)
{
return (E + pow(mat,k/2))*sum(mat,k/2) + pow(mat,k);
}
else
{
return (E + pow(mat,k/2))*sum(mat,k/2);
}
}
int main()
{
int n,k;
while (~scanf("%d%d",&n,&k) && n && k)
{
Matrix Mat(n,n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d",&Mat.mat[i][j]);
Mat.mat[i][j] %= mod;
}
}
if (k == 0)
{
Mat.show();
continue;
}
Mat = sum(Mat,k);
Mat.show();
printf("\n");
}
return 0;
}
UVa 11149 Power of Matrix(倍增法、矩阵快速幂)的更多相关文章
- Power of Matrix(uva11149+矩阵快速幂)
Power of Matrix Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- UVA 11149 - Power of Matrix(矩阵乘法)
UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞.∑kiAi=(1+Ak/2)∑k/2iAi,不断二分就可以 代码: ...
- UVA 11149 Power of Matrix
矩阵快速幂. 读入A矩阵之后,马上对A矩阵每一个元素%10,否则会WA..... #include<cstdio> #include<cstring> #include< ...
- POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- HDU - 4965 Fast Matrix Calculation 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4965 题意 给出两个矩阵 一个A: n * k 一个B: k * n C = A * B M = (A ...
- UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)
题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...
- UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)
题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...
- UVA 11149 Power of Matrix 快速幂
题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...
随机推荐
- logstash搭建日志追踪系统
前言 开始博客之前,首先说下10月份没写博客的原因 = =. 10月份赶上国庆,回了趟老家休息了下,回来后自己工作内容发生了点改变,开始搞一些小架构的东西以及研究一些新鲜东西,当时我听到这个消息真的是 ...
- [转]关于Python中的yield
在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何 ...
- Pattern Recognition And Machine Learning读书会前言
读书会成立属于偶然,一次群里无聊到极点,有人说Pattern Recognition And Machine Learning这本书不错,加之有好友之前推荐过,便发了封群邮件组织这个读书会,采用轮流讲 ...
- Bootstrap系列 -- 10. 网格布局
一. 实现原理 网格布局是通过容器的大小,平均分为12份(可以修改),再调整内外边距,和表格布局有点类似但是也存在区别. 实现步骤如下: (1) 数据行.row 必须包含在容器.container 中 ...
- 自己留存:小经验在asp.net 4.5或者asp.net mvc 5解决A potentially dangerous Request.Form value was detected from the client
以前的解决办法是 <configuration> <system.web> <pages validateRequest="false&q ...
- 20140207 - Java and Mac OS X Retina
在Mac下使用文件管理工具类似Total Commander的muCommander,muCommander的编写语言是Java,打开后发现Java不兼容Mac Retina. muCommander ...
- Http协议中的Content-Length属性
Android开发的时候需要与从服务器上获取数据,数据是通过http协议封装的.Android端使用的是Xutils第三方插件来发起http请求,但是每次只能拿到部分数据.通过仔细分析后原来是Cont ...
- python file 文件读写
python 文本对象 继承自C的stdio包 打开 可以用内置的open()函数创建 with open("hello.txt") as f: for line in f: pr ...
- "此站点已经禁用应用程序"在sharepoint 2013中通过v2013部署app提示该错误
该错误的原文是:the apps are disabled in this site 可以在yahoo或者bing上搜索这个错误,可以找到解决办法: msdn上也有该错误解决办法,但是如果搜索中文,目 ...
- 一键系统优化15项脚本,适用于Centos6.x
#!/bin/sh ################################################ #Author:nulige # qqinfo:1034611705 # Date ...