UVA11149_Power of Matrix
题目简洁明了,给出矩阵,求前k次方和。
不知道这种方法是叫做二分幂还是倍增法,如果有知道的,请告诉我一下。
具体思想是这样的,A^1+A^2+A^3+......A^n=(E+A^(n/2))*(A^1+A^2+.....A^(n/2)),如果n为奇数,那么我们只要加上多余的哪一项就可以满足条件了,于是我们就通过这个公式不断的二分下去,用一个矩阵保存左边的矩阵的值,然后右边始终一直二分就可以了,整个复杂度是log^2的。
不过,我看别人的代码都比我跑得快,所以鄙人觉得应该有更简洁的方法,求指教啊。。。。
召唤代码君:
#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 44
using namespace std; int N,m; struct Mat{
int a[][];
Mat(){
for (int i=; i<N; i++)
for (int j=; j<N; j++) a[i][j]=;
}
Mat(int x){
for (int i=; i<N; i++){
for (int j=; j<N; j++) a[i][j]=;
a[i][i]=;
}
}
Mat operator + (Mat M0) const {
Mat M1;
for (int i=; i<N; i++)
for (int j=; j<N; j++) M1.a[i][j]=(a[i][j]+M0.a[i][j])%;
return M1;
}
Mat operator * (Mat M0) const {
Mat M1;
for (int i=; i<N; i++)
for (int j=; j<N; j++)
for (int k=; k<N; k++)
M1.a[i][j]=(M1.a[i][j]+a[i][k]*M0.a[k][j])%;
return M1;
}
void input(){
for (int i=; i<N; i++)
for (int j=; j<N; j++) scanf("%d",&a[i][j]),a[i][j]%=;
}
void output(){
for (int i=; i<N; i++){
printf("%d",a[i][]);
for (int j=; j<N; j++) printf(" %d",a[i][j]);
printf("\n");
}
printf("\n");
}
}; Mat power(Mat M,int P){
Mat tot();
while (P){
if (P&) tot=tot*M;
P>>=,M=M*M;
}
return tot;
} Mat count(Mat M,int P){
Mat M0,E(),M1=E;
while (P){
if (P&) M0=M0+M1*power(M,P);
P>>=;
M1=M1*(E+power(M,P));
}
return M0;
} int main(){
Mat M;
while (scanf("%d%d",&N,&m) && N!=){
M.input();
M=count(M,m);
M.output();
}
return ;
}
UVA11149_Power of Matrix的更多相关文章
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- 如何决定DCOM是否可用
如何决定DCOM是否可用
- WCF之Binding详解
本文的出发点: 通过阅读本文,您能了解以下知识: WCF中的Binding是什么? Binding的组 成? Binding Element 的分类? Binding描述 了那些层面的信息? 选择正确 ...
- php用simplexml来操作xml
<?php$username = 'zhansan';if (!file_exists('001.xml')){ $fp = fopen('001.xml', 'w'); $xmlContent ...
- CentOS6.5上源码安装MongoDB3.2.1
1.环境准备: mkdir /home/mongodb #创建MongoDB程序存放目录 mkdir /data/mongodata -p #创建数据存放目录 mkdir /data/log/mong ...
- JavaScript 正则表达式的应用实例
都是自己实例记录,不断更新中.... 1.字符串找出所有匹配的邮箱并替换 <html> <body> <script type="text/javascript ...
- [delphi]运行cmd命令,并取得输出字符
http://blog.csdn.net/nerdy/article/details/8969189 [delphi]运行cmd命令,并取得输出字符 标签: delphiCMD命令 2013-05- ...
- 力软信息化系统快速开发框架 web端+winform端
力软信息化系统快速开发框架是一套集权限管理+快速开发+动态接口+通用组件+动态UI于一体的全新.net信息化快速开发框架.力软信息化系统快速开发框架的使用,大大地缩短了开发周期,提高了软件质量,同时也 ...
- angularjs(二)模板终常用的指令的使用方法
通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...
- oricle数据库关于定时
- Ubuntu 查询 so 归属的 package
. . . . . 今天 LZ 在运行一个程序的时候,出现找不到 so 库的情况: >$ ./core ./core: error : cannot open shared object fil ...