HDU 4920 (水~)

2019-04-14 20:23发布

题目链接:点击这里

题意: 求两个矩阵的模3乘积。

被我用暴力炸过去了。因为模3, 所以0会出现的比较多, 所以枚举的时候0可以省去一维。

#include using namespace std; #define maxn 805 int n; int a[maxn][maxn], b[maxn][maxn]; int ans[maxn][maxn]; int main () { while (scanf ("%d", &n) == 1) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf ("%d", &a[i][j]); a[i][j] %= 3; ans[i][j] = 0; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf ("%d", &b[i][j]); b[i][j] %= 3; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) if (a[i][j]) { for (int k = 1; k <= n; k++) { ans[i][k] += a[i][j]*b[j][k]; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { printf ("%d%c", ans[i][j]%3, (j == n ? ' ' : ' ')); } } } return 0; }