Matrix Power Series
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 27277   Accepted: 11143

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

POJ Monthly--2007.06.03, Huang, Jinsong
 
题意: 这题就是求一个矩阵的和式:S(k),直接对和式建立递推:

A^i是一个矩阵

很显然,把每个A^i算出来是不行的,所以我们得找找关系

因为这里牵扯到了矩阵相加求和,所以我们可以想到构建一个包含A的矩阵(只要包含A和两个一就行,这样是为了最后能得到A^1+A^2+...+A^K的式子)

其中1是单位矩阵,单位矩阵是左对角线为1的矩阵

然后容易得到:

可以看出这个分块矩阵的左下角那块就可以得到要求的解S

我们取这一块,再减去一个单位矩阵1即可。

参考博客:https://www.cnblogs.com/pdev/p/4063669.html

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 110;
const int mod = 2;
typedef long long ll;
struct matrix {
ll a[maxn][maxn];
};
matrix base, ans;
ll n, t, m;
matrix multip( matrix x, matrix y ) {
matrix tmp;
for( ll i = 0; i < 2*n; i ++ ) {
for( ll j = 0; j < 2*n; j ++ ) {
tmp.a[i][j] = 0;
for( ll k = 0; k < 2*n; k ++ ) {
tmp.a[i][j] = ( tmp.a[i][j] + x.a[i][k] * y.a[k][j] ) % m;
}
}
}
return tmp;
}
void f( ll x ) {
while( x ) {
if( x&1 ) {
ans = multip( ans, base );
}
base = multip( base, base );
x /= 2;
}
}
int main() {
while( cin >> n >> t >> m ) {
memset( ans.a, 0, sizeof(ans.a) );
memset( base.a, 0, sizeof(base.a) );
for( ll i = 0; i < n; i ++ ) {
for( ll j = 0; j < n; j ++ ) {
cin >> base.a[i][j];
}
}
for( ll i = n; i < 2*n; i ++ ) { //两个单位矩阵
base.a[i][i-n] = base.a[i][i] = 1;
}
//上面两个for循环是为了构建出新的包含A的矩阵
for( ll i = 0; i < 2*n; i ++ ) {
ans.a[i][i] = 1;
}
f(t+1); //由上面举的例子可以看出要求出n次方,得算n+1次
for( ll i = n; i < 2*n; i ++ ) {
for( ll j = 0; j < n; j ++ ) {
if( i == j+n ) {
ans.a[i][j] --;
}
if( j != n-1 ) {
cout << ( ans.a[i][j] + m ) % m << " ";
} else {
cout << ( ans.a[i][j] + m ) % m << endl;
}
}
}
}
return 0;
}

  

POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵的更多相关文章

  1. POJ3233 Matrix Power Series(快速幂求等比矩阵和)

    题面 \(solution:\) 首先,如果题目只要我们求\(A^K\) 那这一题我们可以直接模版矩乘快速幂来做,但是它现在让我们求$\sum_{i=1}^{k}{(A^i)} $ 所以我们思考一下这 ...

  2. [POJ3233]Matrix Power Series 分治+矩阵

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [POJ3233]Matrix Power Series 分治+矩阵 题目大意 A为n×n(n<= ...

  3. 矩阵乘法&&矩阵快速幂&&最基本的矩阵模型——斐波那契数列

    矩阵,一个神奇又令人崩溃的东西,常常用来优化序列递推 在百度百科中,矩阵的定义: 在数学中,矩阵(Matrix)是一个按照长方阵列排列的复数或实数集合 ,最早来自于方程组的系数及常数所构成的方阵.这一 ...

  4. POJ3233:Matrix Power Series(矩阵快速幂+二分)

    http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...

  5. poj3233 Matrix Power Series(矩阵快速幂)

    题目要求的是 A+A2+...+Ak,而不是单个矩阵的幂. 那么可以构造一个分块的辅助矩阵 S,其中 A 为原矩阵,E 为单位矩阵,O 为0矩阵    将 S 取幂,会发现一个特性: Sk +1右上角 ...

  6. POJ-3233 Matrix Power Series 矩阵A^1+A^2+A^3...求和转化

    S(k)=A^1+A^2...+A^k. 保利求解就超时了,我们考虑一下当k为偶数的情况,A^1+A^2+A^3+A^4...+A^k,取其中前一半A^1+A^2...A^k/2,后一半提取公共矩阵A ...

  7. POJ3233]Matrix Power Series && [HDU1588]Gauss Fibonacci

    题目:Matrix Power Series 传送门:http://poj.org/problem?id=3233 分析: 方法一:引用Matrix67大佬的矩阵十题:这道题两次二分,相当经典.首先我 ...

  8. poj3233—Matrix Power Series

    题目链接:http://poj.org/problem?id=3233 题目意思:给一个矩阵n*n的矩阵A和一个k,求一个式子 S = A + A2 + A3 + … + Ak. 这个需要用到等比数列 ...

  9. POJ3233:Matrix Power Series(矩阵快速幂+递推式)

    传送门 题意 给出n,m,k,求 \[\sum_{i=1}^kA^i\] A是矩阵 分析 我们首先会想到等比公式,然后得到这样一个式子: \[\frac{A^{k+1}-E}{A-E}\] 发现要用矩 ...

随机推荐

  1. Is it a full physical image???

    My friend asked me why she could not find some important files in a physical image acquired from an ...

  2. 如何选择合适的SSL证书类型

    网站安装SSL证书就可以将http升级为https加密模式,网站安装SSL证书因此成为一种趋势.如何为网站选择适合的SSL证书类型呢? SSL证书类型可分为2大类:1)按照验证方式分类2)按照支持域名 ...

  3. 【React踩坑记一】React项目中禁用浏览器双击选中文字的功能

    常规项目,我们只需要给标签加一个onselectstart事件,return false就可以 例: <div onselectstart="return false;" & ...

  4. 使用webstorm搭建vue-cli项目

    前言 随着vue在前端不断的壮大,越来越多的前端工程师使用vue了,作为大型项目的开发,vue-cli是不二之选,所以这篇博客是为搭建vue-cli所写,想要搭建vue-cli项目就必须先有git,n ...

  5. HTML/CSS:div水平与元素垂直居中(2)

    单个div水平居中:设置margin的左右边距为自动 div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性,所以再css中设置这两个值不能居中 ...

  6. 重学计算机组成原理(七)- 程序无法同时在Linux和Windows下运行?

    既然程序最终都被变成了一条条机器码去执行,那为什么同一个程序,在同一台计算机上,在Linux下可以运行,而在Windows下却不行呢? 反过来,Windows上的程序在Linux上也是一样不能执行的 ...

  7. wordpress搬家 更换域名

    结论:wordpress网站文件夹是和域名相关联的 wordpress,备份了数据库 然后用另一个新域名新建站,直接从wordpress官网直接下载的网站压缩包,没有用之前的网站文件夹. 然后把原来的 ...

  8. MySQL高可用架构:mysql+keepalived实现

    系统环境及架构 #主机名 系统版本 mysql版本 ip地址 mysqlMaster <a href="https://www.linuxprobe.com/" title= ...

  9. 前端小知识-css3

    一.实现图片倒影 如图: css属性 .style{ -webkit-box-reflect:below 0 linear-gradient(transparent,white 50% ,white) ...

  10. 实现API管理系统的几个重要关键词

    管理API的需求源自于Web API开展业务.从2006年开始,然后逐渐成熟,并在2016年之前进入市场.无论是通过代理现有API的管理网关.本身作为用于部署API本身的网关的一部分,还是作为连接层在 ...