Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-mjj111 #209

Merged
merged 1 commit into from
Sep 15, 2024
Merged

10-mjj111 #209

merged 1 commit into from
Sep 15, 2024

Conversation

mjj111
Copy link
Collaborator

@mjj111 mjj111 commented Jul 30, 2024

πŸ”— 문제 링크

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

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

30λΆ„

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

ν•΄λ‹Ή λ¬Έμ œλŠ” μ΄ν•­κ³„μˆ˜μ˜ 결과값을 λ°˜ν™˜ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.
문제의 μž…λ ₯ κ°’μ˜ μ œν•œμ„ 보면 Nκ³Ό KλŠ” 1이상 1000μ΄ν•˜μž…λ‹ˆλ‹€.

image

보톡 μ΄ν•­κ³„μˆ˜λ₯Ό 계산할 λ•Œ νŽ™ν† λ¦¬μ–Όμ„ μ‚¬μš©ν•΄μ„œ ν’€μ§€λ§Œ,
νŽ™ν† λ¦¬μ–Όμ„ 반볡문 ν˜Ήμ€ μž¬κ·€λ₯Ό 톡해 ν’€κ²Œλ˜λ©΄
μ΅œλŒ€κ°’μ΄ 1000! 이 κ°€λŠ₯ν•΄μ§‘λ‹ˆλ‹€. 이러면.. int, longμœΌλ‘œλ„ ν’€ 수 μ—†κ²Œ 되죠.

κ·Έλž˜μ„œ μ΅œλŒ€ν•œ 좔가적인 μˆ˜ν•™μ  접근을 μ€„μ΄λ©΄μ„œ.. κ°€λŠ₯ν•œ 방법이 무엇이 μžˆμ„κΉŒ κ³ λ―Όν•˜λ‹€κ°€.
nCk = (n-1)C(k-1) + (n-1)C(k)λ₯Ό ν™œμš©ν•˜κΈ°λ‘œ ν–ˆμŠ΅λ‹ˆλ‹€.

        // nCk = (n-1)C(k-1) + (n-1)C(k)
        return dp[n][k] = (binomialCoefficient(n - 1, k - 1) 
                + binomialCoefficient(n - 1, k)) % MOD;

이 같은 경우 DP둜 점화식을 두면 O(n**2) μ•ˆμ— ν•΄κ²° κ°€λŠ₯ν•˜κ³ 
1000! κ³Ό 같이 큰 값에 μ ‘κ·Όν•˜λŠ” 경우λ₯Ό 없앨 수 μžˆκ²Œλ©λ‹ˆλ‹€.

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

DP와 λ“œλ””μ–΄ μΉœν•΄μ§€κΈ° μ‹œμž‘ν•˜λŠ” κΈ°λΆ„μ΄μ—ˆμŠ΅λ‹ˆλ‹€ γ…Ž

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

이항 κ³„μˆ˜ μ˜€λžœλ§Œμ΄λ„€μš”...
$\binom {n} {k} = \binom {n-1} {k-1} + \binom {n-1} {k}$ μ΄λŸ¬ν•œ 점화식이 μ•Œλ €μ ΈμžˆμœΌλ‹ˆ, 2차원 DP ν…Œμ΄λΈ”μ„ λ§Œλ“€κ³  bottom-up λ°©μ‹μœΌλ‘œλ„ ν’€μ–΄λ‚Ό 수 있죠 :)

#include <cstdio>

constexpr int MOD = 10007;
constexpr int MAX_N = 1000, MAX_K = 1000;

int N, K; 
int DP[MAX_N + 1][MAX_K + 1];

int main()
{
    scanf(" %d %d", &N, &K);
    
    for(int n = 1; n <= N; ++n)
    {
        for(int k = 0; k <= n; ++k)
        {
            if(k == 0 || n == k)
            {
                DP[n][k] = 1;
                continue;
            }
            
            
            // nCk = n-1Ck-1 + n-1Ck
            DP[n][k] = (DP[n - 1][k - 1] + DP[n - 1][k]) % MOD;
        }
    }
    
    printf("%d", DP[N][K]);

    return 0;
}

TMI: C/C++μ—μ„œ 또 κ³ λ €ν•΄μ•Όν•  게, main ν•¨μˆ˜ μ•ˆμ— dp ν…Œμ΄λΈ”μ„ λ§Œλ“€λ©΄ 크기가 λ„ˆλ¬΄ μ»€μ„œ λŸ°νƒ€μž„ μ—λŸ¬(...)κ°€ λ°œμƒν•©λ‹ˆλ‹€. κ·Έλž˜μ„œ μ–΄μ©” 수 없이 μ „μ—­λ³€μˆ˜λ‘œ...

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

생각보닀 μ–΄λ €μš΄ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€..
μž¬κ·€μ μœΌλ‘œ ν•¨μˆ˜ ν˜ΈμΆœν•˜λŠ” 방법은 μ‹œκ°„μ΄ˆκ³Όκ°€ λ‚˜λ”λΌκ΅¬μš”...? (μ œκ°€ 잘 λͺ»ν•΄μ„œ 그런걸 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€ ν—ˆν—ˆ..)
κ·Έλž˜μ„œ λΈ”λ‘œκ·Έ μ°Έκ³ ν•΄μ„œ 배열에 μ €μž₯ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν’€μ–΄μ„œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

input = open(0).readline

n, r = map(int, input().split())
dp = [[1 for _ in range(r+1)] for _ in range(n+1)]

for j in range(1, r+1):
    for i in range(j+1, n+1):
        dp[i][j] = dp[i-1][j-1] + dp[i-1][j]

print(dp[n][r]%10007)

@9kyo-hwang 9kyo-hwang merged commit 9e90104 into main Sep 15, 2024
10 checks passed
@9kyo-hwang 9kyo-hwang deleted the 10-mjj111 branch September 15, 2024 15:32
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.

3 participants