Hdu-4920-Matrix multiplication (Cache Optimization + ON/OFF)

Source: Internet
Author: User

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;}
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.