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. 把程序嵌入网页之ATL编写ActiveX[标准窗口+接受参数]

    从VS2010开始ATL ActiveX支持IObjectSafety接口,所以用VS2010来编写,新建一个ATL项目 向导的第一页没什么东西,直接下一步,选项可以根据具体需求调整 点“完成”,切换 ...

  2. 【转】android 电池(一):锂电池基本原理篇

    关键词:android  电池关机充电 androidboot.mode charger 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...

  3. PHP Database ODBC 之 ODBC

    ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数据库). 创建 ODBC ...

  4. hdu 5610 Baby Ming and Weight lifting

    Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which c ...

  5. actionInvocation

    1.actionInvocation是什么 ActionInvocation就是Action的调用者.ActionInvocation在Action的执行过程中,负责Interceptor.Actio ...

  6. SHDP--Working With HBase (二)之HBase JDBC驱动Phoenix与SpringJDBCTemplate的集成

    Phoenix:Phoenix将SQL查询语句转换成多个scan操作,并编排执行最终生成标准的JDBC结果集.   Spring将数据库访问的样式代码提取到JDBC模板类中,JDBC模板还承担了资源管 ...

  7. C#常用語法糖(Csharp Syntactic sugar)

    首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1.  ...

  8. uva 10911 - Forming Quiz Teams(记忆化搜索)

    题目链接:10911 - Forming Quiz Teams 题目大意:给出2 * n个选手的坐标, 要求将所有的选手分成n组, 每组两个人, 所有组的两个人之间的距离之和要最小, 输出最小值. 解 ...

  9. Oracle SQL函数之转换函数To_char汇总

    TO_CHAR(x[[,c2],C3])[功能]将日期或数据转换为char数据类型[参数]x是一个date或number数据类型.c2为格式参数c3为NLS设置参数如果x为日期nlsparm=NLS_ ...

  10. 解决net-snmp正确输出MAC地址和判断空的IP地址

    function readVarbinds (buffer, varbinds) { buffer.readSequence (); while (1) { buffer.readSequence ( ...