线性代数(矩阵乘法):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 一道比较有意思的水题,水一水更健康!
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int mod,n,K;
struct Matrix{
int mat[maxn][maxn];
Matrix(){
memset(mat,,sizeof(mat));
} Matrix operator +(Matrix a){
Matrix r;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
(r.mat[i][j]=(mat[i][j]+a.mat[i][j])%mod)%=mod;
return r;
} Matrix operator *(Matrix a){
Matrix r;
for(int i=,s;i<=n;i++)
for(int k=;k<=n;k++){
s=mat[i][k];
for(int j=;j<=n;j++)
(r.mat[i][j]+=(s*a.mat[k][j])%mod)%=mod;
}
return r;
} Matrix operator ^(int k){
Matrix r,x;
for(int i=;i<=n;i++)
r.mat[i][i]=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
x.mat[i][j]=mat[i][j];
while(k){
if(k&)
r=r*x;
k>>=;
x=x*x;
}
return r;
}
}A,B,ans;
Matrix Solve(int k){
if(k==)return A;
if(k%)return A+A*Solve(k-);
else return ((A^(k/))+B)*Solve(k/);
}
int main(){
#ifndef ONLINE_JUDGE
//freopen("","r",stdin);
//freopen("","w",stdout);
#endif
scanf("%d%d%d",&n,&K,&mod);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&A.mat[i][j]); for(int i=;i<=n;i++)
B.mat[i][i]=; ans=Solve(K); for(int i=;i<=n;i++){
for(int j=;j<=n;j++)
printf("%d ",ans.mat[i][j]);
printf("\n");
}
return ;
}
线性代数(矩阵乘法):POJ 3233 Matrix Power Series的更多相关文章
- 矩阵十点【两】 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 ...
- 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: 11954 Accepted: ...
- [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15417 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(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
随机推荐
- Android常用组件【转】
UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...
- CTE-递归[2]
在此之前写过一个CTE的递归,取出了所有的子节点,基本上可以满足大多数的需求,这里我们来延伸一下:首先我们回顾下原来的场景 图片的上半部分递归查出某个节点的所有子节点,这个我们已经通过CTE实现了,可 ...
- java中怎么进行字符串替换?
String str = "test.doc"; String newStr = str.replaceAll("doc","html");
- ios8及以前的特性
目前最新系统为ios8.以下为历代系统的回顾: iOS 1 关键词:iPhone的诞生 也许放在现在来看,当时的情景很难想象.当第一代iPhone正式发布时,在某些功能和方面其实是要远远落后于当时的竞 ...
- Spring 中的注解
1.普通方式注解 a.在配置文件中配置 1.导入命名空间 xmlns:context="http://www.springframework.org/schema/ ...
- LA 6856 Circle of digits 解题报告
题目链接 先用后缀数组给串排好序.dc3 O(n) 二分答案+贪心check 答案的长度len=(n+k-1)/k 如果起点为i长为len串大于当前枚举的答案,i的长度取len-1 从起点判断k个串的 ...
- topcoder算法练习3
SRM144 DIV1 1100 point Problem Statement NOTE: There are images in the examples section of this ...
- ThinkPHP框架下,给jq动态添加的标签添加点击事件移除标签
jq移除标签主要就是$("#要移除的id").remove();不再赘述,这里要提醒的是jq中动态添加标签后怎样添加点击事件.一般的jq添加点击事件是用这种方法$("#i ...
- 基于BitNami for XAMPP进行Drupal7安装的教程(Win7平台)
BitNami是一个开源项目,该项目产生的开源软件包可用于安装Web应用程序和解决方案堆栈,以及虚拟设备.BitNami 提供wordpress.joomla.drupal.bbpress等开源程序的 ...
- ACM组队安排
Problem Description ACM亚洲区比赛结束,意味着开始备战明年的浙江省大学生程序设计竞赛了! 杭州电子科技大学ACM集训队也准备开始组队. 教练想把所有的n个队员组成若干支队 ...