POJ 3233 Matrix Power Series (矩阵乘法)
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 11954 | Accepted: 5105 |
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
Source
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int n,k,mod; struct Matrix{
int arr[][];
}; Matrix unit,init; Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=;i<n;i++)
for(int j=;j<n;j++){
c.arr[i][j]=;
for(int k=;k<n;k++)
c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j]%mod)%mod;
c.arr[i][j]%=mod;
}
return c;
} Matrix Pow(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=Mul(b,a);
}
x>>=;
a=Mul(a,a);
}
return b;
} Matrix Add(Matrix a,Matrix b){
Matrix c;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
c.arr[i][j]=(a.arr[i][j]+b.arr[i][j])%mod;
return c;
} Matrix solve(int x){
if(x==)
return init;
Matrix res=solve(x/),cur;
if(x&){
cur=Pow(init,unit,x/+);
res=Add(res,Mul(cur,res));
res=Add(res,cur);
}else{
cur=Pow(init,unit,x/);
res=Add(res,Mul(cur,res));
}
return res;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%d",&n,&k,&mod)){
for(int i=;i<n;i++)
for(int j=;j<n;j++){
scanf("%d",&init.arr[i][j]);
unit.arr[i][j]=(i==j?:);
}
Matrix res=solve(k);
for(int i=;i<n;i++){
for(int j=;j<n-;j++)
printf("%d ",res.arr[i][j]);
printf("%d\n",res.arr[i][n-]);
}
}
return ;
}
POJ 3233 Matrix Power Series (矩阵乘法)的更多相关文章
- 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 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. 这 ...
- poj 3233 Matrix Power Series 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- 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(矩阵等比求和)
题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...
- 矩阵十点【两】 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 ...
随机推荐
- bzoj 1975 [Sdoi2010]魔法猪学院
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1758 Solved: 557[Submit][Statu ...
- 学习笔记:STL
第一部分:(参考百度百科) 一.STL简介 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Me ...
- hdu1166 敌兵布阵(线段树 求区间和 更新点)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 第二章 eclipse中m2e插件问题
1.当引入一个maven项目到eclipse中时,这时候可能会出现找不到一个插件的问题,例如: <plugin> <groupId>org.apache.maven.plugi ...
- Oracle—RMAN完全恢复
一.RMAN完全恢复的相关概念 1.在RMAN完全恢复中主要使用两个命令,一个是restore,另一个是recover. 2.可以在三个级别恢复,数据库,表空间,数据文件. 3.RMAN中应对于各种情 ...
- 直播【95秀】JNI 基本实现 简洁
2017-2-8 基本架构 1.使用SurfaceView在UI层面展示视频 2.通过JNI调用C代码控制视频的播放.停止 基本功能 1.从服务器获取正在直播的主播的列表信息 2.进入直播界面 3.可 ...
- QML 与 C++ 交互之工厂方法
QML 与 C++ 交互之工厂方法 先看例如以下的类声明,声明了一个产品类和工厂类. #include <QObject> class Productor : public QObject ...
- easyui加入自己定义图标
近期用easyui发现图标挺少的,事实上能够另外加入一个css样式,只是我偷懒,直接在easyui的css里面加入了. 以下是文件夹: icon.css是easyui的默认样式文件.ext_icons ...
- NodeBB,一个基于nodejs的响应式论坛
喜欢方便的同学请绕道去discuz,好吧我是nodejs的重视患者,首先你要有自己的vps或则云空间,比如9cloud,我今天用的是阿里云的VPS. 进入阿里云Ubuntu主机 .... 输入密码进入 ...
- android 实现qq聊天对话界面效果
效果图: chatting_item_from.xml <?xml version="1.0" encoding="UTF-8"?><Line ...