-
Notifications
You must be signed in to change notification settings - Fork 2
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
8-H0ngJu #170
Conversation
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.
1, 2 μ°¨μ dpν μ΄λΈ λ λ€ μ¬μ©ν΄μ νμ΄λ³΄μμ΅λλ€
μ΄μ μλ¦Ώμμμμ κ°μ κΈ°λ°μΌλ‘ κ³λ¨ μ κ°μλ₯Ό ꡬνλ κ±°λ€λ³΄λ
1μ°¨μ 리μ€νΈλ‘λ νμ΄κ° κ°λ₯ν©λλ€
1μ°¨μ νμ΄
dp
λ₯Ό κ·Έλλ‘ μ°λ©΄μ κ³μ°νλ©΄ μ΄μ μλ¦Ώμμμμ κ°μ΄ λ°λκΈ° λλ¬Έμ
new_dp
λ‘ λ¦¬μ€νΈλ₯Ό μλ‘ μμ±ν΄μ μ΄μ κ°μ 보쑴μν¨ ν μ°μ° ν©λλ€
dp[i]
λ₯Ό dp[1],` dp[2] μμλλ‘ μ°μ°ν λ dp[2], dp[0], dp[3], dp[1] μ κ°μ΄ λ³κ²½λ©λλ€
μ΄ κ³Όμ μμ dp[3]μ κ°μ΄ iκ° 3μ΄ λκΈ°λ μ μ λ°λμ΄ λ²λ¦½λλ€
κ·Έλμ iκ° 3μ΄ λμμ λ μ΄μ μλ¦Ώμμμμ κ°μ΄ μμ€λ μνμ΄λ―λ‘ κ³λ¨ μμ κ°μλ₯Ό μ λλ‘ κ΅¬ν μκ° μμ΅λλ€
N = int(input())
MOD = 1_000_000_000
dp = [1] * 10
dp[0] = 0
for _ in range(1, N):
new_dp = [0] * 10
for i, c in enumerate(dp):
if i < 9:
new_dp[i + 1] += c % MOD
if i > 0:
new_dp[i - 1] += c % MOD
dp = new_dp
print(sum(dp) % MOD)
2μ°¨μ νμ΄
νμ£Όλ μ½λλ κ±°μ λκ°κ΅°μ
dp = [[0] * 10 for _ in range(N)]
for i in range(1, 10):
dp[0][i] = 1
for i in range(1, N):
for j in range(10):
if j < 9:
dp[i][j] += dp[i - 1][j + 1] % MOD
if j > 0:
dp[i][j] += dp[i - 1][j - 1] % MOD
print(sum(dp[-1]) % MOD)
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.
νμ£Όλ PRμ 보λ DPμ μ¬κ³ λ°©μμ΄ λ무 μ νν©λλ€.
κ·Έλ κ² DPλ₯Ό μ κ·Όν΄μ£Όμλ©΄ μ’μ κ² κ°μμ.
"μ΄λ€ κ°μ κΈ°μ΅ν΄λμΌ λ€μ κ°μ μ»μ μ μμ§?"
"μ΄λ€ μ λ³΄κ° μμ΄μΌμ§ λκ°μ μ°μ°μ λ°λ³΅νμ§ μμ μ μμκΉ?
Nλ²μ§Έ κ°μ ꡬνκΈ° μν΄ N-1λ²μ§Έμμ μ΄λ€ μ λ³΄κ° νμνμ§? λ±λ± μ΄μ...!
μμ£Ό μ’μμ,
N = int(input())
dp_table = [[0 for _ in range(10)] for _ in range(N+1)]
for i in range(1,10):
dp_table[1][i] = 1
for step in range(2,N+1):
for num in range(10):
if num != 0 and num != 9:
dp_table[step][num] = dp_table[step-1][num-1] + dp_table[step-1][num+1]
elif num == 0:
dp_table[step][num] = dp_table[step-1][num+1]
elif num == 9:
dp_table[step][num] = dp_table[step-1][num-1]
print(sum(dp_table[-1])%1_000_000_000)
#include <iostream>
#include <functional>
#include <unordered_map>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
unordered_map<int, unordered_map<int, int>> memo;
for (int i = 0; i <= 9; i++) {
memo[i][1] = 1;
}
int MOD = 1e9;
function<int(int, int)> dp = [&](int head, int digit) {
if (memo.contains(head) && memo[head].contains(digit)) {
return memo[head][digit];
}
if (head == 0) {
return memo[head][digit] = dp(1, digit - 1);
}
if (head == 9) {
return memo[head][digit] = dp(8, digit - 1);
}
return memo[head][digit] = (dp(head - 1, digit - 1) + dp(head + 1, digit - 1)) % MOD;
};
int N;
cin >> N;
int answer = 0;
for (int i = 1; i <= 9; i++) {
answer += dp(i, N);
answer %= MOD;
}
cout << answer;
return 0;
} κ³λ¨ μμ μλΈμνμ€λ κ³λ¨ μ -> DPλ₯Ό λ μ¬λ¦¬κ³ νλ€μ΄μΌλ‘ ꡬννμ΅λλ€. λ€λ₯Έ λΆλ€ νμ΄ λ³΄λκΉ λ°ν μ μ΄ λ κΉλν΄λ³΄μ΄λ€μ λ€μμλ λ°ν μ μΌλ‘ νΈλ λ°©λ²λ μ°μ΅ν΄μ€κ² μ΅λλ€. |
π λ¬Έμ λ§ν¬
μ¬μ΄ κ³λ¨ μ
βοΈ μμλ μκ°
1μκ°?
β¨ μλ μ½λ
μ΄ λ¬Έμ λ μ‘°κΈλ§ μκ°ν΄λ³΄λ©΄ κΈΈμ΄κ° K+1μΈ κ³λ¨ μλ KμΈ κ³λ¨ μμ λμλ¦¬κ° μ΄λ»κ² λλλμ λ°λΌμ κ²½μ°μ μκ° 1μΈμ§ 2μΈμ§λ‘ ꡬλΆλλ κ²μ μ μ μμ΅λλ€.
"μ΄μ κΈΈμ΄μ κ³λ¨ μμ λμλ¦¬λ§ μλ©΄ λ€μ κ³λ¨ μλ₯Ό ꡬν μ μλ€"λ κ²μ μμμ§λ§, λ¬Έμ λ 'κ³λ¨ μμ λμ리λ₯Ό μ΄λ»κ² μ μ₯ν κ²μΈκ°?'μμ΅λλ€.
κ·Έλ°λ° μ‘°κΈλ§ λ μκ°ν΄λ³΄λ©΄,
κΈΈμ΄κ° K+1μΈ κ³λ¨ μλ₯Ό ꡬνλ€κ³ κ°μ νμμ λ, Kλ²μ§Έ κ³λ¨ μμ λμλ¦¬κ° Nμ΄λΌκ³ νλ©΄,
Nμ΄ 0μ΄λ©΄ -> μ¬λΌκ°λ κ²½μ°μ μ λ°μ μμΌλ―λ‘ κΈΈμ΄κ° Kλ©΄μ Nμ΄ 0μΈ κ²½μ°μ μ μ μ§
Nμ΄ 9μ΄λ©΄ -> λ΄λ €κ°λ κ²½μ°μ μ λ°μ μμΌλ―λ‘ κΈΈμ΄κ° Kλ©΄μ Nμ΄ 9μΈ κ²½μ°μ μ μ μ§
Nμ΄ 2~8μ΄λ©΄ -> λ΄λ €κ°κ³ μ¬λΌκ°λ κ²½μ°μ μ(2κ°μ§)κ° μ‘΄μ¬νλ―λ‘ κΈΈμ΄κ° K μ΄λ©΄μ Nμ΄ N-1,N+1κ²½μ°μ ν©
μ΄ λ©λλ€.
λ°λΌμ 2μ°¨μ DPλ₯Ό μμ±νκ³ , λ€μκ³Ό κ°μ μ νμμ μΈμ°λ©΄ μ½κ² λ¬Έμ λ₯Ό ν μ μμ΅λλ€.
π μλ‘κ² μκ²λ λ΄μ©