Matrix multiplication

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

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
题意:给你两个矩阵,求矩阵相乘%3;
思路:正常思路n*n*n求解(运气好可以ac。。。),看了下数据范围由于mod=3,所以利用bitset标记每行1,2,0的位置,取并集就可以得到相同位置的个数;
   这样就可以优化最后的一个n。详见代码;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e3+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
int n;
bitset<N>a[N][],b[N][];
int ans(int i,int j)
{
int u=(a[i][]&b[j][]).count(),v=(a[i][]&b[j][]).count();
int x=(a[i][]&b[j][]).count(),y=(b[j][]&a[i][]).count();
return u+v*+x*+y*;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
for(int j=;j<=;j++)
a[i][j].reset(),b[i][j].reset();
for(int i=; i<n; i++)
for(int j=; j<n; j++)
{
int x;
scanf("%d",&x);
a[i][x%].set(j);
}
for(int i=; i<n; i++)
for(int j=; j<n; j++)
{
int x;
scanf("%d",&x);
b[j][x%].set(i);
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
printf("%d%c",ans(i,j)%,(j!=n-?' ':'\n'));
}
}
return ;
}

hdu 4920 Matrix multiplication bitset优化常数的更多相关文章

  1. HDU 4920 Matrix multiplication(bitset)

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

  2. HDU 4920 Matrix multiplication(bitset优化)

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

  3. hdu 4920 Matrix multiplication(矩阵乘法)2014多培训学校5现场

    Matrix multiplication                                                                           Time ...

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

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

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

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

  6. hdu - 4920 - Matrix multiplication(缓存优化+开挂)

    题意:求两个n x n的矩阵相乘后模3的结果,n <= 800. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 -->>呀呀 ...

  7. HDU 4920 Matrix multiplication (硬件优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 解题报告:求两个800*800的矩阵的乘法. 参考这篇论文:http://wenku.baidu ...

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

    题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据 ...

  9. hdu 4920 Matrix multiplication (矩阵计算)

    题目链接 题意:给两个矩阵a, b, 计算矩阵a*b的结果对3取余. 分析:直接计算时间复杂度是O(n^3),会超时,但是下面第一个代码勉强可以水过,数据的原因. #include <iostr ...

随机推荐

  1. python脚本生成exe可执行文件

    1.先安装第三方插件: py2exe. Get py2exe from http://www.py2exe.org/ 在download里下载与自己python对应的版本 2.写一个测试python文 ...

  2. ARM 开发工具 Keil和DS-5的比较。

    http://www.eeboard.com/bbs/thread-25219-1-1.html 如今ARM体系架构的处理器在嵌入式市场上呼风唤雨,从低端的MCU应用到高端的多媒体消费电子,移动设备领 ...

  3. Linux -- objdump二进制文件比较

    objdump工具用来显示二进制文件的信息,就是以一种可阅读的格式让你更多地了解二进制文件可能带有的附加信息. 常用参数说明 -f 显示文件头信息 -D 反汇编所有section (-d反汇编特定se ...

  4. 使用css让div半透明

    设置div的背景为半透明,下面有个不错示例,使用div+css来实现下,不会的朋友可以参考下 div{filter:alpha(Opacity=80);-moz-opacity:0.5;opacity ...

  5. iptables 设置肯限制流量

    1.查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source ...

  6. Node.js Express 框架 GET方法

    GET 方法 以下实例演示了在表单中通过 GET 方法提交两个参数,我们可以使用 server.js 文件内的 process_get 路由器来处理输入: index.htm 文件代码如下: < ...

  7. asp.net sqlite 当插入数据后的第一次访问会变的很慢!

    <add key="ConnectionStringSQLite" value="data source=|path|DB/XXX.db;Version=3;&qu ...

  8. Microsoft SQL Server 数据库服务器管理维护角色

    固定服务器角色: 按照从最低级别的角色(bulkadmin)到最高级别的角色(sysadmin)的顺序进行描述: Bulkadmin:这个服务器角色的成员可以运行BULK INSERT语句.这条语句允 ...

  9. CheckBox 半选中状态

    <input type='checkbox' />可以半选中,这个特性,很多浏览器都支持,包括Firefox,Chrome和IE 用 input.indeterminate 这个属性来获取 ...

  10. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    参考: http://www.cnblogs.com/sunxucool/archive/2013/06/07/3124380.html   ---------------------------&g ...