HDU 5671 矩阵

Matrix

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 271    Accepted Submission(s): 126

Problem Description

There is a matrix M

that has n

rows and m

columns (1≤n≤1000,1≤m≤1000)

.Then we perform q(1≤q≤100,000)

operations:

1 x y: Swap row x and row y (1≤x,y≤n)

;

2 x y: Swap column x and column y (1≤x,y≤m)

;

3 x y: Add y to all elements in row x (1≤x≤n,1≤y≤10,000)

;

4 x y: Add y to all elements in column x (1≤x≤m,1≤y≤10,000)

;

Input

There are multiple test cases. The first line of input contains an integer T(1≤T≤20)

indicating the number of test cases. For each test case:

The first line contains three integers n

, m

and q

.
The following n

lines describe the matrix M.(1≤Mi,j≤10,000)

for all (1≤i≤n,1≤j≤m)

.
The following q

lines contains three integers a(1≤a≤4)

, x

and y

.

Output

For each test case, output the matrix M

after all q

operations.

Sample Input

2

3 42

1 2 3 4

2 3 4 5

3 4 5 6

1 1 2

3 1 10

2 2 2

1 10

10 1

1 1

2
2

1 2

Sample Output

12 13 14 15

1 2 3 4

3 4 5 6

1 10

10 1

Hint

Recommand to use scanf and printf

Source

BestCoder Round #81 (div.2)

题意:对矩阵执行q次  4种类型的操作 输出 最终矩阵

题解:

对于交换行、交换列的操作,分别记录当前状态下每一行、每一列是原始数组的哪一行、哪一列即可。

对每一行、每一列加一个数的操作,也可以两个数组分别记录。注意当交换行、列的同时,也要交换增量数组。

输出时通过索引找到原矩阵中的值,再加上行、列的增量

 1  #include<iostream>
 2  #include<cstring>
 3  #include<cstdio>
 4  #include<queue>
 5  #include<stack>
 6  #include<map>
 7  #include<set>
 8  #include<algorithm>
 9  #define LL __int64
10  #define pi acos(-1.0)
11  #define mod 1
12  #define maxn 10000
13  using namespace std;
14 int t;
15 int mp[1005][1005] ;
16 int n,m,q;
17 int a,x,y;
18 int l[1005],h[1005];
19 int ladd[1005],hadd[1005];
20 int main()
21 {
22     scanf("%d",&t);
23     for(int i=1;i<=t;i++)
24     {
25         scanf("%d %d %d",&n,&m,&q);
26         for(int j=1;j<=n;j++)
27          for(int k=1;k<=m;k++)
28           scanf("%d",&mp[j][k]);
29           for(int j=1;j<=n;j++)
30           {
31           h[j]=j;hadd[j]=j;
32           }
33           for(int j=1;j<=m;j++)
34           {
35           l[j]=j; ladd[j]=0;
36           }
37           memset(hadd,0,sizeof(hadd));
38           memset(ladd,0,sizeof(ladd));
39           int t;
40           for(int j=1;j<=q;j++)
41           {
42                scanf("%d %d %d",&a,&x,&y);
43                if(a==1)
44                {
45                    t=h[y];
46                 h[y]=h[x];
47                    h[x]=t;
48              }
49                else
50                if(a==2)
51                {
52                    t=l[y];
53                 l[y]=l[x];
54                    l[x]=t;
55                }
56                else
57                if(a==3)
58                {
59                    hadd[h[x]]+=y;
60               }
61                else
62                   ladd[l[x]]+=y;
63           }
64           for(int j=1;j<=n;j++)
65            {
66                printf("%d",mp[h[j]][l[1]]+hadd[h[j]]+ladd[l[1]]);
67              for(int k=2;k<=m;k++)
68            {
69                    printf(" %d",mp[h[j]][l[k]]+hadd[h[j]]+ladd[l[k]]);
70            }
71            printf("\n");
72            }
73     }
74 return 0;
75 } 
时间: 2024-10-25 15:55:45

HDU 5671 矩阵的相关文章

hdu 4291 矩阵幂 循环节

http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,而且矩阵的更神奇: g(g(g(n))) mod 109 + 7  最外层MOD=1e9+7  可以算出g(g(n))的循环节222222224,进而算出g(n)的循环节183120LL,然后由内而外计算即可 注释掉的是求循环节的代码 //#pragma comment(linker, "/STACK:102400000,102400000")

HDU 4965 矩阵快速幂

顺手写了下矩阵类模板 利用到矩阵乘法的交换律 (A*B)^n == A * (B*A)^n-1 *B #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <utility> #include <stack> #includ

hdu 4965 矩阵快速幂 矩阵相乘性质

Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a gir

hdu 4686 矩阵乘法优化递推关系

这里有一份解题报告 解题报告 这是理论知识: 点我 最主要的是构造乘法矩阵,这个是通过递推关系得到的. 有了它,求数列的第n项可以在log(n)的时间里求出来. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <set> 5 #include <algorithm> 6 #include <map> 7 #include<vect

Hdu 4920矩阵乘法(内存访问的讲究)

题目链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2143    Accepted Submission(s): 967 Problem Description Given two matrices A and B of size n×n, find the product of t

hdu 5671 Matrix(BC——思维题)

题目链接:acm.hdu.edu.cn/showproblem.php?pid=5671 Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 891    Accepted Submission(s): 371 Problem Description There is a matrix M that has n rows

HDU 2276 矩阵快速幂

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2650    Accepted Submission(s): 1393 Problem Description There are n lights in a circle numbered from 1 to n. The left of lig

HDU 5895 矩阵快速幂+高次幂取模

HDU 5895 Mathematician QSC 题意:已知f(n)=2*f(n-1)+f(n-2), g(n)=∑f(i)²(0<=i<=n), 给出n,x,y,s, 求x^(g(n*y))%(s+1); 思路:OEIS查到了g(n)=f(n)*f(n+1)/2, f(n)可以用矩阵快速幂求得, 有一个定理可以用于高次幂取模 x^n %k=x^(n%phi(k)+phi(k)) %k, 此处phi(x)为欧拉函数,但是在对幂次取模时存在一个除2, 又因为(a/b)%k=(a%bk)/b,

HDU 1757 矩阵相乘,快速幂模板题

HDU 1757 题意: If x < 10, f(x) = x; If x >= 10, f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10);  给出k和mod,求f(k). 总结: 1.特别注意,矩阵相乘不满足交换律,即a*b != b*a.  2.感觉推方程有点困难. 3.矩阵初始化注意. f(x-10)   0 0 0 0 0 0 0 0 0        ( first矩阵 )       f(x-9)