A为一个n*n的矩阵,求A+A^2+A^3+...+A^n

Sk = A + A2 + A3 + … + Ak  

    =(1+Ak/2)*(A + A2 + A3 +
… + Ak/2  )+{Ak}

    =(1+Ak/2)*(Sk/2 )+{Ak}//
k为偶数时无 {Ak}

A
 
可用二分迭代求出

因此,只要求出 上面的三部分就可以求出 Sk

设f(n)=A+A^2+A^3+...+A^n

n%2==1时,f(n)=f(n-1)+A^n

n%2==0时,f(n)=f(n/2)+f(n/2)*A^(n/2)

由于矩阵乘法满足结合律,计算A^n时,也可以二分

#include<iostream>
#include<cstdio>
using namespace std;
int n,m;
struct Mat
{
int mat[31][31];
Mat operator*(const Mat &x)
{
Mat tmp;
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
tmp.mat[i][j]=0;
for(k=0;k<n;k++)
{
tmp.mat[i][j]+=(mat[i][k]*x.mat[k][j])%m;
tmp.mat[i][j]%=m;
} }
}
return tmp;
}
Mat operator+(const Mat &x)
{
Mat tmp;
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
tmp.mat[i][j]=(mat[i][j]+x.mat[i][j])%m;
}
return tmp;
}
}p;
Mat _pow(int k)
{
if(k==1)
return p;
if(k&1)
return _pow(k-1)*p;
else
{
Mat tmp=_pow(k/2); return tmp*tmp;
}
}
Mat cal(int k)
{
if(k==1)
return p;
else
{
if(k&1)
return cal(k-1)+_pow(k);
else
{
Mat tmp=cal(k/2);
return tmp+tmp*_pow(k/2);
}
}
}
int main()
{
int i,j,k;
scanf("%d%d%d",&n,&k,&m);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&p.mat[i][j]);
}
Mat res=cal(k);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
printf("%d ",res.mat[i][j]);
}
printf("%d\n",res.mat[i][j]);
}
return 0;
}

poj 3233 Matrix Power Series的更多相关文章

  1. 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

    poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  2. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  3. POJ 3233 Matrix Power Series (矩阵乘法)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11954   Accepted:  ...

  4. [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15417   Accepted:  ...

  5. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  6. 线性代数(矩阵乘法):POJ 3233 Matrix Power Series

    Matrix Power Series   Description Given a n × n matrix A and a positive integer k, find the sum S = ...

  7. POJ 3233 Matrix Power Series(二分等比求和)

    Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...

  8. POJ 3233 Matrix Power Series(矩阵快速幂)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...

  9. poj 3233 Matrix Power Series(矩阵二分,高速幂)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted:  ...

  10. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

随机推荐

  1. Oracle 10g体系机构及安全管理《思维导图》

    通过学习Oracle数据库后,我对数据库又有了进一步的了解,Oracle数据库 10g体系机构分为 物理和逻辑存储结构. 下面是我对Oracle数据库做的思维导图和笔记 (全屏查看:<思维导图& ...

  2. [一步一步MVC]第二回:还是ActionFilter,实现对业务逻辑的统一Authorize处理 OnActionExecuting内如何获取参数

    如何获取参数:http://www.cnblogs.com/anytao/archive/2009/04/23/anytao-mvc-02-actionauthorize.html 由问题引出 在AS ...

  3. 2.7 Structured Regression Models

    $RSS(f)=\sum_i^N \left(y_i-f(x_i)\right)^2$ 当数据量足够大时,数据存在相同$x_i$,不同$y_{il},l=1\cdots t$ 则得到的f即为条件均值$ ...

  4. Davinci开发板DM368 nandwrite.c简要分析

    #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <limits.h ...

  5. SSH框架-Caused by: org.hibernate.MappingException: column attribute may not be used together with <column> subelement

    昨晚修改了一些表关系,在相关的hbm.xml文件中做了改动,今天早上起来启动tomcat后,发现项目启动不了,控制台报错: 2015-6-14 9:09:42 org.apache.catalina. ...

  6. Atom编辑器

    Atom介绍 Github的员工Nathan Sobo在Atom的博客中提到:”Sublime和TextMate十分方便,但是扩展性不足:另一方面,Emacs和 Vim扩展性很强却需要学习日程工作中很 ...

  7. 关于iconfont

    1. 从FONT-FACE说起 要想了解iconfont,得从一个新的css3规则说起.css3中,新增了一种样式规则,@font-face,这个规则可以用来引入自定义的字体,到客户端.以前,我们的字 ...

  8. CMD下用csc.exe编译.cs 代码

    用惯了VS来写C#代码,要是用记事本写会不会觉得很好玩,然后再CMD窗口下编译运行,当然写一些简单的Console代码还是可以这么玩玩的,如果你觉得打开VS太麻烦的话. 把后缀名改成.cs,test. ...

  9. 初识ege图形库

    简介: EGE (Easy Graphics Engine),是Windows 下的简易绘图库, 是一个类似BGI (graphics.h)的 面向C/C++语言新手的图形库,对新手来说,简单,友好, ...

  10. 给WebApp加一个“壳”,实现Andriod系统添加到桌面

    IOS系统的Safari浏览器有一个“添加到桌面”的功能,能在手机桌面上为你的Webapp添加一个快捷方式,其外观和Native App看起来一样. 这个功能对Webapp来说太有用了,它能让用户像“ ...