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. UESTC_秋实大哥与妹纸 2015 UESTC Training for Data Structures<Problem F>

    F - 秋实大哥与妹纸 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 1500/1500KB (Java/Others) Submit ...

  2. Best Time to Buy and Sell Stock 解答

    Question Say you have an array for which the ith element is the price of a given stock on day i. If ...

  3. LeeCode-Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. LeeCode-Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008

    Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...

  6. apache FtpServer 整合spring部署

    我们在项目中可能会出现这样的需求,使用ftp上传很大的文件后对需要对文件进行相应的逻辑处理,这时我们可以使用apache ftpServer来处理这段逻辑,只要我们做相应的部署和编写我们的逻辑代码,这 ...

  7. MP3/WAV 播放

    一.编译libmad  1.先下载压缩包到本地,并解压  tar -xvzf  libmad-0.15.1b.tar.gz   -C   ./ 2.进入源代码文件夹并配置 编写一个配置文件,便于< ...

  8. 【进制问题】【HDU2056】A + B Again

    A + B Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. 小谈@override

    @override是jdk1.5增加的注解,主要是用来声明子类的某方法覆盖了父类的某方法.非常简单的注解,但是有个小问题: 项目最开始使用的是jdk1.6,mvc模式:接口 ----> 实现类. ...

  10. c# 面相对象4-多态性

    一.定义: 多态是面向对象程序设计的又一个特性.在面向过程的程序设计中,主要工作是编写一个个的过程或函数,这些过程和函数不能重名.例如在一个应用中,需要对数值型数据进行排序,还需要对字符型数据进行排序 ...