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 ...
随机推荐
- 手把手教你nginx/linux下如何增加网站
先进入到nginx的配置文件目录请输入以下命令 cd /alidata/server/nginx/conf/vhosts 再输入 ll 看看是不是像下面截图的一样 用神器xftp将default. ...
- Django-select_related优化查询
对于一对一字段(OneToOneField)和外键字段(ForeignKey),可以使用select_related 来对QuerySet进行优化. select_related 返回一个QueryS ...
- 将txt文件数据存入excel表格
前言 最近使用Appium自动化在测试设备配网的情况,需要记录每次成功与否和耗时时间. 由于App不是很稳定,执行一段时间会奔溃,因此数据只能通过追加的形式写入到txt文件. 实现过程 存储在txt文 ...
- spring 拦截器简介
spring 拦截器简介 常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等.2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直 ...
- JS 怎么把数组类型的参数传递到后台,后台怎么获取
说明:开发环境 vs2012 asp.net mvc4 c# 1.HTML前端代码 <%@ Page Language="C#" AutoEventWireup=" ...
- Routine Subroutine Coroutine 子程序 协程
https://en.wikipedia.org/wiki/Subroutine In computer programming, a subroutine is a sequence of prog ...
- Windows server 2008 R2 如何启动任务计划程序
使用windows server 2008 R2 的任务计划程序需要启动服务 Task Scheduler 服务, windows server 2008 R2 默认状态下Task Schedule ...
- Mac 操作系统安装 SVN server教程(Subversion With Mac OS X Tutorial)
Find recent articles on my github page: rubyrobot.github.io © 2006-2014 Imagine Ecommerce Subversion ...
- 安装mysql时,提示This application requires .NET framework 4.0问题
Question:双击MySQL的安装文件, 弹框提示, Answer:安装微软的 .NET Framework 4或更新的版本,下载地址:https://www.microsoft.com/zh ...
- JAVA变量初始化赋值null
在Java中,null值表示引用不指向任何对象.运行过程中系统发现使用了这样一个引用时·可以立即停止进一步的访问,不会给系统带来任何危险. 1.如果是对象的field的话那么系统在初始化对象的时候会 ...