diff --git "a/khj20006/202502/13 BOJ G3 K\354\247\204 \355\212\270\353\246\254.md" "b/khj20006/202502/13 BOJ G3 K\354\247\204 \355\212\270\353\246\254.md" new file mode 100644 index 00000000..79ce7c90 --- /dev/null +++ "b/khj20006/202502/13 BOJ G3 K\354\247\204 \355\212\270\353\246\254.md" @@ -0,0 +1,74 @@ +```java + +import java.util.*; +import java.io.*; + + +class Main { + + // IO field + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + static long nextLong() {return Long.parseLong(st.nextToken());} + static void bwEnd() throws Exception {bw.flush();bw.close();} + + // Additional field + static long N, K, Q; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + } + + static void ready() throws Exception{ + + nextLine(); + N = nextLong(); + K = nextLong(); + Q = nextLong(); + + } + + static void solve() throws Exception{ + + while(Q-- > 0) { + nextLine(); + long x = nextLong(), y = nextLong(); + bw.write(dist(x-1,y-1) + "\n"); + } + + } + + static long dist(long x, long y) throws Exception{ + if(K == 1) return Math.abs(x-y); + List X = find(x), Y = find(y); + int i = X.size()-1, j = Y.size()-1; + + while(i>=0 && j>=0 && X.get(i).equals(Y.get(j))){ + i--; + j--; + } + return i+j+2; + } + + static List find(long x){ + List result = new ArrayList<>(); + + result.add(x); + while(x > 0) { + x = (x-1)/K; + result.add(x); + } + return result; + } + +} + +```