Hdu-4920-Matrix multiplication (Cache Optimization + ON/OFF)
Result of multiplying two n x n matrices and then modulo 3, n <= 800.
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4920
--> Sorry ..
1. Cache Optimization for layer-3 computing reduces cache changes based on the implementation principle of CPU L1 cache. If the result of a cell is calculated every time and then the result of the next cell is calculated, the access to the matrix will frequently update the cache, making the efficiency very low ..
2. Input mounting, G ++ efficiency improvement: 500 ms + ..
3. pruning multiplication ..
There are no 1st operations, and the consequence is serious ..
The complexity of n ^ 3 is too much. However, this is not A positive solution ..
#include
#include
const int MAXN = 800 + 10;int n;int A[MAXN][MAXN], B[MAXN][MAXN], mtRet[MAXN][MAXN];int ReadInt(){ int nRet = 0; char cIn; while ((cIn = getchar()) >= '0' && cIn <= '9') { nRet = nRet * 10 + cIn - '0'; } return nRet % 3;}void Read(){ getchar(); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { A[i][j] = ReadInt(); } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { B[i][j] = ReadInt(); } }}void Solve(){ memset(mtRet, 0, sizeof(mtRet)); for (int i = 1; i <= n; ++i) { for (int k = 1; k <= n; ++k) { if(!A[i][k]) continue; for (int j = 1; j <= n; ++j) { mtRet[i][j] += A[i][k] * B[k][j]; } } }}void Print(){ for (int i = 1; i <= n; ++i) { for (int j = 1; j < n; ++j) { printf("%d ", mtRet[i][j] % 3); } printf("%d\n", mtRet[i][n] % 3); }}int main(){ while (scanf("%d", &n) == 1) { Read(); Solve(); Print(); } return 0;}