poj 3233 Matrix Power Series
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}
Ak
可用二分迭代求出
因此,只要求出 上面的三部分就可以求出 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的更多相关文章
- 矩阵十点【两】 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的迹(就是主对角线上各项的 ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- [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: ...
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- 线性代数(矩阵乘法):POJ 3233 Matrix Power Series
Matrix Power Series Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
随机推荐
- Sed 与 Linux 等价命令代码鉴赏(转)
参考了 http://www.chinaunix.net/jh/24/307045.html sed http://bbs.chinauni ...
- python每次处理一个字符的三种方法
python每次处理一个字符的三种方法 a_string = "abccdea" print 'the first' for c in a_string: print ord(c) ...
- UVA 10047-The Monocycle(队列bfs+保存4种状态)
题意:给你一张地图,S代表起点,T代表终点,有一个轮盘,轮盘平均分成5份,每往前走一格恰好转1/5,轮盘只能往前进,但可以向左右转90°,每走一步或是向左向右转90° 要花费1单位的时间,问最少的时间 ...
- HDU5137 How Many Maos Does the Guanxi Worth(枚举+dijkstra)
How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/5 ...
- A canvas fillText and strokeText example
A canvas fillText and strokeText example A canvas fillText and strokeText example
- (转)Java 的swing.GroupLayout布局管理器的使用方法和实例
摘自http://www.cnblogs.com/lionden/archive/2012/12/11/grouplayout.html (转)Java 的swing.GroupLayout布局管理器 ...
- Python的ASCII, GB2312, Unicode , UTF-8 相互转换
ASCII 是一种字符集,包括大小写的英文字母.数字.控制字符等,它用一个字节表示,范围是 0-127 Unicode分为UTF-8和UTF-16.UTF-8变长度的,最多 6 个字节,小于 127 ...
- distinct() 去重复
distinct 是对整个结果集进行数据重复抑制,而不是针对每一个列. select distinct FDepartment from T_Employee
- ESCAPE用法
ESCAPE用法1.使用 ESCAPE 关键字定义转义符: 在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符. 2.ESCAPE 'escape_character' 允许在字符串中搜索通 ...
- 【考虑周全+数学变形】【11月赛】Is it a fantastic matrix?
Is it a fantastic matrix? Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/ ...