๋ฐ˜์‘ํ˜•
Notice
Recent Posts
Recent Comments
Link
ยซ   2025/04   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
04-03 16:54
๊ด€๋ฆฌ ๋ฉ”๋‰ด

ImJay

[BOJ/Java] 6902. Orko ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜/BOJ - Java

[BOJ/Java] 6902. Orko

ImJay 2025. 3. 13. 17:35
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ [BOJ/Java] 6902. Orko

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ: ๋ฌธ์ œ ๋ณด๊ธฐ


๐Ÿ“– ๋ฌธ์ œ ํ•ด์„

Dumitru๊ฐ€ ์ด์ƒํ•œ ๊ฟˆ์„ ๊พธ๊ฒŒ ๋œ๋‹ค. ๊ฟˆ ์†์—์„œ ๋ฌธ์ด ์ž ๊ธด ๋ฐฉ์— ๊ฐ‡ํžŒ Dumitru๋Š” n๊ฐœ์˜ ๋ฐ•์Šค๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉฐ, ๊ฐ ๋ฐ•์Šค์—๋Š” m๊ฐœ์˜ ์ ‘์‹œ๊ฐ€ ์žˆ๋‹ค. ๊ฐ ์ ‘์‹œ์—๋Š” 1 ์ด์ƒ์˜ ์ •์ˆ˜๊ฐ€ ์ ํ˜€ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋˜ ์ค‘, ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ k์™€ l์ด ์ ํžŒ ์ชฝ์ง€๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ณ , ์ชฝ์ง€์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ž‘์—…์„ ํ•˜๋ผ๋Š” ์ง€์‹œ๊ฐ€ ์ ํ˜€ ์žˆ๋‹ค.

 

1๋‹จ๊ณ„: ์ฒซ ๋ฒˆ์งธ ๋ฐ•์Šค๋ถ€ํ„ฐ n๋ฒˆ์งธ ๋ฐ•์Šค๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ ๋ฐ•์Šค์—์„œ ํ•œ ๊ฐœ์˜ ์ ‘์‹œ๋ฅผ ์„ ํƒํ•œ๋‹ค. ์„ ํƒํ•œ ์ ‘์‹œ์˜ ์ˆซ์ž๋ฅผ ๋…ธํŠธ์— ๊ธฐ๋กํ•œ ํ›„, ํ•ด๋‹น ์ ‘์‹œ์˜ ์ˆซ์ž๋ฅผ 1๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

2๋‹จ๊ณ„: n-1๋ฒˆ์งธ ๋ฐ•์Šค๋ถ€ํ„ฐ ๋‘ ๋ฒˆ์งธ ๋ฐ•์Šค๊นŒ์ง€ ์—ญ์ˆœ์œผ๋กœ ๊ฐ ๋ฐ•์Šค์—์„œ ํ•œ ๊ฐœ์˜ ์ ‘์‹œ๋ฅผ ์„ ํƒํ•œ๋‹ค.

๋ชฉํ‘œ๋Š” ๋…ธํŠธ์— ๊ธฐ๋ก๋œ ์ˆซ์ž๋“ค์˜ ๊ณฑ์ด k๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋„๋ก ํ•˜๋Š” ์„ ํƒ ๋ฐฉ๋ฒ•์˜ ์ด ๊ฐ€์ง“์ˆ˜ T๋ฅผ ๊ตฌํ•˜๊ณ , T๋ฅผ l๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


๐Ÿ› ๏ธ ํ’€์ด ๊ณผ์ •

๋จผ์ €, n, m, k, l๊ณผ ๊ฐ ๋ฐ•์Šค์˜ ์ ‘์‹œ ๊ฐ’๋“ค์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ k์˜ ์•ฝ์ˆ˜๋ฅผ ์ƒํƒœ๋กœ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. k์˜ ๋ชจ๋“  ์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•œ ๋’ค, ๊ฐ ์•ฝ์ˆ˜์— ์ธ๋ฑ์Šค๋ฅผ ๋งคํ•‘ํ•˜์—ฌ ์ƒํƒœ ์ „์ด ํ…Œ์ด๋ธ”์„ ๊ตฌ์„ฑํ•œ๋‹ค.

 

๊ฐ ๋ฐ•์Šค๋งˆ๋‹ค ์ ‘์‹œ ์„ ํƒ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฐ•์Šค์™€ ๋งˆ์ง€๋ง‰ ๋ฐ•์Šค๋Š” ์ ‘์‹œ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์„ ํƒํ•˜๋ฏ€๋กœ, ๊ฐ ์ ‘์‹œ๊ฐ€ ๊ธฐ์—ฌํ•˜๋Š” ๊ฐ’ ๊ทธ๋Œ€๋กœ ๋ฐ˜์˜๋œ๋‹ค.

 

์ค‘๊ฐ„ ๋ฐ•์Šค๋Š” ์ ‘์‹œ๋ฅผ ๋‘ ๋ฒˆ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ, ์ฒซ ๋ฒˆ์งธ ์„ ํƒ(์ ‘์‹œ์˜ ์›๋ž˜ ์ˆซ์ž ๊ธฐ๋ก)๊ณผ ๋‘ ๋ฒˆ์งธ ์„ ํƒ(์ ‘์‹œ ์ˆซ์ž๊ฐ€ 1๋กœ ๋ณ€๊ฒฝ๋จ)์„ ๋ชจ๋‘ ๊ณ ๋ คํ•˜์—ฌ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ˆ„์ ํ•œ๋‹ค.

 

๋™์  ๊ณ„ํš๋ฒ•(DP)์„ ์ด์šฉํ•ด ๊ฐ ๋ฐ•์Šค์—์„œ ๊ณ„์‚ฐ๋œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ˆ„์ ํ•˜๋ฉฐ, ํ˜„์žฌ๊นŒ์ง€ ์„ ํƒํ•œ ์ ‘์‹œ๋“ค์˜ ๊ณฑ์˜ gcd(k, ·) ๊ฐ’์„ ์ƒํƒœ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ์ „์ด๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

๋ชจ๋“  ๋ฐ•์Šค๋ฅผ ์ฒ˜๋ฆฌํ•œ ํ›„, ์ตœ์ข… ์ƒํƒœ๊ฐ€ k(์ฆ‰, k๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ƒํƒœ)๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ l๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๐Ÿ’ป ์ฝ”๋“œ ๊ตฌํ˜„ (Java)

import java.io.*; 
import java.util.*;

public class Main {

// ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜(GCD)๋ฅผ ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ
static long gcd(long a, long b) {
    // b๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    while (b != 0) {
        long temp = a % b;  // a๋ฅผ b๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ž„์‹œ ์ €์žฅ
        a = b;              // a์— b ๊ฐ’์„ ๋Œ€์ž…
        b = temp;           // b์— ๋‚˜๋จธ์ง€ ๊ฐ’์„ ๋Œ€์ž…
    }
    return a;             // ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ ๋ฐ˜ํ™˜
}

public static void main(String[] args) throws Exception {
    // ์ž…๋ ฅ์„ ์œ„ํ•œ BufferedReader ์ดˆ๊ธฐํ™”
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    // ์ฒซ ๋ฒˆ์งธ ์ค„ ์ž…๋ ฅ: ๋ฐ•์Šค ๊ฐœ์ˆ˜ n๊ณผ ๊ฐ ๋ฐ•์Šค๋‹น ์ ‘์‹œ ๊ฐœ์ˆ˜ m
    StringTokenizer st = new StringTokenizer(br.readLine());
    int n = Integer.parseInt(st.nextToken());
    int m = Integer.parseInt(st.nextToken());
    
    // ๋‘ ๋ฒˆ์งธ ์ค„ ์ž…๋ ฅ: ๋ชฉํ‘œ ์ •์ˆ˜ k์™€ ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•  ๊ฐ’ mod
    st = new StringTokenizer(br.readLine());
    int k = Integer.parseInt(st.nextToken());
    int mod = Integer.parseInt(st.nextToken());
    
    // k์˜ ๋ชจ๋“  ์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•จ (๊ฐ ์ƒํƒœ๋กœ ํ™œ์šฉ)
    ArrayList<Integer> dList = new ArrayList<>();
    for (int i = 1; i * i <= k; i++) {
        if (k % i == 0) {
            dList.add(i);
            if (i * i != k) {
                dList.add(k / i);
            }
        }
    }
    
    // ์•ฝ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
    Collections.sort(dList);
    int dCount = dList.size();
    
    // ์•ฝ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅ
    int[] dArr = new int[dCount];
    for (int i = 0; i < dCount; i++) {
        dArr[i] = dList.get(i);
    }
    
    // ๊ฐ ์•ฝ์ˆ˜์— ๋Œ€ํ•ด ์ธ๋ฑ์Šค๋ฅผ ๋งคํ•‘ (์ƒํƒœ ์ „์ด ํ…Œ์ด๋ธ” ๊ตฌ์„ฑ์— ํ™œ์šฉ)
    HashMap<Integer, Integer> dIndex = new HashMap<>();
    for (int i = 0; i < dCount; i++) {
        dIndex.put(dArr[i], i);
    }
    
    // ์ƒํƒœ ์ „์ด ํ…Œ์ด๋ธ” ๊ตฌ์„ฑ: ๋‘ ์ƒํƒœ์˜ ๊ณฑ์˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด gcd(k, ๊ณฑ) ๊ฐ’์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐ
    int[][] mult = new int[dCount][dCount];
    for (int i = 0; i < dCount; i++) {
        for (int j = 0; j < dCount; j++) {
            long product = (long) dArr[i] * dArr[j];
            int g = (int) gcd(product, k);
            mult[i][j] = dIndex.get(g);
        }
    }
    
    // DP ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”: dp[i]๋Š” ํ˜„์žฌ ์ƒํƒœ๊ฐ€ dArr[i]์ผ ๋•Œ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜
    int[] dp = new int[dCount];
    // ์ดˆ๊ธฐ ์ƒํƒœ๋Š” ์ ‘์‹œ ์„ ํƒ ์ „ ๊ณฑ์ด 1์ธ ์ƒํƒœ
    dp[dIndex.get(1)] = 1;
    
    // ๊ฐ ๋ฐ•์Šค๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌ
    for (int box = 1; box <= n; box++) {
        
        // ํ˜„์žฌ ๋ฐ•์Šค์˜ ์ ‘์‹œ๋“ค์„ ์ฝ์–ด, ๊ฐ ์ ‘์‹œ์˜ ์ˆซ์ž์— ๋”ฐ๋ฅธ ์ƒํƒœ(์•ฝ์ˆ˜ ์ƒํƒœ)์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ณ„์‚ฐ
        int[] freq = new int[dCount];
        st = new StringTokenizer(br.readLine());
        for (int j = 0; j < m; j++) {
            int plateValue = Integer.parseInt(st.nextToken());
            int g = (int) gcd(plateValue, k);
            int idx = dIndex.get(g);
            freq[idx]++;  // ํ•ด๋‹น ์ƒํƒœ์˜ ๋นˆ๋„์ˆ˜ ์ฆ๊ฐ€
        }
        
        // ํ˜„์žฌ ๋ฐ•์Šค์—์„œ ์„ ํƒ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜ ๋ถ„ํฌ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
        int[] boxDist = new int[dCount];
        
        // ์ฒซ ๋ฒˆ์งธ ๋ฐ•์Šค์™€ ๋งˆ์ง€๋ง‰ ๋ฐ•์Šค๋Š” ์ ‘์‹œ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์„ ํƒํ•˜๋ฏ€๋กœ ๋‹จ์ˆœํžˆ freq ๊ฐ’์„ ์‚ฌ์šฉ
        if (box == 1 || box == n) {
            for (int i = 0; i < dCount; i++) {
                boxDist[i] = freq[i] % mod;
            }
        } else {
            // ์ค‘๊ฐ„ ๋ฐ•์Šค๋Š” ์ ‘์‹œ๋ฅผ ๋‘ ๋ฒˆ ์„ ํƒํ•ด์•ผ ํ•จ
            // ์ฒซ ๋ฒˆ์งธ ์„ ํƒ: ์ ‘์‹œ์˜ ์›๋ž˜ ์ˆซ์ž๋ฅผ ๊ธฐ๋ก
            // ๋‘ ๋ฒˆ์งธ ์„ ํƒ: ํ•ด๋‹น ์ ‘์‹œ์˜ ์ˆซ์ž๊ฐ€ 1๋กœ ๋ณ€ํ•จ
            int[] doubleDist = new int[dCount];
            
            // ์ฒซ ๋ฒˆ์งธ ์„ ํƒ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถ”๊ฐ€
            for (int i = 0; i < dCount; i++) {
                doubleDist[i] = (doubleDist[i] + freq[i]) % mod;
            }
            
            // ๋‘ ๋ฒˆ์งธ ์„ ํƒ์— ๋Œ€ํ•ด, ๊ฐ™์€ ์ ‘์‹œ๋ฅผ ๋‘ ๋ฒˆ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ์™€ ๋‹ค๋ฅธ ์ ‘์‹œ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ๊ณ ๋ ค
            for (int i = 0; i < dCount; i++) {
                for (int j = 0; j < dCount; j++) {
                    long ways;
                    if (i == j) {
                        // ๊ฐ™์€ ์ ‘์‹œ๋ฅผ ๋‘ ๋ฒˆ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ
                        ways = (long) freq[i] * (freq[i] - 1);
                    } else {
                        // ์„œ๋กœ ๋‹ค๋ฅธ ์ ‘์‹œ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ
                        ways = (long) freq[i] * freq[j];
                    }
                    if (ways == 0)
                        continue;
                    
                    // ๋‘ ์„ ํƒ์˜ ๊ฒฐ๊ณผ ์ƒํƒœ๋ฅผ ์ƒํƒœ ์ „์ด ํ…Œ์ด๋ธ”์„ ์ด์šฉํ•ด ๊ณ„์‚ฐ
                    int newIndex = mult[i][j];
                    doubleDist[newIndex] = (int) ((doubleDist[newIndex] + ways) % mod);
                }
            }
            boxDist = doubleDist;
        }
        
        // ํ˜„์žฌ๊นŒ์ง€์˜ dp ๋ฐฐ์—ด๊ณผ ํ˜„์žฌ ๋ฐ•์Šค์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜(boxDist)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒํƒœ ์ „์ด๋ฅผ ์ˆ˜ํ–‰
        int[] nextDP = new int[dCount];
        for (int i = 0; i < dCount; i++) {
            if (dp[i] == 0)
                continue;
            for (int j = 0; j < dCount; j++) {
                if (boxDist[j] == 0)
                    continue;
                int nextState = mult[i][j];  // ์ƒํƒœ ์ „์ด
                nextDP[nextState] = (int) ((nextDP[nextState] + (long) dp[i] * boxDist[j]) % mod);
            }
        }
        
        // dp ๋ฐฐ์—ด์„ ์—…๋ฐ์ดํŠธ
        dp = nextDP;
    }
    
    // ์ตœ์ข…์ ์œผ๋กœ ๋ชฉํ‘œ ์ƒํƒœ k(์ฆ‰, k๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ƒํƒœ)์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ mod๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅ
    System.out.println(dp[dIndex.get(k)] % mod);
}

โฑ๏ธ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

๋ฌธ์ œ๋Š” ๊ฐ ๋ฐ•์Šค๋งˆ๋‹ค m๊ฐœ์˜ ์ ‘์‹œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ , k์˜ ์•ฝ์ˆ˜ ๊ฐœ์ˆ˜๋ฅผ d๋ผ ํ•  ๋•Œ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n*m + n*d²)์ด๋‹ค.
n์€ ์ตœ๋Œ€ 200, m์€ ์ตœ๋Œ€ 10,000์ด๋ฉฐ, d๋Š” k์˜ ์•ฝ์ˆ˜ ๊ฐœ์ˆ˜๋กœ ๋ณดํ†ต √k์— ๊ฐ€๊นŒ์›Œ ์ œํ•œ ๋‚ด์—์„œ ์ถฉ๋ถ„ํžˆ ๊ณ„์‚ฐ ๊ฐ€๋Šฅํ•˜๋‹ค.


๐Ÿ’ก ๋Š๋‚€์ 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •์—์„œ ๊ฐ ๋ฐ•์Šค์—์„œ ์ ‘์‹œ ์„ ํƒ์— ๋”ฐ๋ฅธ ์ƒํƒœ ์ „์ด๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด์—ˆ๋‹ค.
์ •์ˆ˜๋ก ๊ณผ gcd๋ฅผ ํ™œ์šฉํ•˜์—ฌ k์˜ ์•ฝ์ˆ˜๋ฅผ ์ƒํƒœ๋กœ ์ฒ˜๋ฆฌํ•œ ์ , ๊ทธ๋ฆฌ๊ณ  ์ค‘๊ฐ„ ๋ฐ•์Šค์—์„œ ๋‘ ๋ฒˆ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์„ธ๋ฐ€ํ•˜๊ฒŒ ๊ณ ๋ คํ•œ ์ ์ด ์ธ์ƒ์ ์ด์—ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

'์•Œ๊ณ ๋ฆฌ์ฆ˜ > BOJ - Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ/Java] 12422. ์ฐฝ๋ฌธ ๊นจ๊ธฐ (Large)  (1) 2025.03.14
[BOJ/Java] 3628. Knockdown  (0) 2025.03.14
[BOJ/Java] 5266. Strange Dream  (0) 2025.03.13
[BOJ/Java] 11404. ํ”Œ๋กœ์ด๋“œ  (1) 2025.03.10
[BOJ/Java] 2931. ๊ฐ€์Šค๊ด€  (0) 2025.03.10
Comments