๋ฐ˜์‘ํ˜•
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-04 08:44
๊ด€๋ฆฌ ๋ฉ”๋‰ด

ImJay

[BOJ/Java] 5266. Strange Dream ๋ณธ๋ฌธ

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

[BOJ/Java] 5266. Strange Dream

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

๐Ÿ“Œ [BOJ/Java] 5266. Strange Dream

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


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

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

 

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

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

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

 

โœ… ํ•ต์‹ฌ ๊ฐœ๋…

  • ์ •์ˆ˜๋ก ๊ณผ gcd๋ฅผ ํ™œ์šฉํ•ด k์˜ ์•ฝ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  • ๊ฐ ๋ฐ•์Šค๋ณ„ ์ ‘์‹œ ์„ ํƒ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•ด ๊ณ„์‚ฐํ•œ๋‹ค.
  • ๋™์  ๊ณ„ํš๋ฒ•(DP)์„ ํ†ตํ•ด ์ƒํƒœ ์ „์ด๋ฅผ ๋ˆ„์ ํ•œ๋‹ค.

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

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

 

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

 

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

 

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

 

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


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

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

public class Main {
    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 prod = (long) dArr[i] * dArr[j];
                int g = (int) gcd(prod, k);
                mult[i][j] = dIndex.get(g);
            }
        }
        
        // dp ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” (ํ˜„์žฌ ์ƒํƒœ๋ณ„ ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ €์žฅ)
        int[] dp = new int[dCount];
        // ์ดˆ๊ธฐ ์ƒํƒœ๋Š” 1 (๊ณฑ์˜ ๊ฒฐ๊ณผ๊ฐ€ 1)์ธ ์ƒํƒœ์—์„œ ์‹œ์ž‘
        dp[dIndex.get(1)] = 1;
        
        // ๊ฐ ๋ฐ•์Šค๋ณ„๋กœ ์ฒ˜๋ฆฌ (์ด n๊ฐœ์˜ ๋ฐ•์Šค)
        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 val = Integer.parseInt(st.nextToken());
                int g = (int) gcd(val, k);
                int idx = dIndex.get(g);
                freq[idx]++; // ํ•ด๋‹น ์•ฝ์ˆ˜ ์ƒํƒœ์— ํ•ด๋‹นํ•˜๋Š” ์ ‘์‹œ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€
            }
            
            // ํ˜„์žฌ ๋ฐ•์Šค์—์„œ ์„ ํƒํ•  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•  ๋ฐฐ์—ด
            int[] boxDist = new int[dCount];
            if (box == 1 || box == n) {
                // ์ฒซ ๋ฒˆ์งธ ๋ฐ•์Šค์™€ ๋งˆ์ง€๋ง‰ ๋ฐ•์Šค๋Š” ํ•œ ๋ฒˆ๋งŒ ์„ ํƒํ•˜๋ฏ€๋กœ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ
                for (int i = 0; i < dCount; i++) {
                    boxDist[i] = freq[i] % mod;
                }
            } else {
                // ์ค‘๊ฐ„ ๋ฐ•์Šค๋Š” ๋‘ ๋ฒˆ ์„ ํƒํ•จ (ํ•œ ๋ฒˆ์€ ๊ธฐ๋ก ํ›„ 1๋กœ ๋ณ€๊ฒฝ, ๋‘ ๋ฒˆ ์„ ํƒ ์‹œ ๊ณฑ์— ์˜ํ–ฅ ์—†์Œ)
                int[] doubleDist = new int[dCount];
                // ๋จผ์ € ํ•œ ๋ฒˆ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ (๊ธฐ๋ณธ์ ์œผ๋กœ freq ๊ฐ’์„ ๋”ํ•จ)
                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++) {
                        // i์™€ j๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ: ๊ฐ™์€ ์ ‘์‹œ๋ฅผ ๋‘ ๋ฒˆ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ
                        // i์™€ j๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ: ์„œ๋กœ ๋‹ค๋ฅธ ์ ‘์‹œ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ
                        long ways = (i == j) ? (long) freq[i] * (freq[i] - 1) : (long) freq[i] * freq[j];
                        if (ways == 0)
                            continue;
                        int idx = mult[i][j];
                        doubleDist[idx] = (int) ((doubleDist[idx] + ways) % mod);
                    }
                }
                boxDist = doubleDist;
            }
            
            // ํ˜„์žฌ๊นŒ์ง€์˜ dp์™€ ์ด๋ฒˆ ๋ฐ•์Šค์˜ boxDist๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ ์ƒํƒœ(ndp)๋ฅผ ๊ณ„์‚ฐ
            int[] ndp = 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 ns = mult[i][j]; // ํ˜„์žฌ ์ƒํƒœ i์—์„œ ๋ฐ•์Šค์˜ ์„ ํƒ j๋ฅผ ์ ์šฉํ•œ ์ƒˆ๋กœ์šด ์ƒํƒœ
                    ndp[ns] = (int) ((ndp[ns] + (long) dp[i] * boxDist[j]) % mod);
                }
            }
            // dp ๋ฐฐ์—ด ๊ฐฑ์‹ 
            dp = ndp;
        }
        // ์ตœ์ข…์ ์œผ๋กœ dp ๋ฐฐ์—ด์—์„œ ๋ชฉํ‘œ ์ƒํƒœ k (์ฆ‰, ์•ฝ์ˆ˜ ์ƒํƒœ๊ฐ€ k๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ)์˜ ๊ฐ’์„ ์ถœ๋ ฅ
        System.out.println(dp[dIndex.get(k)] % mod);
    }
    
    // ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์„ ์ด์šฉํ•œ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜(GCD) ๊ณ„์‚ฐ ๋ฉ”์†Œ๋“œ
    static long gcd(long a, long b) {
        while (b != 0) {
            long t = a % b;
            a = b;
            b = t;
        }
        return a;
    }
}

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

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


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

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

๋ฐ˜์‘ํ˜•

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

[BOJ/Java] 3628. Knockdown  (0) 2025.03.14
[BOJ/Java] 6902. Orko  (1) 2025.03.13
[BOJ/Java] 11404. ํ”Œ๋กœ์ด๋“œ  (1) 2025.03.10
[BOJ/Java] 2931. ๊ฐ€์Šค๊ด€  (0) 2025.03.10
[BOJ/Java] 11657. ํƒ€์ž„๋จธ์‹   (0) 2025.03.10
Comments