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 ...
随机推荐
- 偶的《javascript框架设计》终于出版
#cnblogs_post_body p{ text-indent:2em!important; } 历时两年多,我的书终于付梓出版了.应各方面的要求,写软文一篇,隆重介绍一下此书对各位程序员的钱途有 ...
- 关于Onvif的event
昨天又仔细研究了一下camera的alarm功能,发现原来很简单,首先订阅一下,即create,拿到订阅号后直接pull,一旦收到信息就再次用订阅号pull.参考http://www.doc88.co ...
- macbook 重装win7
若是第一次已经成功好了,并且把第一次的安装U盘WININSTALL内容保存完好的前提下, win7要重新安装. 先进入Boot Camp移除Windows,备份好你的WIN系统的重要文件. 把第一次的 ...
- node npm 安装模块 淘宝镜像
npm --registry https://registry.npm.taobao.org info underscore
- 特殊字符(包含emoji)的梳理
背景知识 emoji表情符号,是20世纪90年代由NTT Docomo栗田穣崇(Shigetaka Kurit)创建的,词义来自日语(えもじ,e-moji,moji在日语中的含义是字符).emoji可 ...
- Content is not allowed in prolog ---UTF-8 无bom
- Android Studio开发基础之自定义View组件
一般情况下,不直接使用View和ViewGroup类,而是使用使用其子类.例如要显示一张图片可以用View类的子类ImageView,开发自定义View组件可分为两个主要步骤: 一.创建一个继承自an ...
- logback 配置详解(一)
一:根节点<configuration>包含的属性: scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配置文 ...
- 1_UILabel
// // ViewController.swift // 1_UILabel // // Created by Larry on 2016/12/7. // Copyright © 2016年 nf ...
- Android笔记:DrawerLayout抽屉布局的使用
DrawerLayout(抽屉布局),在各种app中经常出现,比如csdn.. 如下示,只要从屏幕侧边滑一下,或者点击左上角的图标,抽屉就会出来. DrawerLayout要点: 1.使用Drawer ...