codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
// maze package MatrixProduct; import java.io.FileInputStream; import java.util.Scanner; public class Solution { public Scanner sc; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub System.setIn(new FileInputStream("src/MatrixProduct/data")); new Solution(); } public int T = 0; public int length = 0; public long[][] A; public int M = 0; public long q = 100000007; public Solution() { sc = new Scanner(System.in); T = sc.nextInt(); for (int tc = 1; tc <= T; tc++) { length = sc.nextInt(); A = new long[length][length]; M = sc.nextInt(); for (int row = 0; row < length; row++) { for(int col = 0; col<length; col++){ A[row][col] = sc.nextInt(); } } long [][] a = Mu(A, M); System.out.println("Case #" + tc); for (int row = 0; row < length; row++) { for(int col = 0; col<length; col++){ System.out.print(a[row][col]+" "); } System.out.println(""); } // System.out.println(""); } } public long[][] Mu(long[][] a, int m) { if(m == 1) return a; if (m == 2) return Nhan(a, a); else { long[][] c = Mu(a, m / 2); c = Mu(c, 2); if (m % 2 != 0) { return Nhan(a, c); } else { return c; } } } public long[][] Nhan(long[][] a, long[][] b) { long[][] c = new long[length][length]; long total = 0; for (int row = 0; row < length; row++) { for (int col = 0; col < length; col++) { total = 0; for (int n = 0; n < length; n++) { total += (a[row][n] * b[n][col])%q; } c[row][col] = total%q; } } return c; } }
Private
[
?
]
Run code
Submit