From 91cf19fa9f0b8182c599c396627246d318340eaf Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Sat, 10 May 2025 09:36:53 +0900 Subject: [PATCH] =?UTF-8?q?[20250510]=20BOJ=20/=20G4=20/=20=ED=96=89?= =?UTF-8?q?=EB=A0=AC=20=EC=A0=9C=EA=B3=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\353\240\254 \354\240\234\352\263\261.md" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "lkhyun/202505/10 BOJ G4 \355\226\211\353\240\254 \354\240\234\352\263\261.md" diff --git "a/lkhyun/202505/10 BOJ G4 \355\226\211\353\240\254 \354\240\234\352\263\261.md" "b/lkhyun/202505/10 BOJ G4 \355\226\211\353\240\254 \354\240\234\352\263\261.md" new file mode 100644 index 00000000..89fd6a3e --- /dev/null +++ "b/lkhyun/202505/10 BOJ G4 \355\226\211\353\240\254 \354\240\234\352\263\261.md" @@ -0,0 +1,63 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N; + static long B; + static int[][] matrix; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st; + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + B = Long.parseLong(st.nextToken()); + matrix = new int[N][N]; + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < N; j++) { + matrix[i][j] = Integer.parseInt(st.nextToken()); + } + } + int[][] result; + result = pow(matrix, B); + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + bw.write(result[i][j] + " "); + } + bw.write("\n"); + } + bw.close(); + } + static public int[][] mul(int[][] a, int[][] b) { + int[][] c = new int[N][N]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + for (int k = 0; k < N; k++) { + c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % 1000) % 1000; + } + } + } + return c; + } + static public int[][] pow(int[][] a, long n) { + if(n == 1){ + int[][] result = new int[N][N]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + result[i][j] = a[i][j] % 1000; + } + } + return result; + } + if(n%2 == 0){ + return pow(mul(a,a),n/2); + }else{ + return mul(pow(mul(a,a),n/2),a); + } + } +} + +```