HDU 4920 Matrix multiplication(bitset)

HDU 4920 Matrix multiplication

题目链接

题意:给定两个矩阵,求这两个矩阵相乘mod 3

思路:没什么好的想法,就把0的位置不考虑,结果就过了。然后看了官方题解,上面是用了bitset这个东西,可以用来存大的二进制数,那么对于行列相乘,其实就几种情况,遇到0都是0了,1 1得1,2 1,1 2得2,2 2得1,所以只要存下行列1和2存不存在分别表示的二进制数,然后取且bitcount一下的个数,就可以计算出相应的数值了

代码:

暴力:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

inline void scanf_(int &num)//无负数
{
    char in;
    while((in = getchar()) > '9' || in < '0') ;
    num = in - '0';
    while(in = getchar(),in >= '0' && in <= '9')
	num *= 10,num += in - '0';
}

const int N = 805;

int n;

int a[N][N], av[N][N], an[N], b[N][N], bv[N][N], bn[N], c[N][N];

int main() {
    while (~scanf("%d", &n)) {
	int num;
	memset(an, 0, sizeof(an));
	memset(bn, 0, sizeof(bn));
	memset(c, 0, sizeof(c));
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n; j++) {
		scanf_(num);
		num %= 3;
		if (num == 0) continue;
		av[j][an[j]] = i;
		a[j][an[j]++] = num;
	    }
	}
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n; j++) {
		scanf_(num);
		num %= 3;
		if (num == 0) continue;
		bv[i][bn[i]] = j;
		b[i][bn[i]++] = num;
	    }
	}
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < an[i]; j++) {
		for (int k = 0; k < bn[i]; k++) {
		    int x = av[i][j], y = bv[i][k];
		    c[x][y] = (c[x][y] + a[i][j] * b[i][k]) % 3;
		}
	    }
	}
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n - 1; j++)
		printf("%d ", c[i][j]);
	    printf("%d\n", c[i][n - 1]);
	}
    }
    return 0;
}

bitset:

#include <cstdio>
#include <cstring>
#include <string>
#include <bitset>
using namespace std;

const int N = 805;
int n, num;
bitset<800> row[N][2], col[N][2];

int main() {
    while (~scanf("%d", &n)) {
	for (int i = 0; i < n; i++) {
	    row[i][0].reset();
	    row[i][1].reset();
	    col[i][0].reset();
	    col[i][1].reset();
	    for (int j = 0; j < n; j++) {
		scanf("%d", &num);
		if (num % 3 == 1)
		    row[i][0].set(j, 1);
		if (num % 3 == 2)
		    row[i][1].set(j, 1);
	    }
	}
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n; j++) {
		scanf("%d", &num);
		if (num % 3 == 1)
		    col[j][0].set(i, 1);
		if (num % 3 == 2)
		    col[j][1].set(i, 1);
	    }
	}
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n; j++) {
		int ans = 0;
		ans += (row[i][0]&col[j][0]).count();
		ans += 2 * (row[i][1]&col[j][0]).count() + 2 * (row[i][0]&col[j][1]).count();
		ans += (row[i][1]&col[j][1]).count();
		printf("%d%c", ans % 3, j == n - 1 ? '\n' : ' ');
	    }
	}
    }
    return 0;
}

HDU 4920 Matrix multiplication(bitset),布布扣,bubuko.com

时间: 2024-10-12 17:22:46

HDU 4920 Matrix multiplication(bitset)的相关文章

HDU 4920 Matrix multiplication(std::bitset)

题目连接 :http://acm.hdu.edu.cn/showproblem.php?pid=4920 题意 :给两个n*n的矩阵A.B,要求算的A*B (答案对3取模) (比赛的时候一直想不到怎么去消复杂度,在最后的时候想到了用三进制压几位状态(就是几位几位算)应该可以过的,可是敲完比赛也结束.(压6位是可以过的) 正解是bitset搞,第一次接触到bitset这个神器,用起来确实很炫酷. 方法是统计A矩阵mod3后1出现在哪些位置,2出现哪些位置,B也一样,只是A是以一行为一个“状态”,

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

hdu 4920 Matrix multiplication (矩阵计算)

题目链接 题意:给两个矩阵a, b, 计算矩阵a*b的结果对3取余. 分析:直接计算时间复杂度是O(n^3),会超时,但是下面第一个代码勉强可以水过,数据的原因. 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 const in

hdu 4920 Matrix multiplication(矩阵相乘)多校训练第5场

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

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

题意:求两个n x n的矩阵相乘后模3的结果,n <= 800. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 -->>呀呀.. 1.3层计算的for进行缓存优化,根据CPU的L1级缓存的实现原理,减少缓存的变更.如果每次都计算完一个单元格的结果再计算下一个单元格的结果,那么被乘矩阵的访问就会频繁地更新缓存,使效率很低.. 2.输入开挂,G++提效500ms+.. 3.对乘法进行剪枝.. 没有第1个操作,后果是严重的.. n^3的

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≤

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≤

hdu 4920 Matrix multiplication(高效)

题目链接:4920 Matrix multiplication 题目大意:给定两个n阶矩阵,求矩阵相乘后模3. 解题思路:因为矩阵模掉3后只有0,1,2三种情况.所以对于矩阵A,记录每一行中1,2的位置,借助bitset.矩阵B中每一列1,2的位置.然后对于结果中每个位置,只要考虑1?1,1?2,2?1,2?2的个数即可. #include <cstdio> #include <cstring> #include <bitset> #include <algori