HDU-4364 Matrix operation
简单矩阵...
代码如下:
#include <cstring> #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; unsigned int temp[5][5] = { {0, 0, 0, 0, 0}, {0, 2, 3, 1, 1}, {0, 1, 2, 3, 1}, {0, 1, 1, 2, 3}, {0, 3, 1, 1, 2} }; unsigned int fix(unsigned int x) { if (x > 0xff) { x ^= 0x1b; } return x % (0xff+1); } unsigned int op(unsigned int b, unsigned int a) { if (b == 1) { return fix(a); } else if (b == 2) { return fix(a << 1); } else { return fix( fix(a << 1) ^ a ); } } struct Matrix { unsigned int a[5][5]; Matrix unit () { Matrix ret; for (unsigned int i = 1; i <= 4; ++i) { ret.a[i][i] = 1; } return ret; } void zero(){ memset(a, 0, sizeof(a)); } Matrix operator * (Matrix temp) const { Matrix ret; ret.zero(); for ( int i = 1; i <= 4; ++i) { for ( int j = 1; j <= 4; ++j) { for ( int k = 1; k <= 4; ++k) { ret.a[i][j] ^= op(a[i][k], temp.a[k][j]); fix(ret.a[i][j]); } } } return ret; } void read() { for ( int i = 1; i <= 4; ++i) { for ( int j = 1; j <= 4; ++j) { scanf("%X", &a[i][j]); } } } void copy(unsigned int temp[][5]) { for ( int i = 1; i <= 4; ++i) { for ( int j = 1; j <= 4; ++j) { a[i][j] = temp[i][j]; } } } void print() { for (int i = 1; i <= 4; ++i) { for (int j = 1; j <= 4; ++j) { printf(j==1 ? "%02X" : " %02X", a[i][j]); } puts(""); } } }M, S, ret; int main() { unsigned int T; scanf("%u", &T); while (T--) { M.read(); S.copy(temp); ret = S * M; ret.print(); if (T > 0) { puts(""); } } return 0; }