This question... TLE to tears
Later, a person in discuss provided a link and suddenly realized that he had never paid attention to it before?
Transfer
Then, as long as you follow the second method, It's really ac... although the time is still very tight, it's not TLE at least.
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int size = 810; 6 int x[size][size]; 7 int y[size][size]; 8 int z[size][size]; 9 10 int main()11 {12 cin.sync_with_stdio(false);13 int n , num;14 while( cin >> n )15 {16 memset( z , 0 , sizeof(z) );17 for( int i = 0 ; i<n ; i++ )18 {19 for( int j = 0 ; j<n ; j++ )20 {21 cin >> num;22 x[i][j] = num % 3;23 }24 }25 for( int i = 0 ; i<n ; i++ )26 {27 for( int j = 0 ; j<n ; j++ )28 {29 cin >> num;30 y[i][j] = num % 3;31 }32 }33 for( int i = 0 ; i<n ; i++ )34 {35 for( int k = 0 ; k<n ; k++ )36 {37 if( x[i][k] )38 {39 for( int j = 0 ; j<n ; j++ )40 {41 z[i][j] += x[i][k] * y[k][j];42 }43 }44 }45 }46 for( int i = 0 ; i<n ; i++ )47 {48 cout << (z[i][0]%3);49 for( int j = 1 ; j<n ; j++ )50 {51 cout << " " << (z[i][j]%3) ;52 }53 cout << endl;54 }55 }56 return 0;57 }
View code
There is another important judgment.
If (X [I] [k])
Is there a time difference of more than 300 ms? Because of this, a lot of pruning at once, you don't have to enter the for loop.
Today:
I met a cat while diving, but never met you.
I met a dog for rock climbing, But I didn't met you.
I met you when it was snowing in the summer.
I never met you when I hit thunder in winter.
I met all the extraordinary
But never met you.
HDU -- 4920 -- previously written slow Matrix Multiplication