zoj 2671 Cryptography(矩阵相乘+线段树)

本文详细介绍了如何使用C++语言实现矩阵乘法操作,并通过线段树优化来加速计算过程,特别适合处理大规模矩阵乘法问题。通过实例分析,读者可以学习到如何在实际应用中高效解决此类数学难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cryptography

Time Limit: 5 Seconds      Memory Limit: 32768 KB

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 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

InputOutput
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
0 2
0 0

0 2
0 1

0 1
0 0

2 1
1 2


题意:给出n个2*2的矩阵,有m个询问,每个询问i j,要你求出矩阵i*矩阵(i+1)*矩阵(i+2)*....*矩阵j的结果。

思路:直接计算可能超时,可以用线段树保存结果,加快运算。


AC代码:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll __int64
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)

using namespace std;

const int INF = 1000000000;
const int maxn = 30005;
struct Mat
{
    int mat[2][2];
    void zero()
    {
        memset(mat, 0, sizeof(mat));
    }
    void unit()
    {
        zero();
        for(int i = 0; i < maxn; i++) mat[i][i] = 1;
    }
} num[maxn];
struct node
{
    int l, r;
    Mat ans;
} tree[maxn * 4];
int r, n, m;
Mat operator *(const Mat &a, const Mat &b)
{
    Mat tmp;
    for(int i = 0; i < 2; i++)
        for(int j = 0; j < 2; j++)
        {
            int sum = 0;
            for(int k = 0; k < 2; k++)
                sum += a.mat[i][k] * b.mat[k][j];
            tmp.mat[i][j] = sum % r;
        }
    return tmp;
}
void build(int l, int r, int rt)
{
    tree[rt].l = l;
    tree[rt].r = r;
    if(l == r)
    {
        tree[rt].ans = num[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, L(rt));
    build(mid + 1, r, R(rt));
    tree[rt].ans = tree[L(rt)].ans * tree[R(rt)].ans;
}
Mat query(int l, int r, int rt)
{
    if(tree[rt].l == l && tree[rt].r == r) return tree[rt].ans;
    int mid = (tree[rt].l + tree[rt].r) >> 1;
    if(r <= mid) return query(l, r, L(rt));
    else if(l > mid) return query(l, r, R(rt));
    else
    {
        Mat a = query(l, mid, L(rt));
        Mat b = query(mid + 1, r, R(rt));
        return a * b;
    }
}
int main()
{
    int a, b;
    bool flag = false;
    while(~scanf("%d%d%d", &r, &n, &m))
    {
        for(int k = 1; k <= n; k++)
            for(int i = 0; i < 2; i++)
                for(int j = 0; j < 2; j++)
                    scanf("%d", &num[k].mat[i][j]);
        build(1, n, 1);
        while(m--)
        {
            if(flag) puts("");
            else flag = true;
            scanf("%d%d", &a, &b);
            Mat tmp = query(a, b, 1);
            printf("%d %d\n%d %d\n", tmp.mat[0][0], tmp.mat[0][1], tmp.mat[1][0], tmp.mat[1][1]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值