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
Input | Output |
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;
}