fork(3) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone {
  6. public static void main (String[] args) {
  7. Scanner sc = new Scanner (System.in);
  8. int n, m;
  9. m = sc.nextInt();
  10. n = sc.nextInt();
  11. double [][] a = new double [m][m];
  12. double [][] b = new double [m][m];
  13. double [][] c = new double [m][m];
  14. double [][] ans = new double [m][m];
  15. for (int i = 0; i < m; i++) {
  16. for (int j = 0; j < m; j++) {
  17. a[i][j] = sc.nextDouble();
  18. c[i][j] = b[i][j] = ans[i][j] = a[i][j];
  19. }
  20. }
  21. for (int it = 1; it < n; it++) {
  22. for (int i = 0; i < m; i++) {
  23. for (int j = 0; j < m; j++) {
  24. c[i][j] = 0;
  25. for (int k = 0; k < m; k++) {
  26. c[i][j] += a[i][k] * b[k][j];
  27. }
  28. }
  29. }
  30. for (int i = 0; i < m; i++) {
  31. for (int j = 0; j < m; j++) {
  32. ans[i][j] += c[i][j];
  33. b[i][j] = c[i][j];
  34. }
  35. }
  36. }
  37. for (int i = 0; i < m; i++) {
  38. ans[i][i]++;
  39. }
  40. for (int i = 0; i < m; i++) {
  41. for (int j = 0; j < m; j++) {
  42. System.out.print(ans[i][j] + " ");
  43. }
  44. System.out.println();
  45. }
  46. }
  47. }
Success #stdin #stdout 0.16s 321280KB
stdin
2 2
6 7
12 54
stdout
127.0 427.0 
732.0 3055.0