From 6d707f64414b50d7c47dcd75db87104dec8fddf2 Mon Sep 17 00:00:00 2001 From: oncsr Date: Thu, 13 Feb 2025 08:34:39 +0900 Subject: [PATCH] =?UTF-8?q?[20250213]=20BOJ=20/=20G3=20/=20K=EC=A7=84=20?= =?UTF-8?q?=ED=8A=B8=EB=A6=AC=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...K\354\247\204 \355\212\270\353\246\254.md" | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 "khj20006/202502/13 BOJ G3 K\354\247\204 \355\212\270\353\246\254.md" 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; + } + +} + +```