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

12-wkdghdwns199 #51

Merged
merged 3 commits into from
Mar 9, 2024
Merged

12-wkdghdwns199 #51

merged 3 commits into from
Mar 9, 2024

Conversation

wkdghdwns199
Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 commented Mar 6, 2024

πŸ”— 문제 링크

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

문제
μž¬μ›μ΄λŠ” ν•œ λ„μ‹œμ˜ μ‹œμž₯이 λ˜μ—ˆλ‹€. 이 λ„μ‹œμ—λŠ” λ„μ‹œλ₯Ό 동μͺ½κ³Ό μ„œμͺ½μœΌλ‘œ λ‚˜λˆ„λŠ” 큰 일직선 λͺ¨μ–‘μ˜ 강이 흐λ₯΄κ³  μžˆλ‹€. ν•˜μ§€λ§Œ μž¬μ›μ΄λŠ” 닀리가 μ—†μ–΄μ„œ μ‹œλ―Όλ“€μ΄ 강을 κ±΄λ„ˆλŠ”λ° 큰 λΆˆνŽΈμ„ κ²ͺκ³  μžˆμŒμ„ μ•Œκ³  닀리λ₯Ό μ§“κΈ°λ‘œ κ²°μ‹¬ν•˜μ˜€λ‹€. κ°• μ£Όλ³€μ—μ„œ 닀리λ₯Ό 짓기에 μ ν•©ν•œ 곳을 μ‚¬μ΄νŠΈλΌκ³  ν•œλ‹€. μž¬μ›μ΄λŠ” κ°• 주변을 λ©΄λ°€νžˆ 쑰사해 λ³Έ κ²°κ³Ό κ°•μ˜ μ„œμͺ½μ—λŠ” N개의 μ‚¬μ΄νŠΈκ°€ 있고 동μͺ½μ—λŠ” M개의 μ‚¬μ΄νŠΈκ°€ μžˆλ‹€λŠ” 것을 μ•Œμ•˜λ‹€. (N ≀ M)

μž¬μ›μ΄λŠ” μ„œμͺ½μ˜ μ‚¬μ΄νŠΈμ™€ 동μͺ½μ˜ μ‚¬μ΄νŠΈλ₯Ό λ‹€λ¦¬λ‘œ μ—°κ²°ν•˜λ €κ³  ν•œλ‹€. (μ΄λ•Œ ν•œ μ‚¬μ΄νŠΈμ—λŠ” μ΅œλŒ€ ν•œ 개의 λ‹€λ¦¬λ§Œ 연결될 수 μžˆλ‹€.) μž¬μ›μ΄λŠ” 닀리λ₯Ό μ΅œλŒ€ν•œ 많이 μ§€μœΌλ €κ³  ν•˜κΈ° λ•Œλ¬Έμ— μ„œμͺ½μ˜ μ‚¬μ΄νŠΈ 개수만큼 (N개) 닀리λ₯Ό μ§€μœΌλ €κ³  ν•œλ‹€. λ‹€λ¦¬λΌλ¦¬λŠ” μ„œλ‘œ 겹쳐질 수 μ—†λ‹€κ³  ν•  λ•Œ 닀리λ₯Ό 지을 수 μžˆλŠ” 경우의 수λ₯Ό κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ.

μž…λ ₯
μž…λ ₯의 첫 μ€„μ—λŠ” ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ˜ 개수 Tκ°€ 주어진닀. κ·Έ λ‹€μŒ 쀄뢀터 각각의 ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€μ— λŒ€ν•΄ κ°•μ˜ μ„œμͺ½κ³Ό 동μͺ½μ— μžˆλŠ” μ‚¬μ΄νŠΈμ˜ 개수 μ •μˆ˜ N, M (0 < N ≀ M < 30)이 주어진닀.

좜λ ₯
각 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ— λŒ€ν•΄ 주어진 μ‘°κ±΄ν•˜μ— 닀리λ₯Ό 지을 수 μžˆλŠ” 경우의 수λ₯Ό 좜λ ₯ν•œλ‹€.

예제 μž…λ ₯ 1
3
2 2
1 5
13 29

예제 좜λ ₯ 1
1
5
67863915

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

1μ‹œκ°„ - DP λ¬Έμ œλŠ” μš°μ„  경우의 수λ₯Ό λ”°μ Έμ„œ ν‘œμ— 그렀보고 κ·œμΉ™μ„ 찾으면 λœλ‹€..!

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

닀리가 κ²ΉμΉ˜μ§€ μ•Šκ³  μ—°κ²°ν•  수 μžˆλŠ” 경우의 수λ₯Ό ν‘œλ‘œ κ·Έλ €λ³΄μ•˜μŠ΅λ‹ˆλ‹€. (사진 좜처 : https://roytravel.tistory.com/163)

μœ„μ˜ ν‘œλ₯Ό 보면 μ™Όμͺ½ λŒ€κ°μ„ κ³Ό μ™Όμͺ½μ— μžˆλŠ” 값을 λ”ν•˜λ©΄ N,M 개의 μ‚¬μ΄νŠΈμΌ λ•Œμ˜ 닀리 κ°œμˆ˜κ°€ λ‚˜μ˜¨λ‹€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. DP λ₯Ό ν’€ λ•Œ μ΄λŸ°μ‹μœΌλ‘œ 경우의 수λ₯Ό λ‹€ μ°Ύμ•„μ„œ μ•Œμ•„λ‚΄λŠ” 방법을 써야 ν•œλ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€!

import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
    N,M = map(int, input().split())
    dp = [[0] * (M+1) for _ in range(N+1)]
    for x in range(1, N+1) :
        for y in range(1, M+1) :
            if x == 1 : dp[x][y] = y
            else : dp[x][y] = dp[x-1][y-1] + dp[x][y-1]
    
    print(dp[N][M])

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

@alstjr7437
Copy link
Member

t = int(input())

for i in range(t):
    n, m = map(int, input().split())
    dp = [[0] * m for _ in range(n)]

    
    for j in range(n):
        for z in range(j, m):
            if j == 0:
                dp[j][z] = z + 1
            else :
                dp[j][z] = dp[j-1][z-1] + dp[j][z-1]
    print(dp[n-1][m-1])

μ €λŠ” λ”°λ‘œ μ„œμͺ½μ΄ 더 크면 μ•ˆλ˜λ‹ˆκΉŒ xκ°€ yλΆ€ν„° μ‹œμž‘ν•˜λ„λ‘ ν–ˆμŠ΅λ‹ˆλ‹€!!

Comment on lines +7 to +10
for x in range(1, N+1) :
for y in range(1, M+1) :
if x == 1 : dp[x][y] = y
else : dp[x][y] = dp[x-1][y-1] + dp[x][y-1]
Copy link
Member

Choose a reason for hiding this comment

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

이뢀뢄 ν˜Ήμ‹œ λ³€μˆ˜λͺ…에 xλž‘ yκ°€ λ°˜λŒ€λ‘œ 된 것 κ°™μ•„μš”!!
이차원 λ°°μ—΄μ˜ μ•žλΆ€λΆ„μ΄ y 뒷뢀뢄이 x 인 것 κ°™μŠ΅λ‹ˆλ‹€!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

μ•„ κ·Έλ ‡λ„€μš”! 어쩐지 μ²˜μŒμ— μ΄μƒν•˜κ²Œ μ—λŸ¬ λœ¨λ”λΌκ΅¬μš”. 감사함닀 πŸ₯Ή

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

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

DP λ¬Έμ œλŠ” μ²˜μŒλ΄€μ„ λ•Œ 큰 문제λ₯Ό μž‘κ²Œ μͺΌκ°œμ„œ μƒκ°ν•΄λ³΄λŠ” 게 μ€‘μš”ν•˜λ‹€κ³  μƒκ°ν•΄μš”!

https://kau-algorithm.tistory.com/780 이 뢄이 되게 잘 μ •λ¦¬ν•΄μ£Όμ…¨λ„€μš”. 잘 λ΄€μŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

μ €λŠ” 두 κ°€μ§€λ‘œ ν’€μ—ˆλŠ”λ°μš”

첫 번째 풀이

λ‹€λ¦¬λŠ” μ„œλ‘œ 겹치면 μ•ˆλ˜κΈ° λ•Œλ¬Έμ— μˆœμ„œ 상관없이 m 개 쀑에 n 개λ₯Ό λ½‘λŠ” mCn 쑰합을 κ΅¬ν•˜λŠ” 것과 κ°™λ‹€κ³  μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.

λ”°λΌμ„œ μ²˜μŒμ—” λ‹€μŒ κ³΅μ‹μœΌλ‘œ κ³„μ‚°ν–ˆμŠ΅λ‹ˆλ‹€.

image
 fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val t = br.readLine().toInt()
    for (i in 0 until t) {
        val (n, m) = br.readLine().split(" ").map { it.toInt() }
        println((facto(m) / (facto(n) * facto(m - n))).roundToInt())
    }
 }

 private fun facto(n: Int): Double {
    if (n <= 1) {
        return 1.0
    }
    return n * facto(n - 1)
 }

두 번째 풀이

ν’€κ³  λ‚˜λ‹ˆκΉ λ‹€λ₯Έ λΆ„λ“€μ˜ 풀이가 dp λ”λΌκ³ μš”. 점화식이 μ–΄λ–»κ²Œ 생기지 ν•˜λ‹€κ°€ 곡식 ν•˜λ‚˜λ₯Ό 더 λ– μ˜¬λ ΈμŠ΅λ‹ˆλ‹€.

image
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val t = br.readLine().toInt()
    for (i in 0 until t) {
        val (n, m) = br.readLine().split(" ").map { it.toInt() }
        val dp = Array(n + 1) { Array(m + 1) { 0 } }
        for (x in 1..n) {
            for (y in x..m) {
                if (x == y) {
                    dp[x][y] = 1
                    continue
                }
                if (x == 1) {
                    dp[x][y] = y
                    continue
                }
                dp[x][y] = dp[x][y - 1] + dp[x - 1][y - 1]
            }
        }
        println(dp[n][m])
    }
}

이건 λ˜‘κ°™λ„€μš© κ΅Ώ

@wkdghdwns199 wkdghdwns199 merged commit 44c30c5 into main Mar 9, 2024
7 checks passed
@wkdghdwns199 wkdghdwns199 deleted the 12-wkdghdwns199 branch March 9, 2024 14:24
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