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. chrome安装插件,安装Postman

    1.下载postman插件,可以自己到网上下载,也可以点击http://download.csdn.net/detail/u010246789/9528471 2.解压文件,在解压后的文件夹中找到.c ...

  2. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  3. 关于excel导入、导出(POI)

    当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际的开发中, ...

  4. POJ 3613

    可以利用DP的思想来做,不过是在DP时加上了矩阵乘法的思想而已,但乘法不是真的乘法,而是mp[a][i]+mp[i][b]<mp[a][b]则更新,其实更像FLOYD. 但这是符合乘法的格式的. ...

  5. VC 获取控制台窗体的句柄(hWnd)

    在Windows中,句柄是一个系统内部数据结构的引用. 比如当你操作一个窗体.或说是一个Delphi窗体时,系统会给你一个该窗体的句柄,系统会通知你:你正在操作142号窗体.就此你的应用程序就能要求系 ...

  6. Tom和Jerry来了,Tom和Jerry走了——北漂18年(38)

    上次讲到跟我同一时候入职的女销售走了. 回忆起来,她的问题多半是技巧足够,脑子不足够,走了之后再没联系.不久之后,在老板的要求之下.LilyG又招聘了两位男销售,英文名字非常登对一个叫Tom,一个叫J ...

  7. ASP.NET—011:JavaScript报错常见问题

    相信大家都写过JavaScript.JS由于语法以及自己须要实现的业务的原因,可能在一个页面上要写长篇大论.或者单独写js文件写了好几百K.JS不可否认给Web编程带了很多的方便. 可是假设JS发生了 ...

  8. leetCode(32):Power of Two

    Given an integer, write a function to determine if it is a power of two. 2的幂的二进制表示中,必定仅仅有一个"1&q ...

  9. jsp不通过form和Ajax提交

    在页面里面我们一般都通过form表单和Ajax向后台提交请求,但是我如今页面没有form表单,也不想通过ajax异步提交. 解决方式例如以下:location.href="${rootPat ...

  10. 2015.05.18,外语,学习笔记-《Word Power Made Easy》 03 “如何谈论不同从业者”

    Prefix Person,nous,etc. Practice,etc. Adjective psyche 精神 psychic ['saikik] adj.精神的n.灵媒 -logos 科研 ps ...