hdu4920 Matrix multiplication 模3矩阵乘法

hdu4920

Matrix multiplication

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 568    Accepted Submission(s): 225

Problem Description

Given two matrices A and B of size n×n, find the product of them.
bobo hates big integers. So you are only asked to find the result modulo 3.

Input

The input consists of several tests. For each tests:
The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).

Output

For each tests:
Print n lines. Each of them contain n integers -- the matrix A×B in similar format.

Sample Input

1
0
1
2
0 1
2 3
4 5
6 7

Sample Output

0
0 1
2 1

Author

Xiaoxu Guo (ftiasch)

Source

2014 Multi-University Training Contest 5

Recommend

We have carefully selected several similar problems for you:  4919 4918 4917 4916 4914

2014多校5的最水的题,我没做出来,怕了。

题意:给出两个n*n的矩阵A和B,求A*B结果矩阵,元素都模3,n<=800。

题解:矩阵乘法加剪枝(?)。

800*800的矩阵,多组数据,直接算是会超时得飞起来的,只有考虑模3的特殊性。

读入后每个元素模3,得到的矩阵里全是0,1,2,随机数据的话有三分之一是零,所以我们的矩阵乘法要用k i j的循环嵌套顺序,第二层里面发现A[i][k]==0时就continue,直接少一维,也就是1/3概率少一维。这个是这题最关键的一步,没想到这步的话,其他再怎么优化也没用(我们试过了……)。

另外运算时可以使用cal[i][j][k]提前计算好((i*j)+k)%3,矩阵乘法的时候直接用这个结果。

------------------------------其他------------------------------------

顺便说个笑话:为什么Dijkstra没发明Floyd算法?因为他是ijk不是kij……

比赛的时候我们做这题优化得飞起来,读入输出优化,gets按字符串读一行来处理,输出用putchar,乘法、取余运算用上面说的那步省掉,输出不用‘0‘+C[i][j]而用数组存好ch[0]=‘0‘这样输出,就差用fread一次读多行了……可是就是TLE,因为我们没想到最关键那步……

代码:

 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<cmath>
 9 #include<map>
10 #include<set>
11 #include<stack>
12 #include<queue>
13 using namespace std;
14 #define ll __int64
15 #define usint unsigned int
16 #define mz(array) memset(array, 0, sizeof(array))
17 #define minf(array) memset(array, 0x3f, sizeof(array))
18 #define REP(i,n) for(int i=0;i<(n);i++)
19 #define FOR(i,x,n) for(int i=(x);i<=(n);i++)
20 #define RD(x) scanf("%d",&x)
21 #define RD2(x,y) scanf("%d%d",&x,&y)
22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
23 #define WN(x) printf("%d\n",x);
24 #define RE  freopen("D.in","r",stdin)
25 #define WE  freopen("1.out","w",stdout)
26 int n;
27
28 const int maxn=800;
29 int A[maxn][maxn],B[maxn][maxn],C[maxn][maxn];
30 int cal[3][3][3];
31 int main() {
32     int i,j,k;
33     for(i=0; i<3; i++)
34         for(j=0; j<3; j++)
35             for(k=0; k<3; k++)
36                 cal[i][j][k]=((i*j)+k)%3;
37     while(scanf("%d",&n)!=EOF) {
38         for(i=0; i<n; i++)
39             for(j=0; j<n; j++) {
40                 scanf("%d",&A[i][j]);
41                 A[i][j]%=3;
42             }
43         for(i=0; i<n; i++)
44             for(j=0; j<n; j++) {
45                 scanf("%d",&B[i][j]);
46                 B[i][j]%=3;
47             }
48         mz(C);
49         for(k=0; k<n; k++)
50             for(i=0; i<n; i++) {
51                 if(A[i][k]==0)continue;///这个是关键!模3有三分之一是零,也就有三分之一概率去一维,碉!
52                 for(j=0; j<n; j++) {
53                     ///re.a[i][j]=(re.a[i][j]+(A.a[i][k]*B.a[k][j]));
54                     C[i][j]=cal[A[i][k]][B[k][j]][C[i][j]];
55                 }
56             }
57         for(i=0; i<n; i++) {
58             for(j=0; j<n-1; j++) {

59                 putchar(C[i][j]+‘0‘);
60                 putchar(‘ ‘);
61             }
62             putchar(C[i][n-1]+‘0‘);
63             puts("");
64         }
65     }
66     return 0;
67 }

hdu4920 Matrix multiplication 模3矩阵乘法

时间: 2024-10-06 04:20:09

hdu4920 Matrix multiplication 模3矩阵乘法的相关文章

HDU4920 Matrix multiplication 矩阵

不要问我 为什么过了 窝也不造为什么就过了 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include<iostream> #inclu

POJ 3233 Matrix Power Series 二分+矩阵乘法

链接:http://poj.org/problem?id=3233 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思路:非常明显直接用矩阵高速幂暴力求和的方法复杂度O(klogk).肯定会超时.我採用的是二分的方法, A + A^2 + A^3 + - + A^k=(1+A^(k/2)) *(A + A^2 + A^3 + - + A^(k/2)).这样就能够提出一个(1+A^(k/2)),假设k是奇数,单独处理A^k.

【bitset】hdu4920 Matrix multiplication

先把两个矩阵全都mod3. S[i][j][k]表示第i(0/1)个矩阵的行/列的第k位是不是j(1/2). 然后如果某两个矩乘对应位上为1.1,乘出来是1: 1.2:2: 2.1:2: 2.2:1. 然后分这四种情况把bitset and 起来,然后用count()数一下个数,计算下对答案矩阵该位置的贡献即可. #include<cstdio> #include<bitset> using namespace std; #define N 801 int n,x; bitset&

矩阵乘法 --- hdu 4920 : Matrix multiplication

Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 820    Accepted Submission(s): 328 Problem Description Given two matrices A and B of size n×n, find the product of them. b

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr

2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)

题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据是不可能会有的,而我们一开始一直在想极端数据能接受的方法......后来看了鹏哥的做法,就是把是0的地方都跳过就可以了,用矩阵保存前一个非0数的位置是多少.二师兄给我看了一个代码,人家根本没用别的优化,直接将最里层k的循环提到了最外层,然后就AC了,对此我表示无语. 1 #include <cstd

POJ 3318 Matrix Multiplication(矩阵乘法)

题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. 1 //3318 2 #include <stdio.h> 3 #include <string.h> 4 #include <time.h> 5 #include <stdlib.h> 6 #include <iostream> 7 8 using nam

hdu 4920 Matrix multiplication (矩阵乘法)

Problem Description Given two matrices A and B of size n×n, find the product of them.bobo hates big integers. So you are only asked to find the result modulo 3. Input The input consists of several tests. For each tests:The first line contains n (1≤n≤

ACDream 1213 Matrix Multiplication (01矩阵处理)

Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of