-
Notifications
You must be signed in to change notification settings - Fork 1
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
12-wkdghdwns199 #51
Conversation
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λΆν° μμνλλ‘ νμ΅λλ€!! |
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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄λΆλΆ νΉμ λ³μλͺ
μ xλ yκ° λ°λλ‘ λ κ² κ°μμ!!
μ΄μ°¨μ λ°°μ΄μ μλΆλΆμ΄ y λ·λΆλΆμ΄ x μΈ κ² κ°μ΅λλ€!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ κ·Έλ λ€μ! μ΄μ©μ§ μ²μμ μ΄μνκ² μλ¬ λ¨λλΌκ΅¬μ. κ°μ¬ν¨λ€ π₯Ή
There was a problem hiding this 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 μ΄ λΆμ΄ λκ² μ μ 리ν΄μ£Όμ ¨λ€μ. μ λ΄€μ΅λλ€.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ λ κ°μ§λ‘ νμλλ°μ
첫 λ²μ§Έ νμ΄
λ€λ¦¬λ μλ‘ κ²ΉμΉλ©΄ μλκΈ° λλ¬Έμ μμ μκ΄μμ΄ m κ° μ€μ n κ°λ₯Ό λ½λ mCn μ‘°ν©μ ꡬνλ κ²κ³Ό κ°λ€κ³ μκ°νμ΅λλ€.
λ°λΌμ μ²μμ λ€μ 곡μμΌλ‘ κ³μ°νμ΅λλ€.
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 λλΌκ³ μ. μ νμμ΄ μ΄λ»κ² μκΈ°μ§ νλ€κ° 곡μ νλλ₯Ό λ λ μ¬λ Έμ΅λλ€.
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])
}
}
μ΄κ±΄ λκ°λ€μ© κ΅Ώ
π λ¬Έμ λ§ν¬
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 λ₯Ό ν λ μ΄λ°μμΌλ‘ κ²½μ°μ μλ₯Ό λ€ μ°Ύμμ μμλ΄λ λ°©λ²μ μ¨μΌ νλ€λ κ²μ κΉ¨λ¬μμ΅λλ€!
π μλ‘κ² μκ²λ λ΄μ©