ZOJ_2671 Cryptography(数据结构)

Cryptography

Time Limit: 5000 ms
Memory Limit: 32768 KB
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 A 1 A_1 A1 , A 2 A_2 A2 ,…, A n A_n An . As a step of his algorithm he must take some segment A i , A i + 1 , . . . , A j A_i , A_{i+1} , ..., A_j Ai,Ai+1,...,Aj of the sequence and multiply some vector by a product P i , j = A i × A i + 1 × . . . × A j P_{i,j}=A_i × A_{i+1} × ... × A_j 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 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值