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 ...
随机推荐
- 用jquery写循环播放div -2
前面所说的class html元素标签的写法也要有层次性, 这个层次性其实也就是常说的 css类写法要有一个"命名空间, 名字空间" "namespace" 在 ...
- php操作mysql数据库
<span style="font-family:Arial,Helvetica,sans-serif">在php操作数据库过,会频繁对数据库进行各种操作,所以,php ...
- ASP.NET MVC 4 的JS/CSS打包压缩功能-------过滤文件
今天在使用MVC4打包压缩功能@Scripts.Render("~/bundles/jquery") 的时候产生了一些疑惑,问什么在App_Start文件夹下BundleConfi ...
- [百度地图] ZMap 与 MultiZMap 封装类说明;
ZMap.js 与 MultiZMap 说明 1. ZMap 与 MultiZMap 都是封装一些地图常用的使用方法,类方法功能大多使用 prototype 原型 实现: ZMap 在一个页面只能使用 ...
- 【UEditor】 UEditor整合项目上传资源到阿里云服务器
目录 关于此文 下载源码 JSP代码 Java代码 阿里云jar包引入配置 成功啦! 回到顶部 关于此文 项目中要实现编辑器生成带格式的html文档,存入模板,最后生成html的URL,所以选择了UE ...
- iframe框架嵌套技巧(全屏,去双滚动条)
一般情况下我们很少用到iframe(框架),但有些特殊的情况下我们不得不使用iframe,那么或许或遇到嵌套内容不全屏,网页周围有边框,双滚动条等等情况,下面来说一下处理技巧. 全屏与边框处理: &l ...
- 清北暑假模拟day1 艳阳天
/* 注意P有可能不是质数,不要用欧拉函数那一套,正解可以倍增,就是等比数列和的性质,注意n是否为奇数 */ #include <cstdio> #include <algorith ...
- spark
http://www.cnblogs.com/shishanyuan/p/4723604.html?utm_source=tuicool spark presto2.0计算引擎 http://blog ...
- aspcms标签
[newslist:date style=yy-m-d] 日期格式 {aspcms:sitepath}/Templates/{aspcms:defaulttemplate} 幻灯片标签{aspcms: ...
- Qt5.5.1 学习笔记
添加图标(.pro): RC_ICONS = 1.ico RC_FILE = 1.rc 新建 1.rc 内容:IDI_ICON1 ICON "1.ico" 支持c++11(. ...