fork download
  1. /*
  2.   Copyright 2011 Marek "p2004a" Rusinowski
  3.   Matrix multiplication (simplest way)
  4. */
  5. #include <cstdio>
  6. #include <cstring>
  7.  
  8. const int cmod = 1000000007;
  9. int n;
  10.  
  11. int *multiply(int *l, int *r) {
  12. int *res = new int [n * n];
  13. for (int i = 0; i < n; ++i) {
  14. for (int k = 0; k < n; ++k) {
  15. for (int j = 0; j < n; ++j) {
  16. *(res + n * i + j) += *(l + n * i + k) * *(r + n * k + j) % cmod;
  17. *(res + n * i + j) %= cmod;
  18. }
  19. }
  20. }
  21. return res;
  22. }
  23.  
  24. void read_matrix(int *t) {
  25. for (int i = 0; i < n; ++i) {
  26. for (int j = 0; j < n; ++j) {
  27. scanf("%d", t + n * i + j);
  28. }
  29. }
  30. }
  31.  
  32. int main() {
  33. scanf("%d", &n);
  34. int *a = new int [n * n], *b = new int [n * n];
  35. read_matrix(a);
  36. read_matrix(b);
  37. int *res = multiply(a, b);
  38. for (int i = 0; i < n; ++i) {
  39. for (int j = 0; j < n; ++j) {
  40. printf("%3d ", *(res + n * i + j));
  41. }
  42. printf("\n");
  43. }
  44. delete a;
  45. delete b;
  46. return 0;
  47. }
  48.  
stdin
2   
2 1
1 1

2 1
1 1
compilation info
prog.cpp: In function ‘void read_matrix(int*)’:
prog.cpp:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp: In function ‘int main()’:
prog.cpp:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout
  5   3 
  3   2