Cryptography

Time Limit: 5000ms
Memory Limit: 32768KB

This problem will be judged on ZJU. Original ID: 2671
64-bit integer IO format: %lld      Java class name: Main

 

Young cryptoanalyst Georgie is planning to break the new cipher invented by his friend Andie. To do this, he must make some linear transformations over the ring Zr = Z/rZ.

Each linear transformation is defined by 2×2 matrix. Georgie has a sequence of matrices A1 , A2 ,..., An . As a step of his algorithm he must take some segment Ai , Ai+1 , ..., Aj of the sequence and multiply some vector by a product Pi,j=Ai × Ai+1 × ... × Aj of the segment. He must do it for m various segments.

Help Georgie to determine the products he needs.

Input

There are several test cases in the input. The first line of each case contains r (1 <= r <= 10,000), n (1 <= n <= 30,000) and m (1 <= m <= 30,000). Next n blocks of two lines, containing two integer numbers ranging from 0 to r - 1 each, describe matrices. Blocks are separated with blank lines. They are followed by m pairs of integer numbers ranging from1 to n each that describe segments, products for which are to be calculated.
There is an empty line between cases.

Output

Print m blocks containing two lines each. Each line should contain two integer numbers ranging from 0 to r - 1 and define the corresponding product matrix.
There should be an empty line between cases.

Separate blocks with an empty line.

Sample

Input

3 4 4
0 1
0 0 2 1
1 2 0 0
0 2 1 0
0 2 1 4
2 3
1 3
2 2 Output
0 2
0 0 0 2
0 1 0 1
0 0 2 1
1 2

Source

 
解题:坑爹的线段树
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct Matrix {
int m[][];
};
struct node {
int lt,rt;
Matrix matrix;
};
node tree[maxn<<];
int r,n,m;
Matrix Multiplication(const Matrix &a,const Matrix &b) {
Matrix c;
c.m[][] = (a.m[][]*b.m[][] + a.m[][]*b.m[][])%r;
c.m[][] = (a.m[][]*b.m[][] + a.m[][]*b.m[][])%r;
c.m[][] = (a.m[][]*b.m[][] + a.m[][]*b.m[][])%r;
c.m[][] = (a.m[][]*b.m[][] + a.m[][]*b.m[][])%r;
return c;
}
void read(Matrix &c) {
for(int i = ; i < ; ++i)
for(int j = ; j < ; ++j)
scanf("%d",&c.m[i][j]);
}
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt) {
if(lt <= n) read(tree[v].matrix);
else {
tree[v].matrix.m[][] = ;
tree[v].matrix.m[][] = ;
tree[v].matrix.m[][] = ;
tree[v].matrix.m[][] = ;
}
return;
}
int mid = (lt+rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
tree[v].matrix = Multiplication(tree[v<<].matrix,tree[v<<|].matrix);
}
Matrix query(int lt,int rt,int v){
if(tree[v].lt == lt && tree[v].rt == rt) return tree[v].matrix;
int mid = (tree[v].lt + tree[v].rt)>>;
if(rt <= mid) return query(lt,rt,v<<);
else if(lt > mid) return query(lt,rt,v<<|);
else return Multiplication(query(lt,mid,v<<),query(mid+,rt,v<<|));
}
int main() {
bool cao = false;
while(~scanf("%d %d %d",&r,&n,&m)){
int k = log2(n) + ;
k = <<k;
build(,k,);
if(cao) puts("");
cao = true;
bool flag = false;
while(m--){
int s,t;
if(flag) puts("");
flag = true;
scanf("%d %d",&s,&t);
Matrix ans = query(s,t,);
for(int i = ; i < ; ++i)
printf("%d %d\n",ans.m[i][],ans.m[i][]);
}
}
return ;
}

ZJU 2671 Cryptography的更多相关文章

  1. ZOJ 2671 Cryptography 矩阵乘法+线段树

    B - Cryptography Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Subm ...

  2. ZOJ - 2671 Cryptography(线段树+求区间矩阵乘积)

    题意:已知n个矩阵(下标从1开始),求下标x~y区间矩阵的乘积.最多m次询问,n ( 1 <= n <= 30,000) and m ( 1 <= m <= 30,000). ...

  3. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    .Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...

  4. ECC-Elliptic Curves Cryptography,椭圆曲线密码编码学

    ECC ECC-Elliptic Curves Cryptography,椭圆曲线密码编码学,是目前已知的公钥体制中,对每比特所提供加密强度最高的一种体制.在软件注册保护方面起到很大的作用,一般的序列 ...

  5. "System.Security.Cryptography.CryptographicException: 拒绝访问" 问题的解决方法

    .net web程序使用rsa算法进行加解密时,程序报告“System.Security.Cryptography.CryptographicException: 拒绝访问”错.按网上搜的解决方法做了 ...

  6. System.Security.Cryptography.CryptographicException: 指定了无效的提供程序类型

    这两天在调用银联在线的支付接口,把银联提供的demo代码copy过来放到自己网站上,生成通过了,但是运行的时候就报错了: 指定了无效的提供程序类型. 说明: 执行当前 Web 请求期间,出现未经处理的 ...

  7. Raspberry Pi 3 FAQ --- connect automatically to 'mirrors.zju.edu.cn' when downloading and how to accelerate download

    modify the software source: The software source is a place where several free application for linux ...

  8. [POJ2109]Power of Cryptography

    [POJ2109]Power of Cryptography 试题描述 Current work in cryptography involves (among other things) large ...

  9. ACM: Gym 100935B Weird Cryptography - 简单的字符串处理

    Weird Cryptography Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. Eclipse设置jdk相关

    2.window->preferences->java->Compiler->设置右侧的Compiler compliance level 3.window->prefe ...

  2. [SharePoint][SharePoint2013循序渐进]SPS2013简介

    本章概要: 1.啥是SPS2013 2.SharePoint如何作用于团队协作和信息共享 3.SP2013有哪些用户权限 4.什么是SharePoint2013 online 5.SP在内部署和在线订 ...

  3. HDU 5168

    把边按权值排序后,就相当于求一个子序列以1开始和以n结束.由于边权递增,而且相差>=k,所以,边的顺序也必定是递增的.知道,当处理一条出边时,必定是从入边选择一条最优的边,考虑两个因素,入边的权 ...

  4. HDU 5167

    范围 内的斐波那契的数不多,求出范围内的数,再暴力枚举即可. #include <iostream> #include <cstdio> #include <algori ...

  5. Android 四大组件学习之BroadcastReceiver四

    本节学习系统中特殊的广播接收者. 我们前面几节不是说了,当广播接受者一旦注冊到系统中,当系统发送的广播和你注冊的广播的action匹配时,系统就会启动广播接收者所在的进程.除非用户手动停止广播接收者所 ...

  6. java连接mysql数据库增删改查操作记录

    1. 连接数据库.得到数据库连接变量 注意连接数据库的时候 (1)打开DB Browser 新建一个Database Driver,注意加入Driver JARs的时候加入的包,我的是mysql-co ...

  7. 关于vue 自定义组件的写法与用法

    最近在网上看到很多大神都有写博客的习惯,坚持写博客不但可以为自己的平时的学习做好记录积累 无意之中也学还能帮助到一些其他的朋友所以今天我也注册一个账号记录一下学习的点滴!当然本人能力实在有限写出的文章 ...

  8. 【HDU 2176】 取(m堆)石子游戏

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2176 [算法] Nim博弈 当石子数异或和不为0时,先手必胜,否则先手必败 设石子异或和为S 如果 ...

  9. 更改python字符编码以便使用UTF-8的编码url路径

    url编码分两种, 一种是unicode, 另一种是gb2312, 今天遇到的一个网站是要将字符编码按照gb2312来编码,用来得到一个先填写blanks后再返回页面的数据,废话少说,需要做的就是先查 ...

  10. 关于udebug的使用

    关于udebug的使用 特别强调: 转载于此 当你做国外OJ的题目时,总是不知道有什么问题,用题解打对拍都发现不出来,看到某道可恶美好的英文题目WA掉时,又不能下载样例时,会觉得很无奈吧.我们可以使用 ...