Cryptography
Problem Description
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 A1A_1A1 , A2A_2A2 ,…, AnA_nAn . As a step of his algorithm he must take some segment Ai,Ai+1,...,AjA_i , A_{i+1} , ..., A_jAi,Ai+1,...,Aj of the sequence and multiply some vector by a product Pi,j=Ai×Ai+1×...×AjP_{i,j}=A_i × A_{i+1} × ... × A_jPi,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 from 1 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
Sample Output
0 2
0 0
0 2
0 1
0 1
0 0
2 1
1 2
题意
n个2*2的矩阵,有m个询问,每次询问[l,r][l,r][l,r]区间内矩阵的乘积。
题解:
因为矩阵相乘是满足结合律的,所以线段树维护矩阵乘积就行。或者类似ST表一样维护矩阵乘积也行。
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 30010;
const int mod = 1000000007;
struct matrix{
int a[2][2];
}p[maxn*4], b[maxn];
int modd;
matrix mul(matrix a, matrix b);
void creat(int l, int r, int k);
matrix query(int l, int r, int al, int ar, int k);
int main()
{
int n, m, r, i, j, k, l, num = 0;
while(~scanf("%d %d %d", &modd, &n, &m))
{
if(num)printf("\n");
num++;
for(i=1;i<=n;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
scanf("%d", &b[i].a[j][k]);
creat(1, n, 1);
while(m--)
{
scanf("%d %d", &l, &r);
matrix x = query(1, n, l, r, 1);
printf("%d %d\n%d %d\n", x.a[0][0], x.a[0][1], x.a[1][0], x.a[1][1]);
if(m)printf("\n");
}
}
return 0;
}
void creat(int l, int r, int k)
{
if(l == r){
p[k] = b[l];
return ;
}
int mid = (l+r)/2;
creat(l, mid, 2*k);
creat(mid+1, r, 2*k+1);
p[k] = mul(p[2*k], p[2*k+1]);
}
matrix query(int l, int r, int al, int ar, int k)
{
if(l == al && r == ar){
return p[k];
}
int mid = (l+r)/2;
if(ar <= mid)return query(l, mid, al, ar, 2*k);
else if(al>mid)return query(mid+1, r, al, ar, 2*k+1);
else return mul(query(l, mid, al, mid, 2*k), query(mid+1, r, mid+1, ar, 2*k+1));
}
matrix mul(matrix a, matrix b)
{
matrix c;
memset(c.a, 0, sizeof(c.a));
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
c.a[i][j] = (c.a[i][j]+ a.a[i][k]*b.a[k][j]%modd)%modd;
return c;
}
本文介绍了一种使用线段树解决矩阵乘积问题的方法,适用于处理大量区间矩阵乘积的查询,尤其在矩阵相乘满足结合律的情况下,能够高效地进行区间矩阵乘积的维护与查询。
824

被折叠的 条评论
为什么被折叠?



