Matrix Recurrence
给定矩阵$A,B$,且有
$$
f(0) = A ,f(i) =B * \prod_{i=w(i)}^{i-1}f(i)
$$
求f(n)
其中,当w(i)单增时,可以做到$O(n*m^3)$,注意要优化取模运算。
对于加入的f(i),我们压入栈中,维护栈的 元素积。
同时维护栈之前的一段元素的后缀积,当w(i)超过非栈元素的右边界时,将栈内元素暴力化为后缀积。
#include <iostream>
#include <cstdio>
#include <cstring> #define LL long long
#define N 1000010 using namespace std; int P; int m,n; struct MA
{
LL a[][];
void scan()
{
for(int i=,j;i<m;i++)
for(j=;j<m;j++) scanf("%lld",&a[i][j]);
}
void init()
{
memset(a,,sizeof(a));
for(int i=;i<m;i++) a[i][i]=;
}
void print()
{
for(int i=;i<m;i++)
{
for(int j=;j<m;j++) printf("%lld ",a[i][j]);
printf("\n");
}
}
}A0,B; MA sta[N];
MA pre[N];
MA sumv,A;
int c[N],tot,r; MA mul(MA x,MA y)
{
MA ans;
for(int i=,j,k;i<m;i++)
for(j=;j<m;j++)
{
ans.a[i][j]=;
for(k=;k<m;k++)
ans.a[i][j] += x.a[i][k]*y.a[k][j];
}
for(int i=,j;i<m;i++)
for(j=;j<m;j++) ans.a[i][j]%=P;
return ans;
} void build()
{
int tmp=r;
for(int i=;i<=tot;i++) pre[++r]=sta[i];
tot=;
for(int i=r-;i>=tmp+;i--) pre[i]=mul(pre[i], pre[i+]);
sumv.init();
} int main()
{
while(~scanf("%d%d%d",&n,&m,&P))
{
A0.scan();
B.scan();
for(int i=;i<=n;i++) scanf("%d",&c[i]);
for(int i=;i<=n;i++) pre[i].init();
r=;
tot=;
pre[]=A0;
sumv.init();
for(int i=;i<=n;i++)
{
if(c[i]>r) build();
A=mul(pre[c[i]],sumv);
A=mul(A,B);
sta[++tot]=A;
sumv=mul(sumv,A);
}
A.print();
}
return ;
}
。
当w(i)不单增时,我们可以维护$8$个长度为$6,6^2,6^3...6^8$的队列,每一次将新加入的元素先压入长度为$6$的队列,并$O(m^3*6)$维护后缀积,当队列满了之后,将其作为一个元素加入$6^2$的队列,同时维护至多$6$个元素的后缀积,当$6^2$满了之后$O(m^3*6^2)$ 暴力将其变为一个元素(算出$6^2$个元素的后缀积),并作为整体压入下一序列。
每个元素最多被更新了8次,所以 $O(8*n*m^3)$
Matrix Recurrence的更多相关文章
- Searching a 2D Sorted Matrix Part I
Write an efficient algorithm that searches for a value in an n x m table (two-dimensional array). Th ...
- fzu 1911 C. Construct a Matrix
C. Construct a Matrix Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB Special Judge ...
- Construct a Matrix (矩阵快速幂+构造)
There is a set of matrixes that are constructed subject to the following constraints: 1. The matrix ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
随机推荐
- [oracle]pl/sql --分页过程demo
这句sql能够用来查询一张表中的特定位置的记录 --查询的方法获取分页的语句 select *from (select t1.*,rownum rn from (select *from books) ...
- C# 比较两个数组中的内容是否相同的算法
这里要比较的是两个数组中的内容是否相同,以int数组为例 int[] Arraya=new[] {1,2,3,4,5} int[] Arrayb=new[] {5,3,2,1,4} 以上两个数组内的值 ...
- 01 json方式封装通信接口
新建一个json_api.php<?php class Response{ /** *按json方式输出通信 *@param integet $code 状态码 *@param string $ ...
- 协议分析之qq协议---qq登录
QQ 协议分析:获取各类登录会话密钥 我们知道QQ的一些会话密钥是在登录过程中生成的,尤其是Session Key,有了它便可以解密出聊天文本内容.本文主要是了解一下QQ的加密机制,首先是用嗅探工具W ...
- duplicate symbols for architeture arm64 linker command failed with code 1(use-c to see invocation)
duplicate symbols for architeture arm64 linker command failed with code 1(use-c to see invocation) ...
- CMD命令操作
win + R 然后输入 cmd 打开命令窗口 或者开始-->运行 打开 1.进入 D盘 输 d: 回车2.进入D盘 子目录 输入 cd d:\test 回车 3. dir 回车 进入文件目录 ...
- ickeck插件
地址:http://www.bootcss.com/p/icheck/#skins 使用 1. 先引入文件 css <link rel="stylesheet" type=& ...
- 虚拟机linux安装mysql
安装mysql时需要的全套安装包 mysql-5.1.73-3.el6_5.i686.rpm mysql-libs-5.1.73-3.el6_5.i686.rpm mysql-server-5.1.7 ...
- STM32 ~ 外扩SRAM
字节控制功能.支持高/低字节控制. 看看实现 IS62WV51216 的访问,需要对 FSMC进行哪些配置. 这里就做一个概括性的讲解.步骤如下: 1)使能 FSMC 时钟,并配置 FSMC 相关的 ...
- Codeforces 724C Ray Tracing 扩展欧几里得
吐槽:在比赛的时候,压根就没想到这题还可以对称: 题解:http://blog.csdn.net/danliwoo/article/details/52761839 比较详细: #include< ...