zoukankan      html  css  js  c++  java
  • HDU 4920 居然会超时

    题意:求两个n*n的矩阵相乘的结果,得出的每个元素%3;

    分析:2000ms然后n的范围是800,我们自己估算的时间复杂度并不会超时,但是结果就是超时了。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <sstream>
     4 #include <cmath>
     5 #include <cstring>
     6 #include <cstdlib>
     7 #include <string>
     8 #include <vector>
     9 #include <map>
    10 #include <set>
    11 #include <queue>
    12 #include <stack>
    13 #include <algorithm>
    14 using namespace std;
    15 #define ll long long
    16 #define _cle(m, a) memset(m, a, sizeof(m))
    17 #define repu(i, a, b) for(int i = a; i < b; i++)
    18 #define repd(i, a, b) for(int i = b; i >= a; i--)
    19 #define sfi(n) scanf("%d", &n)
    20 #define pfi(n) printf("%d
    ", n)
    21 #define MAXN 100010
    22 const int N = 810;
    23 int n;
    24 int a[N][N], b[N][N], c[N][N];
    25 int scan()
    26 {
    27     int res = 0 , ch;
    28     while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    29     {
    30         if( ch == EOF )  return 1 << 30 ;
    31     }
    32     res = ch - '0' ;
    33     while( ( ch = getchar() ) >= '0' && ch <= '9' )
    34         res = res * 10 + ( ch - '0' ) ;
    35     return res ;
    36 }
    37 
    38 int main()
    39 {
    40     while(~scanf("%d", &n))
    41     {
    42         repu(i,0,n)
    43         repu(j,0,n)
    44         {
    45             a[i][j] = scan() % 3;
    46         }
    47         repu(i,0,n)
    48         repu(j,0,n)
    49         {
    50             b[i][j] = scan() % 3;
    51         }
    52         memset(c, 0, sizeof(c));
    53         repu(i,0,n)
    54         {
    55             repu(j,0,n)
    56             {
    57                 repu(k,0,n)
    58                 {
    59                     //c[i][j] += a[i][k] * b[k][j];///会超时
    60                     //c[i][k] += a[i][j] * b[j][k];///不会超时
    61                     c[j][k] += a[j][i] * b[i][k];///不会超时
    62                 }
    63             }
    64         }
    65         for(int i = 0; i < n; i++)
    66         {
    67             for(int j = 0; j < n; j++)
    68             {
    69                 c[i][j] %= 3;
    70                 if(j) printf(" %d", c[i][j]);
    71                 else printf("%d", c[i][j]);
    72             }
    73             puts("");
    74         }
    75     }
    76     return 0;
    77 }
    View Code

    我当时找稀疏矩阵的时候就找到的是直接矩阵相乘的模板,但是还是超时了,后来才知道我的输入挂需要重新更新了。

    不过我还有一点疑惑就是为什么同样是三层循环(i,j,k),但是跑出来的时间不一样的呢。。。。。。。

    c[i][j] += a[i][k] * b[k][j];///会超时
    c[i][k] += a[i][j] * b[j][k];///不会超时
    c[j][k] += a[j][i] * b[i][k];///不会超时
  • 相关阅读:
    风雨中,苦算什么!!!
    痛心疾首+无奈绝望!!!
    PHP页面跳转总结
    Java的HttpClient的实现
    java细节篇(==和equals的区别)
    cmd命令笔记
    Python的HttpClient实现
    常用linux命令
    Go的HttpClient实现
    android问题笔记集
  • 原文地址:https://www.cnblogs.com/ACMERY/p/4836565.html
Copyright © 2011-2022 走看看