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}\] 发现要用矩 ...
随机推荐
- Android Studio 更新gradle插件
今天更新了CentOS, 更新了java版本. 然后gradle跪了..... 不吐槽java版本的兼容性问题了.... 反正有他自己的理由.... 那么就更新gradle咯.... 下面是方法... ...
- cocos2dx 3.x 开发环境搭建
1. 准备工作 (1)VS2012 (2)cocos2dx cn.cocos2d-x.org/download (3)python 新版本的cocos2dx 需要python编译 2. 安装软件 (1 ...
- spring oauth2相关资料
理解OAuth 2.0 *****http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html Secure REST API with oauth2 ...
- 思科ACL不连续通配符掩码的计算
access-list 120 permit ip 10.0.0.0 0.0.0.191 any 这条ACL看似简单,却又复杂,因为正常我们见到的通配符掩码都是诸如0.0.0.255(255. ...
- Win7-U盘安装出现"We were unable to copy your files. "
使用Windows 7 USB/DVD Download Tool时,提示We were unable to copy your files. Please check your USB device ...
- PE框架学习
PE开发基础: 开发平台PowerEngine: 开发新功能: 业务逻辑处理: 1.Transaction:交易 2.Chain:链.责任链 3.Command:命令 4.Template:模板 5. ...
- oracle db_*和v$*表
dba_开头 dba_users 数据库用户信息 dba_segments 表段信息 dba_extents 数据区信息 dba_objects 数据库对象信息 ...
- DelphiXE8FMX工程实现无边框托动(FMX内部方法)
注意: 可以实现效果,但不知道我的用法对不对(或着说是不是最优化的用法),望高手们指教. 实例代码: unit Unit1; interface uses System.SysUtils, Syste ...
- JS学习笔记(4)--js变量的生命周期
http://www.cnblogs.com/williamxiao/p/3499973.html 最近看国外经典教材的时候发现JavaScript与熟知的Java,C,C++都不同的特性,其中一个就 ...
- XML-RPC使用手册
内容列表 Preface: About This Manual Introduction to XML-RPC for C/C++ What is XML-RPC? How Does XML-RPC ...