Skip to content

Conversation

@hadongun
Copy link
Collaborator

@hadongun hadongun commented May 9, 2025

πŸ”— 문제 링크

https://www.acmicpc.net/problem/11726

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1h

✨ μˆ˜λ„ μ½”λ“œ

λ„ν˜„λ‹˜μ΄ μΆ”μ²œν•΄μ£Όμ‹  κ³¨λ“œ μŠ€νƒ 문제 ν’€λ‹€κ°€ μ‹€νŒ¨ν•΄μ„œ λ‹€λ₯Έ 문제λ₯Ό κ°€μ Έμ™€λ΄€μŠ΅λ‹ˆλ‹΅

이 λ¬Έμ œλŠ” 2 x n 크기의 μ§μ‚¬κ°ν˜•μ— 1x2 μ§μ‚¬κ°ν˜•κ³Ό 2x1 μ§μ‚¬κ°ν˜•μœΌλ‘œ μ±„μš°λŠ” λ°©λ²•μ˜ 수λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹Ή
음.. μ²˜μŒμ— μƒκ°ν•˜λ‹€ λ– μ˜€λ₯Έ 게 μ‘°ν•©(Combination)을 ν™œμš©ν•΄λ³΄μž..! λΌλŠ” 생각을 ν–ˆμ–΄μš”
1x2 타일을 μ„Έλ‘œ 타일, 2x1타일을 κ°€λ‘œ 타일이라고 λΆ€λ₯Όκ²Œμš”!..
κ°€λ‘œ 타일은 κΉ”λ©΄ 1ν–‰, 2행에 λ™μ‹œμ— κΉ”μ•„μ•Ό ν•˜λ‹ˆκΉŒ -> λ‘˜μ€ μ„ΈνŠΈλ‘œ 보고 ν•˜λ‚˜λΌκ³  ν‘œν˜„ν•˜κ² μŠ΅λ‹ˆλ‹€(λ°©λ²•μ˜ 수 ꡬ할 λ•Œ μ§€μž₯ x)

μ €λŠ” 기본적으둜 μ„Έλ‘œ 타일이 λͺ¨λ‘ κΉ”λ €μžˆλ‹€κ³  κ°€μ •ν•œ μƒνƒœμ—μ„œ κ°€λ‘œ νƒ€μΌμ˜ 개수λ₯Ό i둜 λ‘μ—ˆμŠ΅λ‹ˆλ‹€.
for문을 ν™œμš©ν•΄ κ°€λ‘œ 타일을 1κ°œμ”© λŠ˜λ €κ°€λŠ”λ°, 이λ₯Ό 쑰합을 ν™œμš©ν•΄ 배치 κ°€λŠ₯ν•œ λ°©λ²•μ˜ 수λ₯Ό ꡬ해 λͺ¨λ‘ λ”ν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.!

μ§€κΈˆ λ³΄λ‹ˆ μ½”λ“œ 주석이 잘λͺ»λλ„€μš© ν’€λ‹€ ν—·κ°ˆλ €μ„œ μ˜ˆμ „μ— λ‹¬μ•„λ†¨λ˜ 주석인데 λ³€κ²½ν•œ 버전 μˆ˜λ„μ½”λ“œμ— μ˜¬λ €λ†“μ„κ²Œμš”

νŒŒμ΄ν—Œ

n ← μž…λ ₯λ°›κΈ° (μ •μˆ˜)
method_num ← 0

반볡 i = 0λΆ€ν„° (n // 2)κΉŒμ§€:
    a. κ°€λ‘œ νƒ€μΌμ˜ κ°œμˆ˜λŠ” i개 (각 타일은 κ°€λ‘œλ‘œ 2μΉΈ μ°¨μ§€)
    b. μ„Έλ‘œ νƒ€μΌμ˜ κ°œμˆ˜λŠ” n - 2*i개 (각 타일은 κ°€λ‘œλ‘œ 1μΉΈ μ°¨μ§€)
    c. 전체 타일 κ°œμˆ˜λŠ” (n - i)개

    d. method_num += μ‘°ν•©(n - i, i)
       (총 타일 자리 n - i개 쀑 iμžλ¦¬μ— κ°€λ‘œ 타일을 λ†“λŠ” 경우의 수)

좜λ ₯(method_num % 10007)

## πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@froglike6
Copy link
Collaborator

froglike6 commented May 9, 2025

μ €λŠ” DP와 행렬을 μ΄μš©ν•΄μ„œ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€. λ¨Όμ € κ°€μž₯ μ™Όμͺ½μ— μ–΄λ–€ 타일을 두냐에 λ”°λΌμ„œ 두가지 μΌ€μ΄μŠ€λ‘œ λ‚˜λ‰©λ‹ˆλ‹€.

κ°€λ‘œκ°€ 1이고 μ„Έλ‘œκ°€ 2인 타일을 λ‘λŠ” κ²½μš°λŠ” 였λ₯Έμͺ½μ— κ°€λ‘œ $n-1$개의 곡간이 λ‚¨μœΌλ―€λ‘œ, 이λ₯Ό κ³„μ‚°ν•˜λ©΄ λ©λ‹ˆλ‹€.
κ°€λ‘œκ°€ 2이고 μ„Έλ‘œκ°€ 1인 타일을 λ‘λŠ” κ²½μš°λŠ” 였λ₯Έμͺ½μ— κ°€λ‘œ $n-2$개의 곡간이 λ‚¨μœΌλ―€λ‘œ, 이λ₯Ό κ³„μ‚°ν•˜λ©΄ λ©λ‹ˆλ‹€.

2Γ—n μ§μ‚¬κ°ν˜•μ„ 1Γ—2, 2Γ—1 νƒ€μΌλ‘œ μ±„μš°λŠ” λ°©λ²•μ˜ 수λ₯Ό $T(n)$이라고 ν•˜λ©΄, $T(n)=T(n-1)+T(n-2)$라고 ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” ν”Όλ³΄λ‚˜μΉ˜ ν•¨μˆ˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ $T(n)=F(n+1)$이 μ„±λ¦½ν•©λ‹ˆλ‹€. 이제 #9 λ₯Ό 톡해 ν’€ 수 μžˆμŠ΅λ‹ˆλ‹€!! (DP둜 ν‘Όλ‹€κ³  ν–ˆλŠ”λ° μ•ˆ μΌλ„€μš” γ…Žγ…Ž)

import sys
input = sys.stdin.readline
def fibonacci(n):
    mat = [[1, 1], [1, 0]]
    vec = [1, 0]

    while n > 0:
        if n & 1:
            temp = (mat[0][0] * vec[0] + mat[0][1] * vec[1]) % 10007
            vec[1] = (mat[1][0] * vec[0] + mat[1][1] * vec[1]) % 10007
            vec[0] = temp

        mat00 = (mat[0][0] * mat[0][0] + mat[0][1] * mat[1][0]) % 10007
        mat01 = (mat[0][0] * mat[0][1] + mat[0][1] * mat[1][1]) % 10007
        mat10 = (mat[1][0] * mat[0][0] + mat[1][1] * mat[1][0]) % 10007
        mat11 = (mat[1][0] * mat[0][1] + mat[1][1] * mat[1][1]) % 10007
        mat[0][0], mat[0][1] = mat00, mat01
        mat[1][0], mat[1][1] = mat10, mat11

        n >>= 1

    return vec[1]
print(fibonacci(int(input())+1)%10007)

+μ˜ˆμ „μ— DPλ₯Ό μ΄μš©ν•œ ν”Όλ³΄λ‹ˆμΉ˜ ν•¨μˆ˜ μ½”λ“œκ°€ μžˆμ–΄μ„œ, 살짝 μˆ˜μ •ν•΄μ„œ κ°€μ Έμ™€λ΄€μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

DP μ½”λ“œ

section .data
    fmt_in  db "%d", 0
    fmt_out db "%d", 10, 0
    MOD     equ 10007

section .bss
    n       resd 1

section .text
    extern scanf, printf
    global main

main:
    push rbp
    mov rbp, rsp

    lea rdi, [rel fmt_in]
    lea rsi, [rel n]
    xor eax, eax
    call scanf
    mov r9, [n]

    cmp r9, 1
    je .print_1
    cmp r9, 2
    je .print_2

    mov rbx, 1
    mov rcx, 2
    mov r8,  3

.loop:
    mov rax, rbx
    add rax, rcx

    xor rdx, rdx
    mov rsi, MOD
    div rsi

    mov rbx, rcx
    mov rcx, rdx

    inc r8
    cmp r8, r9
    jle .loop

    mov esi, ecx
    lea rdi, [rel fmt_out]
    xor eax, eax
    call printf
    jmp .done

.print_1:
    mov esi, 1
    lea rdi, [rel fmt_out]
    xor eax, eax
    call printf
    jmp .done

.print_2:
    mov esi, 2
    lea rdi, [rel fmt_out]
    xor eax, eax
    call printf

.done:
    pop rbp
    mov rax, 0
    ret

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

였호...쑰합을 ν™œμš©ν•œ ν’€μ΄λΌλ‹ˆ μƒˆλ‘­λ„€μš”..
μ €λŠ” DP둜 ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

예제 μž…λ ₯μ—μ„œ 9κ°€ 55λΌλŠ” 좜λ ₯을 가짐을 ν™•μΈν•˜κ³  dp문제일까 μƒκ°ν•΄λ΄€μ”λ‹ˆλ‹€.
(55λŠ” 10번째 ν”Όλ³΄λ‚˜μΉ˜ μˆ˜μž…λ‹ˆλ‹€. ν”Όλ³΄λ‚˜μΉ˜ μˆ˜μ—΄μ—μ„œλŠ” 첫번째, λ‘λ²ˆμ§Έ μˆ˜κ°€ 1의 값을 κ°€μ§€μ§€λ§Œ
이 λ¬Έμ œμ—μ„œλŠ” 같은 κ²½μš°κ°€ λ‚˜μ˜¬ 수 μ—†κΈ° λ•Œλ¬Έμ— ν•œμΉΈ μ•žλ‹Ήκ²¨ μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.)

$n = 1$ ~ $n = 4$ κΉŒμ§€λ§Œ 보아도 κ°„λ‹¨ν•œ 점화식을 μ„Έμ›Œλ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.
$n = 1 : 1$
$n = 2 : 2$
$n = 3 : 3$
$n = 4 : 5$

n = i일 λ•Œμ˜ 경우의 μˆ˜λŠ”
$dp[i] = dp[i-1] + dp[i-2]$

이 μ ν™”μ‹μ—μ„œ $dp[9]$ = 55μž„μ„ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

단 λ¬Έμ œμ—μ„œλŠ” μˆ˜κ°€ λ„ˆλ¬΄ μ»€μ§€λŠ” 것을 μš°λ €ν•΄ 10007둜 λ‚˜λˆˆ λ‚˜λ¨Έμ§€λ₯Ό 좜λ ₯ν•˜λ„λ‘ ν•˜λ‹ˆ
각 dp[i]에 %10007만 μΆ”κ°€ν•΄μ£Όλ©΄ 문제λ₯Ό ν•΄κ²° ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

C++
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    
    vector<int> dp(n+1);
    dp[1] = 1;
    dp[2] = 2;
    for(int i = 3; i <= n; i++)
        dp[i] = (dp[i-1]+dp[i-2])%10007;
    
    cout << dp[n] << '\n';
    return 0;
}

쑰합을 ν™œμš©ν•œ ν’€μ΄λŠ” μ‹ λ°•ν•΄μ„œ 쑰금 곡뢀해볼 κ°€μΉ˜κ°€ μžˆλŠ”κ²ƒ κ°™λ„€μš” γ…Žγ…Ž

Copy link
Collaborator

@caucsejunseo caucsejunseo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ œκ°€ μ–Όλ§ˆμ „μ— ν’€μ—ˆλ˜ 문제λ₯Ό λ³΄λ‹ˆ λ°˜κ°‘λ„€μš”! μ €λŠ” λ„ν˜„λ‹˜μ΄λž‘ μ™„μ „ λ˜‘κ°™μ΄ ν’€μ—ˆμŠ΅λ‹ˆλ‹΄. 맨 λ§ˆμ§€λ§‰μ— κ°€λ‘œ 2κ°œκ°€ μ˜€λŠκ±°λž‘ μ„Έλ‘œ 1κ°œκ°€ μ˜€λŠ”κ±°λž‘ μΌ€μ΄μŠ€ λΆ„λ₯˜ν•΄μ„œ ν’€μ—ˆμ–΄μ—¬

#include <stdlib.h>

int main() {
    int n = 0;
    scanf("%d", &n);

    int* dp = (int*)malloc(sizeof(int) * (n + 1));

    dp[0] = 0;
    if (n >= 1) dp[1] = 1;
    if (n >= 2) dp[2] = 2;

    for (int i = 3; i <= n; i++) {
        dp[i] = (dp[i - 1] + dp[i - 2]) % 10007;
    }

    printf("%d", dp[n] % 10007);

    free(dp);
    return 0;
}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants