nyoj_299_Matrix Power Series_矩阵快速幂
Matrix Power Series
- 描述
- Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
- 输入
- The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 10^9) and m (m < 10^4). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.
- 输出
- Output the elements of S modulo m in the same way as A is given.
- 样例输入
-
2 2 4
0 1
1 1 - 样例输出
-
1 2
2 3 - 来源
- POJ Monthly
- 上传者
- 张云聪
#include <iostream>
#include <cstdio> using namespace std; int M=; struct Matrix{
long int line,column;
long int m[][];
};
struct Matr{
long int line,column;
long int m[][];
Matr(Matrix x){
line =x.line*;
column=x.column*;
for(int i=;i<x.line*;i++){
for(int j=;j<x.column;j++){
m[i][j]=x.m[i%x.line][j];
}
}
for(int i=;i<x.line;i++){
for(int j=x.column;j<x.column*;j++){
m[i][j]=;
}
}
for(int i=x.line;i<x.line*;i++){
for(int j=x.column;j<x.column*;j++){
if(i==j){
m[i][j]=;
}else{
m[i][j]=;
}
}
}
}
}; Matr mult(Matr a,Matr b){
Matr ans(a);
ans.line=a.line;
ans.column=b.column;
//ans=inist(ans,0);
for(int i=;i<ans.line;i++){
for(int j=;j<ans.column;j++){
ans.m[i][j]=;
for(int k=;k<ans.column;k++){
ans.m[i][j]+=(a.m[i][k]*b.m[k][j]);
ans.m[i][j]%=M;
}
}
}
return ans;
} Matr fast_matrix(Matr x,int n){
Matr an(x),tmp(x);
for(int i=;i<x.line;i++){
for(int j=;j<x.column;j++){
an.m[i][j]=x.m[i+x.line/][j];
}
}
an.line/=;
while(n){
if(n%!=){
an=mult(an,tmp);
}
tmp=mult(tmp,tmp);
n>>=;
}
return an;
} int main()
{
int n,m,k;
Matrix a;
scanf("%d %d %d",&n,&k,&m);
M=m;
a.line=n;
a.column=n;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
scanf("%d",&a.m[i][j]);
}
}
Matr ans(a);
Matr ans2=fast_matrix(ans,k-);
for(int i=;i<ans2.line;i++){
for(int j=;j<ans2.column/;j++){
printf("%d ",ans2.m[i][j]);
}
printf("\n");
}
return ;
}
nyoj_299_Matrix Power Series_矩阵快速幂的更多相关文章
- 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 矩阵快速幂
设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] ...
- 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 矩阵快速幂 乘积
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 18450 Accepted: ...
- POJ3233:Matrix Power Series(矩阵快速幂+递推式)
传送门 题意 给出n,m,k,求 \[\sum_{i=1}^kA^i\] A是矩阵 分析 我们首先会想到等比公式,然后得到这样一个式子: \[\frac{A^{k+1}-E}{A-E}\] 发现要用矩 ...
- POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 27277 Accepted: ...
- POJ3233 Matrix Power Series(矩阵快速幂+分治)
Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...
- POJ3233Matrix Power Series(矩阵快速幂)
题意 题目链接 给出$n \times n$的矩阵$A$,求$\sum_{i = 1}^k A^i $,每个元素对$m$取模 Sol 考虑直接分治 当$k$为奇数时 $\sum_{i = 1}^k A ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
随机推荐
- linux下如何查看chm文件
转自:http://www.cnblogs.com/jesseZh/p/4036811.html (64位) sudo dpkg -i chmsee_1.3.0-2ubuntu2_amd64.d ...
- 【JavaScript】JS_Object跟Function的区别
JS_Object和Function的区别 我们本次的解释,主要通过下图 粗看该图,估计你不一定能看明白.不过接下来让我逐行向你解释. 最左侧:意思是,有两个对象f1和f2,他们是通过new Foo( ...
- 可编辑select
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- StarWind的安装配置
将安装包拷贝到本地后,执行以下exe文件,默认下一步,直到安装完成. 需汉化版本则将language_chinese.xml文件拷贝到以下路径: C:\Program Files\StarWind S ...
- 8-IO总结
3. 4. 5.
- jQuery - 动态创建iframe并加载页面
<html> <head> <script language="JavaScript" src="jquery-1.11.1.min.js& ...
- gcc 和g++区别
gcc和g++都是GNU的一个编译器;这两者的区别:1.从源文件上看,对于文件后缀(扩展名)为.c的test.c文件,gcc会把它看成是C程序,而g++则会把它看成是C++程序;而对于文件后缀(扩展名 ...
- setImageResource和setBackgroundResource的區別
它們的原型如下:void android.widget.ImageView.setImageResource(int resId)void android.view.View.setBackgroun ...
- shareSDK
一. 编写sharesdk代码 在AppDelegate.m中 //shareSDK相关 #import <ShareSDK/ShareSDK.h> #import "Weibo ...
- python 数据结构 初学时没太注意却发现很有用的点点滴滴
1. list.extend(L) 将指定列表中的所有元素附加到另一个列表的末尾:相当于a[len(a):] = L. 2. list.pop([i]) 删除列表中指定位置的元素并返回它.如果未指定索 ...