poj3233Matrix Power Series(矩阵乘法)
Matrix Power Series
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 23187 | Accepted: 9662 |
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
/*
矩阵乘法经典+二分
Sk=A+A2+A3+...+Ak
=(1+Ak/2)*(A+A2+A3+...+Ak/2)+{Ak}
=(1+Ak/2)*(Sk/2)+{Ak}
当k为偶数时不要大括号里面的数
*/
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std;
int n,m,k;
struct matrix
{
int a[][];
void init()
{
memset(a,,sizeof a);
for(int i=;i<;i++) a[i][i]=;
}
}; void print(matrix s)
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if (j)
printf(" ");
printf("%d",s.a[i][j]%m);
}
printf("\n");
}
} matrix m_add(matrix a,matrix b)//加法
{
matrix c;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
c.a[i][j]=((a.a[i][j]+b.a[i][j])%m);
return c;
} matrix m_mul(matrix a,matrix b)//乘法
{
matrix c;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
c.a[i][j]=;
for(int k=;k<n;k++)
c.a[i][j]+=((a.a[i][k]*b.a[k][j])%m);
c.a[i][j]%=m;
}
}
return c;
} matrix mul(matrix s,int k)//矩阵快速幂
{
matrix ans;ans.init();
while(k>=)
{
if(k&) ans=m_mul(ans,s);
k>>=;
s=m_mul(s,s);
}
return ans;
} matrix sum(matrix s,int k)//矩阵前k项求和
{
if(k==) return s;
matrix tmp;tmp.init();
tmp=m_add(tmp,mul(s,k>>));//计算1+A^(k/2)
tmp=m_mul(tmp,sum(s,k>>));//计算(1+A^(k/2))*(A+A^2+A^3+...+A^(k/2))
if(k&) tmp=m_add(tmp,mul(s,k));
return tmp;
} int main()
{
while(cin>>n>>k>>m)
{
matrix s;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
scanf("%d",&s.a[i][j]);
s=sum(s,k);
print(s);
}
}
poj3233Matrix Power Series(矩阵乘法)的更多相关文章
- C++-POJ3233-Matrix Power Series[矩阵乘法][快速幂]
构造矩阵 #include <cstdio> ; struct Matrix{int a[MAXN][MAXN];}O,I;int N; ;i<MAXN;i++);j<MAXN ...
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- POJ3233 [C - Matrix Power Series] 矩阵乘法
解题思路 题目里要求\(\sum_{i=1}^kA^i\),我们不妨再加上一个单位矩阵,求\(\sum_{i=0}^kA^i\).然后我们发现这个式子可以写成这样的形式:\(A(A(A...)+E)+ ...
- POJ3233 Matrix Power Series 矩阵乘法
http://poj.org/problem?id=3233 挺有意思的..学习到结构体作为变量的转移, 题意 : 给定矩阵A,求A + A^2 + A^3 + ... + A^k的结果(两个矩阵相加 ...
- POJ3233Matrix Power Series(矩阵快速幂)
题意 题目链接 给出$n \times n$的矩阵$A$,求$\sum_{i = 1}^k A^i $,每个元素对$m$取模 Sol 考虑直接分治 当$k$为奇数时 $\sum_{i = 1}^k A ...
- POJ3233Matrix Power Series(十大矩阵问题之三 + 二分+矩阵快速幂)
http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total ...
- C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速
Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 27277 Accepted: ...
随机推荐
- cookie和sessionStorage 、localStorage 对比
相同点:都存储在客户端 不同点:1.存储大小 cookie数据大小不能超过4k. sessionStorage和localStorage 虽然也有存储大小的限制,但比cookie大得多,可以达到5M或 ...
- C#使用Win32函数的一些类型转换
C#在访问Win 32 Api时需要处理C 结构与C#结构的映射,这在MSDN以及许多Blog上都可以找到参考的资料.Win 32 中有一些定义复杂的Struct,这些结构体拥有长度固定的数组或者一些 ...
- PHP控制反转(IOC)和依赖注入(DI
<?php class A { public $b; public $c; public function A() { //TODO } public function Method() { $ ...
- mysql异地备份方案经验总结
Mysql 数据库异地备份脚本 实验环境:关闭防火墙不然不能授权登录 Mysql-server:192.168.30.25 Mysql-client: 192.168.30.24 实验要求:对mys ...
- uva10082 WERTYU (Uva10082)
A common typing error is to place the hands on the keyboard one row to the right of the correct posi ...
- zabbix4.0搭建(基于CentOS6.8)
环境 服务端:188.188.3.241,系统:centos6.8,mysql:5.7.3,php:5.4.9,nginx:1.12.0 一.nginx编译安装 NGINX_VERSION=1.1 ...
- ionic使用cryptojs加密 复制到黏贴版 使用md5
npm install crypto-js npm install --save @types/crypto-js import * as crypto from "crypto-js&qu ...
- js for循环中的var与let
var a = []; for (var i = 0; i < 10; i++) { a[i] = function () { console.log(i); }; } a[6](); 上面代码 ...
- nyoj_366_D的小L_201403011600
D的小L 时间限制:4000 ms | 内存限制:65535 KB 难度:2 描述 一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给 ...
- .net performance optimize your C# app 读书笔记
目录 序 作者简介 推荐人简介 感谢 本书简介 第一章 性能指标 第二章 性能测量 第三章 内部类型 第四章 垃圾回收机制 第五章 集合和泛型 第六章 并发和并行性 第七章 网络.I / ...