POJ 3233 Matrix Power Series(构造矩阵求等比)
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=I+A+A^2+...+A^N-1;
构造矩阵[S,A^N]*[1,1]
[I, I ] [1,A]
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
typedef long long int LL;
using namespace std; int n,k,mod; struct Matrix
{
int a[*][*];
Matrix(){memset(a,,sizeof(a));}
Matrix operator* (const Matrix &p)
{
Matrix res;
for(int i=;i<*n;i++)
{
for(int j=;j<*n;j++)
{
for(int k=;k<*n;k++)
{
res.a[i][j]+=(a[i][k]*p.a[k][j]%mod);
}
res.a[i][j]%=mod;
}
}
return res;
}
}ans,base; Matrix quick_pow(Matrix base,int k)
{
Matrix res;
for(int i=;i<*n;i++)
{
res.a[i][i]=;
}
while(k)
{
if(k&) res=res*base;
base=base*base;
k>>=;
}
return res;
} void init_Matrix()
{
for(int i=;i<*n;i++)
{
ans.a[i][i]=;
}
for(int i=;i<n;i++)
{
base.a[n+i][i]=;
base.a[n+i][n+i]=;
}
} int main()
{
while(~scanf("%d%d%d",&n,&k,&mod))
{
init_Matrix();
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&base.a[i][j]);
}
}
ans=ans*quick_pow(base,k+);
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
int tmp=ans.a[n+i][j]%mod;
if(i==j) tmp=(tmp+mod-)%mod;
printf("%d%c",tmp,j==n-?'\n':' ');
}
}
}
return ;
}
POJ 3233 Matrix Power Series(构造矩阵求等比)的更多相关文章
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
- 题解报告:poj 3233 Matrix Power Series(矩阵快速幂)
题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, fin ...
- POJ 3233 Matrix Power Series 二分+矩阵乘法
链接:http://poj.org/problem?id=3233 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思 ...
- Poj 3233 Matrix Power Series(矩阵二分快速幂)
题目链接:http://poj.org/problem?id=3233 解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k ...
- POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)
题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ...
- POJ 3233 Matrix Power Series (矩阵+二分+二分)
题目地址:http://poj.org/problem?id=3233 题意:给你一个矩阵A,让你求A+A^2+……+A^k模p的矩阵值 题解:我们知道求A^n我们可以用二分-矩阵快速幂来求,而 当k ...
- POJ 3233 Matrix Power Series (矩阵快速幂)
题目链接 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A^2 + A^3 + - ...
- POJ - 3233 Matrix Power Series (矩阵等比二分求和)
Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. ...
随机推荐
- zTree的内核
zTree的内核: * 结构 (function($){ //常量部分 //申明常量是因为这些数据是不能改变的,如果成了对象的属性,很容易就改 ...
- VC 6中配置OpenGL开发环境
2010,2012中配置类似 http://hi.baidu.com/yanzi52351/item/f9a600dffa4caa4ddcf9be1d VC 6中配置OpenGL开发环境 这里,我习惯 ...
- 在ubuntu上建立多svn版本库
http://lucane.iteye.com/blog/857486 mkdir ~/dev/repository svnadmin create ~/dev/repository/a svnadm ...
- tomcat,zookeeper,activeMQ,Kafka设置jvm参数
1,tomcat设置jvm参数 设置方法: 在tomcat bin 目录增加配置:setenv.sh #add tomcat pid CATALINA_PID="$CATALINA_ ...
- 构造函数this和base的区别
构造函数this和base的区别: this:调用的是本身,不能调用父类和子类的 base:调用父类的,不能调用本身的,但别人继承,可以调用 从中也可以得出另外个结果构造函数的运行过程 先从基类开始构 ...
- html页面定位
对于css中的position:relative;我始终搞不明白它是什么意思,有什么作用? 对于position的其它几个属性,我都搞懂了 引用 static : 无特殊定位,对象遵循HTML定位规则 ...
- 改变this指针的apply,call,bind的区别
apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. Jav ...
- android五种存储方式
http://www.cnblogs.com/smalltigerlee/archive/2011/11/10/2244143.html
- Easy Tag Write(2)
package skyseraph.android.util.nfc; import com.google.common.collect.BiMap; import com.google.common ...
- JavaWeb-springMVC
<context:component-scan/> 扫描指定的包中的类上的注解,常用的注解有: @Controller 声明Action组件@Service 声明Service组件 ...