ZOJ - 2671 Cryptography(线段树+求区间矩阵乘积)
题意:已知n个矩阵(下标从1开始),求下标x~y区间矩阵的乘积。最多m次询问,n ( 1 <= n <= 30,000) and m ( 1 <= m <= 30,000)。
分析:
1、矩阵初始化为单位矩阵,因为要做乘积,E*A=A。
2、因为输出矩阵的所有值范围在0~r-1,所以要对r取余。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e4;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
struct Matrix{
int ma[][];
Matrix(){
ma[][] = ma[][] = ;
ma[][] = ma[][] = ;
}
}num[MAXN << ];
int r;
Matrix mul(Matrix a, Matrix b){
Matrix ans;
for(int i = ; i < ; ++i){
for(int j = ; j < ; ++j){
ans.ma[i][j] = ;
for(int k = ; k < ; ++k){
(ans.ma[i][j] += (a.ma[i][k] * b.ma[k][j]) % r) %= r;
}
}
}
return ans;
}
void add_up(int id){
num[id] = mul(num[id << ], num[id << | ]);
}
void add(int l, int r, int cur, Matrix x, int id){
if(l == r){
num[id] = x;
return;
}
int mid = (l + r) >> ;
if(cur <= mid){
add(l, mid, cur, x, id << );
}
else add(mid + , r, cur, x, id << | );
add_up(id);
}
Matrix query(int L, int R, int l, int r, int id){
if(l >= L && r <= R){
return num[id];
}
int mid = (l + r) >> ;
Matrix ans;
if(L <= mid){
ans = mul(ans, query(L, R, l, mid, id << ));
}
if(R >= mid + ){
ans = mul(ans, query(L, R, mid + , r, id << | ));
}
return ans;
}
int main(){
int n, m;
bool flag = true;
while(scanf("%d%d%d", &r, &n, &m) == ){
if(flag) flag = false;
else printf("\n");
for(int i = ; i <= n; ++i){
Matrix tmp;
for(int j = ; j < ; ++j){
for(int k = ; k < ; ++k){
scanf("%d", &tmp.ma[j][k]);
}
}
add(, n, i, tmp, );
}
while(m--){
int x, y;
scanf("%d%d", &x, &y);
Matrix t = query(x, y, , n, );
printf("%d %d\n", t.ma[][], t.ma[][]);
printf("%d %d\n", t.ma[][], t.ma[][]);
if(m) printf("\n");
}
}
return ;
}
ZOJ - 2671 Cryptography(线段树+求区间矩阵乘积)的更多相关文章
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- hdu 1754 I Hate It (线段树求区间最值)
HDU1754 I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- 【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps
https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意 ...
- poj 3264 线段树 求区间最大最小值
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- BZOJ 4127: Abs (树链剖分 线段树求区间绝对值之和 带区间加法)
题意 给定一棵树,设计数据结构支持以下操作 1 u v d 表示将路径 (u,v) 加d(d>=0) 2 u v 表示询问路径 (u,v) 上点权绝对值的和 分析 绝对值之和不好处理,那么我们开 ...
- HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. 1e5个点,问 ...
- hdu1166 敌兵布阵(线段树 求区间和 更新点)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ3264(线段树求区间最大最小值)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41162 Accepted: 19327 ...
随机推荐
- windows远程桌面不显示本地磁盘
\\tsclient\D 在资源管理器输入上面的内容就可以访问本地的D盘,但是前提是连接远程桌面的时候设置了可以访问本地D盘.
- js动态添加li,你添加的li具有你绑定的事件
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <%@page i ...
- ThinkPHP 3.2 生成静态页面
1:在根目录下的全局index.php中加下面这行: define('HTML_PATH', './htm');//生成静态页面的文件位置 2:在项目的配置文件config.php中加下面这行: 'H ...
- css - flex 定义排列方向
flex-direction定义伸缩项目放置在伸缩容器的排列方向,对应有四个值: (1)row:从左到右或从右到左 (2)row-reverse:与row属性相反 (3)column:从上到下排列 ( ...
- C语言中指针和取地址符&的关系
一 概念定义: 严格说起来,应该这么讲:指针存的是地址,而&运算符是取变量的地址. 指针原理: 其实计算机中的内存就像一个个抽屉(一兆内存就相当于1百万个抽屉),每个抽屉都有一个编号,用于存放 ...
- 8个最常用的Python内置函数,小白必备!
题图:Photo by Markus Spiske on Unsplash Python给我们内置了大量功能函数,官方文档上列出了69个,有些是我们是平时开发中经常遇到的,也有一些函数很少被用到,这里 ...
- 6.安装telnet服务
yum list telnet* 列出telnet相关的安装包 yum install telnet-server 安装telnet服务 yum install telnet.* 安装telnet客户 ...
- SwiftUI中多设备运行方法
https://blog.csdn.net/weixin_42679753/article/details/94465674 https://www.jianshu.com/p/17fc7929fcb ...
- List<string>绑定到DataGridView控件
问题 将一个简单的List<string>作为数据源绑定到 DataGridView myDataGridView.DataSource = myStringList; 但是只得到一个名为 ...
- JS中的Map对象
1,js创建map对象 var map = new Map(); 2.将键值对放入map对象 map.set("key",value) map.set("key1&quo ...