POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 19338 Accepted: 8161
Description
Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
Input
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.
Output
Output the elements of S modulo m in the same way as A is given.
Sample Input
2 2 4
0 1
1 1
Sample Output
1 2
2 3
可以找到递推关系 : s[k]=s[k-1]+A^k;
然后构造矩阵,利用矩阵快速幂
具体见代码
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int n,k;
int m;
struct Node
{
int a[65][65];
};
Node multiply(Node a,Node b)
{
Node c;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
c.a[i][j]=0;
for(int k=1;k<=n;k++)
{
(c.a[i][j]+=(a.a[i][k]*b.a[k][j])%m)%=m;
}
}
}
return c;
}
Node quick(Node a,int x)
{
Node c;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
c.a[i][j]=(i==j?1:0);
for(x;x>0;x>>=1)
{
if(x&1)
c=multiply(c,a);
a=multiply(a,a);
}
return c;
}
int main()
{
while( scanf("%d%d%d",&n,&k,&m)!=EOF)
{
Node a;Node b;Node c;
memset(a.a,0,sizeof(a.a));
memset(b.a,0,sizeof(b.a));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&a.a[i][j+n]);
b.a[i+n][j+n]=a.a[i][j+n];
}
for(int i=1;i<=n;i++)
{
b.a[i][i]=1;
b.a[i+n][i]=1;
}
n=n*2;
c=multiply(a,quick(b,k));
for(int i=1;i<=n/2;i++)
for(int j=1;j<=n/2;j++)
if(j==n/2)printf("%d\n",c.a[i][j]);
else printf("%d ",c.a[i][j]);
}
return 0;
}
POJ 3233 Matrix Power Series(矩阵快速幂)的更多相关文章
- POJ 3233 Matrix Power Series 矩阵快速幂
设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- POJ 3233:Matrix Power Series 矩阵快速幂 乘积
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 18450 Accepted: ...
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ3233:Matrix Power Series(矩阵快速幂+二分)
http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...
- poj 3233 Matrix Power Series 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 27277 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(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- POJ3233:Matrix Power Series(矩阵快速幂+递推式)
传送门 题意 给出n,m,k,求 \[\sum_{i=1}^kA^i\] A是矩阵 分析 我们首先会想到等比公式,然后得到这样一个式子: \[\frac{A^{k+1}-E}{A-E}\] 发现要用矩 ...
随机推荐
- Graphics View框架
Qt4.2开始引入了Graphics View框架用来取代Qt3中的Canvas模块,并在很多地方作了改进,Graphics View框架实现了模型-视图结构的图形管理,能对大量图元进行管理,支持碰撞 ...
- layui关闭layer.open打开的页面
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引parent.layer.close(index);
- jquery的商品首页
js代码: $(function() { /*新闻滚动*/ var $this = $('.scrollNews'); var scrollTimer; $this.hover(function () ...
- error: no matching function for call to 'Ui::GoToCellDialog::setupUi(QDialog*&)' ui.setupUi(dialog); ^
环境:Qt5.3 参考书是:C++ GUI Qt4编程 问题描述: 按照书中的例子2-2做,编译时遇到的问题,从字面意思看是没有匹配的函数可用,UI::GotoCellDialog类是自动生成的,所以 ...
- 工作流Activiti的学习总结(十二) activiti官方十分钟快速学习 (zhuan)
http://topmanopensource.iteye.com/blog/1315341 ***************************************************** ...
- Hbase脚本小结
脚本使用小结: 1.开启集群,start-hbase.sh 2.关闭集群,stop-hbase.sh 3.开启/关闭所有的regionserver.zookeeper,hbase-daemons.sh ...
- Spring mvc 返回JSON 在IE 下提示下载 解决办法
http://www.blogjava.net/iamlibo/archive/2013/11/21/406646.html ————————————————————————————————————— ...
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...
- EasyUI 表单 tree
第一步:创建HTML标记 <divid="dlg"style="padding:20px;"> <h2>Account Info ...
- 多媒体开发之rtp 打包发流--- 从h264中获取分辨率
http://blog.csdn.net/DiegoTJ/article/details/5541877 http://www.cnblogs.com/lidabo/p/4482684.html 分辨 ...