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
 
题意:给你两个矩阵,让你把它们相乘后输出%3后的结果。
思路:普通的矩阵乘法+优化能过的,还有一种思路是开bitset<1000>bt[3],ct[3],储存每一行mod3后为0,1,2的情况,那么1*1=1,1 *2=2,2*1=2,2*2=1.
代码一:普通矩阵乘法+优化
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define MOD 3
int n,m;
int data[4][805][805]; void solve()
{
int i,j,k;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
data[3][i][j]=0;
}
}
for(i=0;i<n;i++){
for(k=0;k<m;k++){
if(data[1][i][k]>0){
for(j=0;j<m;j++){
data[3][i][j]=data[3][i][j]+data[1][i][k]*data[2][k][j];
}
}
}
}
} int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
m=n;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&data[1][i][j]);
data[1][i][j]%=3;
}
} for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&data[2][i][j]);
data[2][i][j]%=3;
}
} solve();
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(j<n-1)printf("%d ",data[3][i][j]%3);
else printf("%d\n",data[3][i][j]%3);
}
}
}
return 0;
}

代码二:用bitset

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define MOD 3
#define maxn 805
bitset<1000>bt[maxn][3],ct[maxn][3]; int main()
{
int n,m,i,j,c;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++){
for(j=0;j<3;j++){
bt[i][j].reset();
ct[i][j].reset();
}
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&c);
bt[i][c%3].set(j);
}
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&c);
ct[j][c%3].set(i);
}
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
c=((bt[i][1]&ct[j][1]).count()+(bt[i][1]&ct[j][2]).count()*2+(bt[i][2]&ct[j][1]).count()*2+(bt[i][2]&ct[j][2]).count() )%3;
if(j==n)printf("%d\n",c);
else printf("%d ",c);
}
}
}
return 0;
}

hdu4920Matrix multiplication (矩阵,bitset)的更多相关文章

  1. HDU 4920 Matrix multiplication(bitset优化)

    题目链接 Matrix multiplication 求矩阵A和B相乘的结果. 因为答案只要对3取模,所以我们可以通过一些方法来加速计算. 我们对两个矩阵各开两个bitset,分别存储模3余1和模3余 ...

  2. HDU 4920 Matrix multiplication(bitset)

    HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑.结果就过了.然后看了官方题解,上面是用 ...

  3. UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)

    意甲冠军  由于矩阵乘法计算链表达的数量,需要的计算  后的电流等于行的矩阵的矩阵的列数  他们乘足够的人才  非法输出error 输入是严格合法的  即使仅仅有两个相乘也会用括号括起来  并且括号中 ...

  4. Codeforces 781D Axel and Marston in Bitland 矩阵 bitset

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF781D.html 题目传送门 - CF781D 题意 有一个 n 个点的图,有 m 条有向边,边有两种类型: ...

  5. HDU 4920 Matrix multiplication(矩阵相乘)

    各种TEL,233啊.没想到是处理掉0的情况就能够过啊.一直以为会有极端数据.没想到居然是这种啊..在网上看到了一个AC的奇妙的代码,经典的矩阵乘法,仅仅只是把最内层的枚举,移到外面就过了啊...有点 ...

  6. Poj 3318 Matrix Multiplication( 矩阵压缩)

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18928   Accepted: ...

  7. HDU 4920 Matrix multiplication 矩阵相乘。稀疏矩阵

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  8. POJ 3318 Matrix Multiplication(矩阵乘法)

    题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...

  9. UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)

    栈的练习,如此水题竟然做了两个小时... 题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量. 我的算法很简单:比如(((((DE)F)G)H)I),遇到 (就cnt累计加一,字母入栈,遇到) ...

随机推荐

  1. 【MySQL 高级】索引优化分析

    MySQL高级 索引优化分析 SQL 的效率问题 出现性能下降,SQL 执行慢,执行时间长,等待时间长等情况,可能的原因有: 查询语句写的不好 索引失效 单值索引:在 user 表中给 name 属性 ...

  2. Python eval 函数用途

    Python eval 函数用途: eval 函数可将字符串转换成列表,元组和字典 实例如下: 可以把list,tuple,dict和string相互转化. ##################### ...

  3. oracle创建恢复编录(recovery catalog)

    1.在要作为恢复编录的数据库创建用户 create user rman identified by oracle default tablespace system temporary TABLESP ...

  4. 腾讯云COS对象存储占据数据容灾C位

    说到公有云容灾,大家首先想到的是云上数据备份. 然而,随着企业核心业务逐渐从线下迁移到云上,客户提出了更高的要求.如何确保云上业务的高可用.数据的高可靠,这对云厂商提出了新的挑战. 腾讯云作为全球领先 ...

  5. 容器编排系统K8s之包管理器Helm基础使用

    前文我们了解了k8s上的hpa资源的使用,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14293237.html:今天我们来聊一下k8s包管理器helm的相 ...

  6. Ubuntu14.04系统安装

    1. 使用U盘或光盘进行引导进入系统安装向导. 2. 安装类型选择,选择中文(简体).然后点安装ubuntu. 3. 安装ubuntu电脑必须接入外网(外网的方式有自动获取或手动编辑IP地址). 网络 ...

  7. 解读腾讯敏捷研发核心驱动力 腾讯TAPD TAPD 2020-12-17

    解读腾讯敏捷研发核心驱动力 腾讯TAPD TAPD 2020-12-17

  8. 13 | 实战:单机如何实现管理百万主机的心跳服务? https://time.geekbang.org/column/article/240656

    13 | 实战:单机如何实现管理百万主机的心跳服务? https://time.geekbang.org/column/article/240656

  9. aio 系列函数是由 POSIX 定义的异步操作接口,可惜的是,Linux 下的 aio 操作,不是真正的操作系统级别支持的,它只是由 GNU libc 库函数在用户空间借由 pthread 方式实现的,而且仅仅针对磁盘类 I/O,套接字 I/O 不支持。

    30 | 真正的大杀器:异步I/O探索 https://time.geekbang.org/column/article/150780

  10. 用Jenkins构建Django持续集成环境

    用Jenkins构建Django持续集成环境 - V2EX https://www.v2ex.com/t/32054