Hdu 4920 Matrix multiplication (efficient)
Link: 4920 Matrix multiplication
Given two n-order matrices, evaluate the modulo after matrix multiplication 3.
Solution: there are only three situations after matrix modulo 3: 0, 1, and 2. Therefore, for matrix A, the location of 1 and 2 in each row is recorded with bitset. Position of 1, 2 in each column of matrix B. Then, for each position in the result, consider 1? 1, 1? 2, 2? 1, 2? 2.
#include
#include
#include
#include using namespace std;const int maxn = 805;int N, C[maxn][maxn];bitset
x[maxn][2], y[maxn][2];void init () { int u; memset(C, 0, sizeof(C)); for (int i = 0; i < N; i++) { for (int j = 0; j < 2; j++) { x[i][j].reset(); y[i][j].reset(); } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &u); u %= 3; if (u) x[i][u-1].set(j, 1); } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &u); u %= 3; if (u) y[j][u-1].set(i, 1); } }}int solve (int u, int v) { int ret = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { bitset
k = x[u][i]&y[v][j]; ret = (ret + (i+1)*(j+1)*k.count()) % 3; } } return ret;}int main () { while (scanf("%d", &N) == 1) { init(); for (int i = 0; i < N; i++) { printf("%d", solve(i, 0)); for (int j = 1; j < N; j++) printf(" %d", solve(i, j)); printf("\n"); } } return 0;}