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 ...
随机推荐
- Des加解密算法
class DesHelper { /// <summary> /// DES加密方法 /// </summary> ...
- xml对象的序列化和反序列化
对象序列化: /// <summary> /// 将一个对象序列化为XML字符串 /// </summary> /// <par ...
- 【231】◀▶ 利用 IDL 读取 TIFF 数据
参考:Create Latitude/Longitude Arrays for GeoTIFF Image 用到的函数为 READ_TIFF,通过此函数可以获取 TIFF 数据的数组信息,同时可以获取 ...
- java 枚举
DK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是: publicstaticfianl... ...
- Qt之QMutex
概述 QMutex 类使得线程之间可序列化,文档中的描述为provides access serialization between threads 它被设计的初衷是用来保护一个对象.数据结构.代码段 ...
- taiyi_interview(Introduction To Database Refactoring)
Introduction To Database Refactoring 原文链接:by Scott W. Ambler:http://www.tdan.com/view-articles/5010/ ...
- 通过系统自带的内容提供器(ContentResolver)读取系统的通讯录,并设置点击事件
1.布局 主布局只有一个listview,用来显示电话簿的名字和手机号码 <?xml version="1.0" encoding="utf-8"?> ...
- MVC中实现只有当用户登录成功的时候才等浏览内容,否则跳转到登录页面
第一步,在登录的时候记录Session //提供Session接口方便后面判断用户登录 Session["UserInfo"] = uinfo; //uInfo是用户登录Mode ...
- Java 中extends与implements使用方法
Java 中extends与implements使用方法 标签: javaclassinterfacestring语言c 2011-04-14 14:57 33314人阅读 评论(7) 收藏 举报 分 ...
- 匿名函数 lambda表达式(lambda expression)
阅读g2log时,发现有两行代码居然看不懂. 1. auto bg_call = [this, log_directory]() {return pimpl_->backgroundChang ...