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. iOS,信息加解密

    1.AES加解密 AES加解密 // //  AESEncryptAndDecrypt.h //  NSData扩展方法,用于处理aes加解密 // //  Created by Vie on 16/ ...

  2. @overrive报错解决方案

    有时将项目移到另一个环境里出现@overrive报错的情况,可以看看eclipse工具的编译设置,将编译版本设置高点.

  3. leetcode 204

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解法一: 遍历从1-n ...

  4. git基本配置

    用户信息 你个人的用户名称和电子邮件地址,用户名可随意修改,git 用于记录是谁提交了更新,以及更新人的联系方式. $ git config --global user.name "Donl ...

  5. $.Ajax({});方法使用 返回json格式 string格式

    //Json格式 $.ajax({ url: url + "?action=Save1", type: "post", dataType: "json ...

  6. github创建文件夹

      网页上只能通过在创建新文件的时候顺便创建文件夹(文件夹与文件用 / 隔开),例如home/test.md就在该仓库下创建了一个文件夹home,该文件夹下有一个新的文件test.md

  7. IIS7 全新管理工具AppCmd.exe的命令使用实例分享

    IIS 7 提供了一个新的命令行工具 Appcmd.exe,可以使用该工具来配置和查询 Web 服务器上的对象,并以文本或 XML 格式返回输出. 下面是一些可以使用 Appcmd.exe 完成的任务 ...

  8. SqlServer 一个查询语句以致tempdb增大55G (转载)

    SqlServer 一个查询语句导致tempdb增大55G 今天操作着服务器,突然右下角提示“C盘空间不足”! 吓一跳!~ 看看C盘,还有7M!!!这么大的C盘空间怎么会没了呢?搞不好等下服务器会动不 ...

  9. logstash实战

    官网上的文档没有更新,估计快商业化了,elasticsearch和kibana 都内嵌了不用再下载,可以参看1.3.3的,除了打个包外没啥区别 http://logstash.net/docs/1.3 ...

  10. 108 vpn iptables

    [root@fge108 webapps]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 47.88.1 ...