baekjoon 20260111

This commit is contained in:
songyc macbook 2026-01-11 22:02:15 +09:00
parent 38b311b802
commit ca4a7fdb2c
2 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,43 @@
import java.util.*;
public class _10775 {
static int[] parents;
static int ans = 0;
static int find(int x) {
if(parents[x] != x) return parents[x] = find(parents[x]);
return parents[x];
}
static void union(int x, int y) {
x = find(x);
y = find(y);
if(x > y) {
int temp = x;
x = y;
y = temp;
}
parents[y] = x;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int G = sc.nextInt(), P = sc.nextInt();
parents = new int[G+1];
for(int i=1; i<=G; i++) parents[i] = i;
for(int i=0; i<P; i++) {
int gi = sc.nextInt();
gi = find(gi);
if(gi == 0) break;
union(gi, gi-1);
ans++;
}
System.out.println(ans);
sc.close();
}
}

View File

@ -0,0 +1,63 @@
import java.util.*;
public class _10830 {
static int MOD = 1000;
static void print_mat(int[][] M, int N) {
for (int[] row : M) {
for(int i=0; i<N; i++) System.out.printf("%d ",row[i]);
System.out.println();
}
}
static int[][] mul_mat(int[][] m1, int[][] m2, int N) {
int[][] result = 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++) {
result[i][j] += m1[i][k]*m2[k][j];
}
result[i][j] %= MOD;
}
}
return result;
}
static int[][] power_mat(int[][] A, long B, int N) {
int[][] result = new int[N][N];
if(B==1) {
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
result[i][j] = A[i][j]%MOD;
}
}
return result;
}
int[][] temp = power_mat(A, B/2, N);
if(B%2 == 0) return mul_mat(temp, temp, N);
return mul_mat(mul_mat(temp, temp, N), A, N);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long B = sc.nextLong();
int[][] A = new int[N][N];
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
A[i][j] = sc.nextInt();
}
}
sc.close();
int[][] result = power_mat(A, B, N);
print_mat(result, N);
}
}