矩阵乘法。不过这里加法被xor取代,乘法有三种情况要分别处理。尤其要注意,矩阵中的数是8位的,所能表示的最大数为255,所以,左移后要与上255或对256取模。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int t;
unsigned int state[4][4] = {
{0x2, 0x3, 0x1, 0x1},
{0x1, 0x2, 0x3, 0x1},
{0x1, 0x1, 0x2, 0x3},
{0x3, 0x1, 0x1, 0x2}
};
unsigned int mat[4][4];
unsigned int ans[4][4];
int main()
{
scanf("%d", &t);
for (int cas = 1; cas <= t; ++cas) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
scanf("%X", &mat[i][j]);
}
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
int tmp = 0, a;
for (int k = 0; k < 4; ++k) {
if (state[i][k] == 0x1) {
a = mat[k][j];
} else if (state[i][k] == 0x2) {
a = mat[k][j];
a <<= 1;
if (a > 0xFF) {
a ^= 0x1B;
a %= 256;
}
} else if (state[i][k] == 0x3) {
a = mat[k][j];
a <<= 1;
if (a > 0xFF) {
a ^= 0x1B;
a %= 256;
}
a ^= mat[k][j];
}
tmp ^= a;
}
ans[i][j] = tmp;
}
}
if (cas != 1) {
printf("\n");
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (j == 3) {
printf("%02X\n", ans[i][j]);
} else {
printf("%02X ", ans[i][j]);
}
}
}
}
return 0;
}